From 08f7847419f5134793f618772f7212d40d98c734 Mon Sep 17 00:00:00 2001 From: L'Ajuntament Date: Sat, 27 Nov 2021 12:36:44 +0100 Subject: [PATCH] =?UTF-8?q?Afegit=20endpoint=20de=20calcular=20pres=C3=A8n?= =?UTF-8?q?cia.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 4c0b147..397b19f 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,10 @@ import functools -import io +import re import flask -from werkzeug.wsgi import FileWrapper - -import dibuixa +text2bool = {"absència": True, "presència": False, "absent": True, "present": False} +bool2text = {True: "absent", False: "present"} app = flask.Flask(__name__) @@ -29,10 +28,29 @@ def handle_errors(func): return wrapped -@app.route("/") +@app.route("/calcula", methods=['POST']) @handle_errors -def hola(code): - return "Hola" +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__":