Skip to content

A basic decorator for caching a function's results in local storage.

Notifications You must be signed in to change notification settings

JoranDox/memorize.py

 
 

Repository files navigation

memorize.py

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.

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.

  • 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)?

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

pip install git+https://github.com/brmscheiner/memorize.py.git#egg=memorize

About

A basic decorator for caching a function's results in local storage.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%