Afegit endpoint de calcular presència.
This commit is contained in:
32
main.py
32
main.py
@@ -1,11 +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"}
|
||||||
import dibuixa
|
|
||||||
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
@@ -29,10 +28,29 @@ def handle_errors(func):
|
|||||||
return wrapped
|
return wrapped
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/calcula", methods=['POST'])
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def hola(code):
|
def calcula():
|
||||||
return "Hola"
|
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__":
|
||||||
|
|||||||
Reference in New Issue
Block a user