GitHub - brmscheiner/memorize.py: A basic decorator for caching a function's results in local storage. (original) (raw)

Memorize.py

Memorize.py is a Python decorator for caching a function's results in local storage.

What's the point?

In many situations, recycling the output of a function is more efficient than running the function multiple times. A typical memoizing decorator does exactly that for as long as a program is running (the output is stored in Python variable space). Memorize.py stores the output as a .cache file in the current (or target file's) directory for reuse in future program executions. If the Python file containing the decorated function has changed since the last run, the current cache is deleted and a new cache is created (in case the behavior of the function has changed).

Danger zone!

BEWARE: only pure functions should be memoized! Otherwise you might encounter unexpected results.

Unfinished business

Don't use this for functions whose arguments can't be dictionary keys (such as lists). I'd like to add support for this in the future.

Install

Usage

from memorize import Memorize

@Memorize
def yourFunction(x, y, z):
  # do great things...