fsl.utils.memoize¶
This module provides a handful of decorators which may be used to memoize a function:
memoizeMemoize the given function by the value of the input arguments. MemoizeDecorator which can be used to memoize a function or method. InstanceifyThis class is intended to be used to decorate other decorators, so they can be applied to instance methods. memoizeMD5Memoize the given function. skipUnchangedThis decorator is intended for use with setter functions - a function which accepts a name and a value, and is intended to set some named attribute to the given value.
-
fsl.utils.memoize.memoize(func=None)¶ Memoize the given function by the value of the input arguments.
This function simply returns a
Memoizeinstance.
-
class
fsl.utils.memoize.Memoize(*args, **kwargs)¶ Bases:
objectDecorator which can be used to memoize a function or method. Use like so:
@memoize def myfunc(*a, **kwa): ... @memoize() def otherfunc(*a, **kwax): ...
A
Memoizeinstance maintains a cache which contains{args : value}mappings, whereargsare the input arguments to the function, andvalueis the value that the function returned for those arguments. When a memoized function is called with arguments that are present in the cache, the cached values are returned, and the function itself is not called.The
invalidate()method may be used to clear the internal cache.Note that the arguments used for memoization must be hashable, as they are used as keys in a dictionary.
-
invalidate(*args, **kwargs)¶ Clears the internal cache. If no arguments are given, the entire cache is cleared. Otherwise, only the cached value for the provided arguments is cleared.
-
-
fsl.utils.memoize.memoizeMD5(func)¶ Memoize the given function. Whenever the function is called, an md5 digest of its arguments is calculated - if the digest has been previously cached, the previous value calculated by the function is returned.
-
fsl.utils.memoize.skipUnchanged(func)¶ This decorator is intended for use with setter functions - a function which accepts a name and a value, and is intended to set some named attribute to the given value.
This decorator keeps a cache of name-value pairs. When the decorator is called with a specific name and value, the cache is checked and, if the given value is the same as the cached value, the decorated function is not called. If the given value is different from the cached value (or there is no value), the decorated function is called.
The
invalidatemethod may be called on askipUnchanged-decorated function to clear the internal cache. For example:@skipUnchanged def setval(name, value): # ... # ... setval.invalidate()
Note
This decorator ignores the return value of the decorated function.
Returns: Trueif the underlying setter function was called,Falseotherwise.
-
class
fsl.utils.memoize.Instanceify(realDecorator)¶ Bases:
objectThis class is intended to be used to decorate other decorators, so they can be applied to instance methods. For example, say we have the following class:
class Container(object): def __init__(self): self.__items = {} @skipUnchanged def set(self, name, value): self.__items[name] = value
Given this definition, a single
skipUnchanged()decorator will be created and shared amongst allContainerinstances. This is not ideal, as the value cache created by theskipUnchanged()decorator should be associated with a singleContainerinstance.By redefining the
Containerclass definition like so:class Container(object): def __init__(self): self.__items = {} @Instanceify(skipUnchanged) def set(self, name, value): self.__items[name] = value
a separate
skipUnchanged()decorator is created for, and associated with, everyContainerinstance.This is achieved because an
Instanceifyinstance is a descriptor. When first accessed as an instance attribute, anInstanceifyinstance will create the real decorator function, and replace itself on the instance.