Created
April 12, 2019 03:04
-
-
Save tervay/d4bb1edada84e76e0d8bf9198bcc579d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import re | |
| import pathlib | |
| import gzip | |
| import json | |
| import tbapy.models | |
| cache_dir = 'cache/' | |
| fn_dir = 'fn/' | |
| api_dir = 'api/' | |
| # https://github.com/django/django/blob/master/django/utils/text.py#L219 | |
| def get_valid_filename(s): | |
| s = str(s).strip().replace(' ', '_') | |
| return re.sub(r'(?u)[^-\w.]', '', s) | |
| def call(fn, refresh=False, *args, **kwargs): | |
| filepath = f'{cache_dir}{fn_dir}{get_valid_filename(fn.__name__)}_{get_valid_filename(str(args))}_' \ | |
| f'{get_valid_filename(kwargs)}' | |
| # pathlib.Path(filepath).mkdir(parents=True, exist_ok=True) | |
| if os.path.exists(f'{filepath}.json.gz') and not refresh: | |
| with gzip.open(f'{filepath}.json.gz', 'rb') as f: | |
| content = json.loads(f.read()) | |
| return content | |
| else: | |
| data = fn(*args, **kwargs) | |
| with gzip.open(f'{filepath}.json.gz', 'wb+') as f: | |
| f.write(json.dumps(data).encode()) | |
| if isinstance(data, tbapy.models._base_model_class): | |
| data = dict(data) | |
| return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment