musif.cache.cache module

class musif.cache.cache.CallableArguments(*args, **kwargs)[source]

Bases: object

This class represents a set of ordered arguments. The hash is the concatenation of the argument hashes. If the arguments are SmartModuleCache objects, this object uses their cache (which persists to disk), otherwise it uses deepdiff.DeepHash to compute a hash based on the content value (which should persists across pickling).

Note that DeepHash may be much slower than the builtin hash() function, so for large and complex objects like music21.Score, just use a SmartModuleCache wrapper.

class musif.cache.cache.MethodCache(reference: ObjectReference, attr_name: str, target_addresses: List[str] = ['music21'], special_method: bool = False, check_reference_changes: bool = False)[source]

Bases: object

A simple wrapper that checks if the arguments are in the cache, otherwise runs the method and caches the arguments

cache
check_reference_changes
name
reference
smartforcecache(*args, **kwargs)[source]
special_method
target_addresses
class musif.cache.cache.ObjectReference(reference: Any, resurrect_reference: Optional[Tuple], parent=None, name: str = '', args: Optional[Tuple] = None)[source]

Bases: object

This handles the calls to the reference object so that both MethodCache and SmartModuleCache reference the same object and when one resurrects it, the other sees it.

args
get_attr(name: str) Any[source]
name
parent
reference
resurrect_reference
class musif.cache.cache.SmartModuleCache(reference: Optional[Any] = None, resurrect_reference: Optional[Tuple] = None, parent: Optional[ObjectReference] = None, name: Tuple[str] = ('',), args: Tuple[Optional[Tuple]] = (None,), target_addresses: List[str] = ['music21'], check_reference_changes: bool = False)[source]

Bases: object

This class wraps any object so that its function calls can be cached in a dict, very similarly to how functools.lru_cache works.

The object keeps a dictionary and a reference to the wrapped object. Each time a method, a field, or a property is called, it stores the result value in the dictionary. When the same call is performed a second time, the result value is taken from the dictionary.

If the object is changed, the change is not cached. As such, only read operations can be executed on a SmartModuleCache.

After unpickling, the referenced object is no longer available. If resurrect_reference is not None, it will be loaded using its value. In such a case, resurrect_reference should be a tuple with a function in the first position and the arguments later; the function returned value must be the reference object.

If the method/property/field returns an object defined in a one of the target_addresses, this class will return a wrapped version of that object. In musiF, this is used to wrap all the music21 objects and to cache (most of) music21 operations.

When a method is called, it is matched with all the arguments, similarly to functools.lru_cache, thus using the hash value of the objects.

When pickled, this object only pickles the cache dictionary. The matching of the arguments in a method works on a custom hash value that is guaranteed to be the same when the object is pickled/unpickled. As consequence, as long as the method arguments are SmartModuleCache objects, they will re-use the cache after pickling/unpickling.

Sometimes, the methods of the referenced object expect a non-cached module, for instance when a type checking or a hash comparison is done. For those situations, SmartModuleCache provides special methods that start with SmartModuleCache.SPECIAL_METHODS_NAME (which defaults to “smartcache__”). It is possible call any method with smartcache__methodname, e.g. score.smartcache__remove. When a special method is called, the first call is performed with the non-cached arguments, as it would happen without SmartModuleCache.

NotImplemented: When smart_pickling is True, it pickles the dictionary alone, without the reference, otherwise, it pickles the reference as well.

Note: this class implements __repr__ in a custom way, while proxies the __str__ method to the referenced object.

Note: In future, deepdiff.Delta could be used to allow in-place operations, but it needs that:

  1. the object modified are pickable

  2. the reference are deep-copiable (for some reason, music21 objects)

    sometimes exceed the maximum number of recursion while deep-copying

SPECIAL_METHODS_NAME = 'smartcache__'
cache
ischanged()[source]

Returns if the reference object is changed using value comparison of its attributes recursively.

smartforcecache(name, *args, **kwargs)[source]

This method can be used to forcedly cache some attribute of the reference object without using it. name is the name of the attribute. If it is a method, *args and **kwargs.

Return (True, None) if object is cached, (False, Exception) otherwise

property target_addresses