memoclass package

Submodules

memoclass.memoclass module

class memoclass.memoclass.MemoClass(mutable_attrs=())

Bases: object

A class with several utilities to enable interacting with memoized methods

A MemoClass’ methods can be declared mutating, in which case they will automatically clear the caches on any memomethods present on it.

Alternatively, a MemoClass can be locked, which prevents calling any mutating methods. By default, locking enables caching and unlocking disables and clears the caches if they were disabled before, which allows it to temporarily create a ‘const’ object. This is useful when an object has time consuming attributes to calculate but may change in ways that are difficult to reset the caches on (for example, if the class calculates values based on another object that it doesn’t own or control).

clear_caches(clsmethods=False)

Clear the cache on all memomethods

disable_caches(clsmethods=False)

Disable the cache on all memomethods

enable_caches(clsmethods=False)

Enable the cache on all memomethods

property is_locked

Is this class locked

lock()

Lock the class

A locked class’ caches are always enabled and calling a mutating method on it results in a ValueError

locked(clear_on_unlock=None)

A context manager that temporarily locks the class

Does nothing if the class is already locked

Parameters

clear_on_unlock – If True, disable the class’ caches and clear them when unlocking. The caches will only be cleared if the class was unlocked before calling locked

mutate(_stack=None)

Signal that something has changed in this object

Raises a ValueError if this object is locked

Clears the caches on this object, then calls ‘mutate’ on anything returned by self.mutates_with_this()

mutates_with_this()

Return an iterable of objects whose mutate method should be called when this one is

unlock(clear_caches)

Unlock the class

Parameters

clear_caches – If True, disable the class’ caches and clear them

unlocked(clear_caches=True)

A context manager that temporarily unlocks the class

Does nothing if the class is already unlocked

Parameters

clear_caches – If True, clear and disable the caches when unlocking

memoclass.memoclass.mutates(func)

Signal that a method mutates its class

Here, mutates means that it clears the caches on any memomethods on the class calling the method

memoclass.memoize module

Basic decoarators for memoizing functions and methods

class memoclass.memoize.LockMemoFunc(func, clear_on_unlock, **kwargs)

Bases: memoclass.memoize.MemoFunc

Used to memoize a bound function on a lockable class

The bound function will lock its class before being called

class memoclass.memoize.MemoClsMethod(func, cache_cls=<class 'dict'>, on_return=<function MemoMethod.<lambda>>, prehash=<function _to_hashable>, locks=True, clear_on_unlock=None)

Bases: memoclass.memoize.MemoMethod

Memoize a classmethod

Memomethod/memofunc do not interact nicely with the classmethod decorator, so this should be used instead where a classmethod should be memoized

class memoclass.memoize.MemoFunc(func, cache=None, on_return=<function MemoFunc.<lambda>>, prehash=<function _to_hashable>)

Bases: object

Memoizes a free function

property cache_enabled
clear_cache()

Clear the cache

disable_cache()
enable_cache()
rm_from_cache(*args, **kwargs)

Remove the corresponding value from the cache

class memoclass.memoize.MemoMethod(func, cache_cls=<class 'dict'>, on_return=<function MemoMethod.<lambda>>, prehash=<function _to_hashable>, locks=True, clear_on_unlock=None)

Bases: object

Memoizes a class’ method

clear_cache(bound=None)

Clear the cache

Parameters

bound – If not None, clear only the cache corresponding to that object, if bound is None, remove all caches

memoclass.memoize.bind_callargs(sig, *args, **kwargs)

Convert a set of args and kwargs into a dictionary of function arguments including defaults

This is different to the result of signature.bind(*args, **kwargs).arguments as it includes defaults. It’s different to inspect.getcallargs as it works properly on callables. It’s equivalent to

>>> b = sig.bind(*args, **kwargs)
>>> b.apply_defaults()
>>> return dict(b.arguments)

but the apply_defaults function is python3 only and not included in funcsigs

memoclass.memoize.make_decorator(decorator)
memoclass.memoize.memoclsmethod(func=None, **kwargs)
memoclass.memoize.memofunc(func=None, **kwargs)
memoclass.memoize.memomethod(func=None, **kwargs)

Module contents

A utility package for memoizing functions and class methods