Package: memoclass
IntroductionMemoizationis a common technique used primarily as an optimizer, allowing the
caching of the return values of expensive functions. When a memoized function is
called with a given set of arguments for the first time its return value is
cached, and that value is then returned on all subsequent calls.While many memoization packages exist, as well as instructions to write your own
simple decorator, this package provides enhanced utility for memoizing methods
onclasses. Especially, this allows for a couple of ways of automatically
clearing the caches related to a particular instance, for example, when changing
a member variable would change the result of those functions. This package also
usesinspect.signatureto correctly treat default argument values, where
possible.Preliminary sphinx documentation can be found athttps://jon-burr.github.io/memoclass/memoclass.htmlThe project is hosted on github athttps://github.com/Jon-Burr/memoclassInstallationpipinstallmemoclassOverview of core componentsmemoclass.memoize.memofuncMemoizes a single free function. The returned object is aMemoFuncobject,
which has functions that allow you to temporarily disable the caching, or clear
it entirely.>>>frommemoclass.memoizeimportmemofunc>>>>>>@memofunc>>>defbuild_list(x):>>>return[1,2,3,x]>>>>>>a=build_list(5)>>>b=build_list(5)>>>aisbTrue>>>build_list.clear_cache()>>>c=build_list(5)>>>aiscFalseTheMemoFuncclass also has some extra properties that can be set while
decorating>>>frommemoclass.memoizeimportmemofunc>>>importcopy>>>>>>@memofunc(on_return=copy.copy)>>>defbuild_list(x):>>>return[1,2,3,x]>>>>>>a=build_list(5)>>>b=build_list(5)>>>aisbFalsememoclass.memoize.memomethodMemoizes a class method. Methods bound to different instances have independent
caches, so the cache on one object can be cleared without clearing it for all
other objects.>>>frommemoclass.memoizeimportmemomethod>>>>>>classListBuilder(object):>>>@memomethod>>>def__call__(self,x):>>>return[1,2,3,x]>>>>>>x=ListBuilder()>>>y=ListBuilder()>>>a=x()>>>b=y()>>>aisbFalse>>>x.__call__.clear_cache()>>>c=y()>>>bisc# Clearing x's cache has not touched y'sTruememoclass.memoclass.MemoClassBase class meant to make interacting with memoized methods easier. It can
enabled, disable and clear all memomethods attached to an instance (note that
which methods exist is calculated at theclasslevel, so any added onto an
instance will not be seen).By default, setting any attribute will reset the object’s caches, unless that
attribute has been provided to themutable_attrsargument ofMemoClass.__init__. This behaviour can be disabled by settingmutable_attrs=None. Additionally, any function can have thememoclass.memoclass.mutatesdecorator applied to it, which will then reset
the caches whenever it is called.>>>frommemoclass.memoizeimportmemomethod>>>frommemoclass.memoclassimportMemoClass>>>>>>classPartialSum(MemoClass):>>>>>>def__init__(self,stored):>>>super().__init__()>>>self.stored=stored>>>>>>@memomethod>>>def__call__(self,other):>>>returnself.stored+other>>>>>>a=PartialSum(5)>>>a(3)8>>>a.stored=3# Triggers a cache reset>>>a(3)6AMemoClasscan belockedwhich means that all caches are enabled and
calling a function markedmutatesor setting a non-mutable attribute results
in aValueError. When the class is then unlocked again, if the caches
were previously disabled, they will be disabled and cleared. This means it is
possible to create a class whose methods are only temporarily memoized. This
might be useful if a class has expensive methods to calculate that rely on a
global state. Note that by default, amemomethoddeclared on aMemoClasswill lock its caller while it is called.