memorize.py is a basic decorator for caching a function's results in local storage. Memorize is a play on memoize, which means to cache a function.
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).
BEWARE: only pure functions should be memoized! Otherwise you might encounter unexpected results.
- Does your function have any external effects? (writing to a file, printing to the console, changing a global variable...)
- Does the function depend on anything outside of the current file that may have changed (external classes, methods, functions, or data)?
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.
pip install git+https://github.com/brmscheiner/memorize.py.git#egg=memorize