1
0
Fork
You've already forked cachewrapper
0
python package for felxible and persisting caching of function calls (e.g. API calls)
  • Python 100%
2025年04月09日 19:15:28 +02:00
.github/workflows Update python-app.yml 2022年01月13日 19:18:50 +01:00
src/cachewrapper refactor: fix spelling 2025年04月09日 19:15:28 +02:00
tests refactor: fix spelling 2025年04月09日 19:15:28 +02:00
.gitignore refactor: fix spelling 2025年04月09日 19:15:28 +02:00
LICENSE first commit (empty project structure) 2021年11月18日 12:57:51 +01:00
MANIFEST.in first commit (empty project structure) 2021年11月18日 12:57:51 +01:00
README.md doc (README.md): improve example 2025年04月09日 19:13:03 +02:00
requirements.txt first commit (empty project structure) 2021年11月18日 12:57:51 +01:00
setup.py first commit (empty project structure) 2021年11月18日 12:57:51 +01:00

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 where setup.py lives).

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)