python package for felxible and persisting caching of function calls (e.g. API calls)
- Python 100%
| .github/workflows | Update python-app.yml | |
| src/cachewrapper | refactor: fix spelling | |
| tests | refactor: fix spelling | |
| .gitignore | refactor: fix spelling | |
| LICENSE | first commit (empty project structure) | |
| MANIFEST.in | first commit (empty project structure) | |
| README.md | doc (README.md): improve example | |
| requirements.txt | first commit (empty project structure) | |
| setup.py | first commit (empty project structure) | |
Code style: black Python application
Cachewrapper
Use case: you have modules or objects whose methods you want to call. These calls might be expensive (e.g. rate-limited API calls). Thus you do not want to make unnecessary calls which would only give results that you already have. However, during testing repeatedly calling these methods is unavoidable. Cachewrapper solves this by automatically providing a cache for all calls.
Currently this package is an early prototype, mainly for personal use.
Installation
- clone the repository
- run
pip install -e .(run from wheresetup.pylives).
Usage Example
This is extracted from a real use case (translating a math ontology which originally contained mainly Russian labels) and not directly executable due to abridgement.
import os
from tqdm import tqdm
import cachewrapper as cw
# suppose data contains strings which should be translated into english
from . import data
# rate limited API module
from translate import Translator
cache_path = "translate_cache.pcl"
cached_translator = cw.CacheWrapper(Translator)
if os.path.isfile(cache_path):
cached_translator.load_cache(cache_path)
res_list = []
for original_label in tqdm(data.untranslated_labels):
translation = cached_translator.translate(original_label)
if "MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY" in translation:
# we ran into the rate-limit -> we do not want to save this result
cached_translator._remove_last_key()
break
record = {
"ru": f"{original_label}",
"en": f"{translation}",
}
res_list.append(record)
cached_translator.save_cache(cache_path)