This commit is contained in:
Lucas Verney 2017-09-24 23:10:07 +02:00
parent 88a923c87e
commit 7e35b71c28
2 changed files with 41 additions and 0 deletions

View File

@ -13,6 +13,7 @@ from flatisfy import database
from flatisfy import data_files from flatisfy import data_files
from flatisfy.models.postal_code import PostalCode from flatisfy.models.postal_code import PostalCode
from flatisfy.models.public_transport import PublicTransport from flatisfy.models.public_transport import PublicTransport
from flatisfy.tools import hash_dict
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -68,6 +69,7 @@ def preprocess_data(config, force=False):
session.add_all(data_objects) session.add_all(data_objects)
@hash_dict
@lru_cache(maxsize=5) @lru_cache(maxsize=5)
def load_data(model, constraint, config): def load_data(model, constraint, config):
""" """

View File

@ -18,6 +18,18 @@ import time
import requests import requests
import unidecode import unidecode
try:
from functools import wraps
except ImportError:
try:
from functools32 import wraps
except ImportError:
def wraps(func):
"""
Identity implementation of ``wraps`` for fallback.
"""
return lambda func: func
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -25,6 +37,33 @@ LOGGER = logging.getLogger(__name__)
NAVITIA_ENDPOINT = "https://api.navitia.io/v1/coverage/fr-idf/journeys" NAVITIA_ENDPOINT = "https://api.navitia.io/v1/coverage/fr-idf/journeys"
def hash_dict(func):
"""
Decorator to use on functions accepting dict parameters, to transform them
into immutable dicts and be able to use lru_cache.
From https://stackoverflow.com/a/44776960.
"""
class HDict(dict):
"""
Transform mutable dictionnary into immutable. Useful to be compatible
with lru_cache
"""
def __hash__(self):
return hash(json.dumps(self))
@wraps(func)
def wrapped(*args, **kwargs):
args = tuple(
[HDict(arg) if isinstance(arg, dict) else arg
for arg in args
])
kwargs = {k: HDict(v) if isinstance(v, dict) else v
for k, v in kwargs.items()}
return func(*args, **kwargs)
return wrapped
class DateAwareJSONEncoder(json.JSONEncoder): class DateAwareJSONEncoder(json.JSONEncoder):
""" """
Extend the default JSON encoder to serialize datetimes to iso strings. Extend the default JSON encoder to serialize datetimes to iso strings.