Merge branch 'main' of ssh://git.lajuntament.space:2222/lajuntament/absencia-de-presencia

This commit is contained in:
Marce Coll
2021-11-27 12:43:05 +01:00

30
main.py
View File

@@ -1,9 +1,10 @@
import functools import functools
import io import re
import flask import flask
from werkzeug.wsgi import FileWrapper text2bool = {"absència": True, "presència": False, "absent": True, "present": False}
bool2text = {True: "absent", False: "present"}
app = flask.Flask(__name__) app = flask.Flask(__name__)
@@ -33,5 +34,30 @@ def index():
return flask.render_template("./index.html") return flask.render_template("./index.html")
@app.route("/calcula", methods=['POST'])
@handle_errors
def calcula():
frase = flask.request.get_json().get('frase')
if not frase:
raise HTTPError("Cal introduir una frase!", status_code=400, status="Missing parameter")
return calcula_presencia(frase)
def xor(a, b):
return (a and not b) or (b and not a)
def calcula_presencia(frase):
frase = frase[3:]
subjecte, atribut = frase.split(" és ")
llista_subjecte = re.split(" de | d'", subjecte)
objecte = llista_subjecte[-1]
llista_bool = [text2bool[x.strip()] for x in llista_subjecte[:-1]]
bool_total = text2bool[atribut]
for bool in llista_bool:
bool_total = xor(bool_total, bool)
return f'{objecte} és {bool2text[bool_total]}'
if __name__ == "__main__": if __name__ == "__main__":
app.run() app.run()