python package for felxible and persisting caching of function calls (e.g. API calls)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Carsten Knoll 8ac0f01018 md fix in README 5 days ago
.github/workflows Update python-app.yml 4 months ago
src/cachewrapper bump version after release 5 days ago
tests add _remove_last_key 1 week ago
.gitignore first commit (empty project structure) 6 months ago
LICENSE first commit (empty project structure) 6 months ago
MANIFEST.in first commit (empty project structure) 6 months ago
README.md md fix in README 5 days ago
requirements.txt first commit (empty project structure) 6 months ago
setup.py first commit (empty project structure) 6 months ago

README.md

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 usecase (and not directly runable due to abridgement).

import os
from tqdm import tqdm
import cachewrapper as cw

# rate limited API module
from translate import Translator

cache_path = "translate_cache.pcl"
cached_translator = cw.CacheWrapper(original_translator)

if os.path.isfile(cache_path):
    cached_translator.load_cache(cache_path)

# not shown in this example
untranslated_classes = do_some_ontology_stuff()

res_list = []

for c in tqdm(untranslated_classes[:]):
    original = c.label.ru[0]
    translation = cached_translator.translate(original)

    if "MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY" in translation:
        cached_translator._remove_last_key()
        break

    record = {
        c.name: {
            "ru": f"{original}",
            "en": f"{translation}",
        }
     }

    res_list.append(record)

cached_translator.save_cache(cache_path)