Package: charmonium-cache
Provides a decorator for caching a function between subsequent processes.Whenever the function is called with the same arguments, the result is
loaded from the cache instead of computed. This cache ispersistent
across runs. If the arguments, source code, or enclosing environment
have changed, the cache recomputes the data transparently (no need
for manual invalidation).The use case is meant for iterative development, especially on scientific
experiments. Many times a developer will tweak some of the code but not
all. Often, reusing intermediate results saves a significant amount of time
every run.See full documentationhere.QuickstartIf you don’t havepipinstalled, see thepip install
guide.$pipinstallcharmonium.cache>>> from charmonium.cache import memoize
>>> i = 0
>>> @memoize()
... def square(x):
...     print("recomputing")
...     # Imagine a more expensive computation here.
...     return x**2 + i
...
>>> square(4)
recomputing
16
>>> square(4) # no need to recompute
16
>>> i = 1
>>> square(4) # global i changed; must recompute
recomputing
17AdvantagesWhile there are other libraries and techniques for memoization, I believe this
one is unique because it is:Correct with respect to source-code changes:The cache detects if you
edit the source code or change a file which the program reads (provided they
use this library’s right file abstraction). Users never need to manually
invalidate the cache, so long as the functions are pure (unlikejoblib.Memory,Klepto).It is precise enough that it will ignore changes in unrelated functions in
the file, but it will detect changes in relevant functions in other files. It
even detects changes in global variables (as in the example above). SeeDetecting Changes in Functionsfor details.Useful between runs and across machines:The cache can persist on the
disk (unlikefunctools.lru_cache). Moreover, a cache can be shared on the
network, so that ifanymachine has computed the function for the same
source-source and arguments, this value can be reused byany othermachine,
provided your datatype is de/serializable on those platforms.Easy to adopt:Only requires adding one line (decorator) to
the function definition.Bounded in size:The cache won’t take up too much space. This
space is partitioned across all memoized functions according to the
heuristic.Supports smart heuristics:By default, the library uses state-of-the-art
cache policies that can take into account time-to-recompute and storage-size
in addition to recency, more advanced than simpleLRU.Overhead aware:The library measures the time saved versus overhead. It
warns the user if the overhead of caching is not worth it.Memoize CLIMake is good for compiling code, but falls short for data science. To get
correct results, you have to incorporateeveryvariable your result depends on
into a file or part of the filename. If you put it in a file, you only have one
version cached at a time; if you put it in the filename, you have to squeeze the
variable into a short string. In either case, stale results will accumulate
unboundedly, until you runmake cleanwhich also purges the fresh
results. Finally, it is a significant effor to rewrite shell scripts in make.memoizemakes it easy to memoize steps in shell scripts, correctly. Just addmemoizeto the start of the line. If the command, its arguments,
or its input files change, thencommand arg1 arg2 ...will be
rerun. Otherwise, the output files (including stderr and stdout) will be
produced from a prior run.memoizeuses ptrace to automatically determine
what inputs you depend on and what outputs you produce.memoize command arg1 arg2
# or
memoize --key=$(date +%Y-%m-%d) -- command arg1 arg2SeeCLIfor more details.