Reorg repo
This commit is contained in:
0
historico/__init__.py
Normal file
0
historico/__init__.py
Normal file
66
historico/handler.py
Normal file
66
historico/handler.py
Normal file
@ -0,0 +1,66 @@
|
||||
import unicodedata
|
||||
import urllib
|
||||
from collections import defaultdict as ddict
|
||||
from dataclasses import dataclass
|
||||
from json import loads
|
||||
|
||||
import pygal
|
||||
import pyrqlite.dbapi2 as dbapi2
|
||||
import requests
|
||||
|
||||
connection = dbapi2.connect(
|
||||
host="10.61.0.1",
|
||||
user="root",
|
||||
port=4001,
|
||||
password="",
|
||||
)
|
||||
|
||||
|
||||
def remove_accents(input_str):
|
||||
nfkd_form = unicodedata.normalize("NFKD", input_str)
|
||||
return "".join([c for c in nfkd_form if not unicodedata.combining(c)])
|
||||
|
||||
|
||||
def handle(req):
|
||||
"""handle a request to the function
|
||||
Args:
|
||||
req (str): request body
|
||||
|
||||
{"i": ["nombre1, nombre2"]}
|
||||
|
||||
"""
|
||||
nombres = []
|
||||
try:
|
||||
nombres = loads(req)
|
||||
nombres = nombres["i"].split(",")
|
||||
nombres = [remove_accents(x.strip().lower()) for x in nombres]
|
||||
nombres = [n for n in nombres if n]
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not nombres:
|
||||
nombres = ["maria", "juan"]
|
||||
|
||||
chart = pygal.Line(
|
||||
height=200, fill=True, human_readable=True, show_minor_x_labels=False
|
||||
)
|
||||
chart.x_labels = [str(x) for x in range(1922, 2015)]
|
||||
chart.x_labels_major = [str(x) if x % 10 == 0 else "" for x in range(1922, 2015)]
|
||||
for nombre in nombres:
|
||||
datos = ddict(int)
|
||||
with connection.cursor() as cursor:
|
||||
sql = """
|
||||
SELECT anio, contador, nombre
|
||||
FROM nombres
|
||||
WHERE nombre = :nombre
|
||||
ORDER BY anio
|
||||
"""
|
||||
cursor.execute(sql, {"nombre": nombre})
|
||||
datos.update({r["anio"]: r["contador"] for r in cursor.fetchall()})
|
||||
chart.add(nombre.title(), [datos[x] for x in range(1922, 2015)])
|
||||
|
||||
chart.x_labels = [str(n) for n in range(1922, 2015)]
|
||||
chart.x_labels_major = [str(n) for n in range(1920, 2020, 10)]
|
||||
|
||||
# return Response(chart.render(is_unicode=True), mimetype="image/svg+xml")
|
||||
return chart.render(is_unicode=True), 200, {"Content-Type": "image/svg+xml"}
|
11
historico/handler_test.py
Normal file
11
historico/handler_test.py
Normal file
@ -0,0 +1,11 @@
|
||||
from .handler import handle
|
||||
|
||||
# Test your handler here
|
||||
|
||||
# To disable testing, you can set the build_arg `TEST_ENABLED=false` on the CLI or in your stack.yml
|
||||
# https://docs.openfaas.com/reference/yaml/#function-build-args-build-args
|
||||
|
||||
|
||||
def test_handle():
|
||||
# assert handle("input") == "input"
|
||||
pass
|
3
historico/requirements.txt
Normal file
3
historico/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
pygal
|
||||
requests
|
||||
pyrqlite
|
41
historico/tox.ini
Normal file
41
historico/tox.ini
Normal file
@ -0,0 +1,41 @@
|
||||
# If you would like to disable
|
||||
# automated testing during faas-cli build,
|
||||
|
||||
# Replace the content of this file with
|
||||
# [tox]
|
||||
# skipsdist = true
|
||||
|
||||
# You can also edit, remove, or add additional test steps
|
||||
# by editing, removing, or adding new testenv sections
|
||||
|
||||
|
||||
# find out more about tox: https://tox.readthedocs.io/en/latest/
|
||||
[tox]
|
||||
envlist = lint,test
|
||||
skipsdist = true
|
||||
|
||||
[testenv:test]
|
||||
deps =
|
||||
flask
|
||||
pytest
|
||||
-rrequirements.txt
|
||||
commands =
|
||||
# run unit tests with pytest
|
||||
# https://docs.pytest.org/en/stable/
|
||||
# configure by adding a pytest.ini to your handler
|
||||
pytest
|
||||
|
||||
[testenv:lint]
|
||||
deps =
|
||||
flake8
|
||||
commands =
|
||||
flake8 .
|
||||
|
||||
[flake8]
|
||||
count = true
|
||||
max-line-length = 127
|
||||
max-complexity = 10
|
||||
statistics = true
|
||||
# stop the build if there are Python syntax errors or undefined names
|
||||
select = E9,F63,F7,F82
|
||||
show-source = true
|
Reference in New Issue
Block a user