votesim.utilities.decorators¶
Collection of utilities such as memoization, automatic property storage, etc
Contents
Class Summary¶
|
Decorator used to store past calls. |
|
cache the return value of a method |
Function Summary¶
|
Clean all lazy properties |
|
Clean properties in iterable names |
|
Version of lazy_property by John Huang. |
|
Version of lazy_property by John Huang. |
|
Modify a lazy property |
|
Reuse the docstring from f on the decorated function |
Module Classes¶
memoize¶
-
class
votesim.utilities.decorators.
memoize
(function)¶ Decorator used to store past calls.
Method/Attribute Summary¶
method_memoize¶
-
class
votesim.utilities.decorators.
method_memoize
(func)¶ cache the return value of a method
This class is meant to be used as a decorator of methods. The return value from a given method invocation will be cached on the instance whose method was invoked. All arguments passed to a method decorated with memoize must be hashable.
If a memoized method is invoked directly on its class the result will not be cached. Instead the method will be invoked like a static method: class Obj(object):
@memoize def add_to(self, arg):
return self + arg
Obj.add_to(1) # not enough arguments Obj.add_to(1, 2) # returns 3, result is not cached
Method/Attribute Summary¶
Module Functions¶
clean_lazy_properties¶
-
votesim.utilities.decorators.
clean_lazy_properties
(instance)¶ Clean all lazy properties
clean_some_lazy_properties¶
-
votesim.utilities.decorators.
clean_some_lazy_properties
(instance, names)¶ Clean properties in iterable names
lazy_property¶
-
votesim.utilities.decorators.
lazy_property
(fn)¶ Version of lazy_property by John Huang.
Decorator used to cache property results into dictionary. The cache can be clered using clean_lazy_properties.
lazy_property2¶
-
votesim.utilities.decorators.
lazy_property2
(name='_cache_properties')¶ Version of lazy_property by John Huang.
Decorator used to cache property results into dictionary. The cache can be clered using clean_lazy_properties.
Decorator must be called as a function.
- Parameters
name (str) – Name of cache dictionary
Example
Set the lazy property
>>> class class1(object): >>> @lazy_property2('my_cache') >>> def property(self): >>> x = 2.0 >>> return x
Delete the lazy property >>> a = class1() >>> del a.my_cache
modify_lazy_property¶
-
votesim.utilities.decorators.
modify_lazy_property
(instance, name, value)¶ Modify a lazy property
reuse_doc¶
-
votesim.utilities.decorators.
reuse_doc
(f)¶ Reuse the docstring from f on the decorated function
- Parameters
f (func or class) – Desired func/class whose __doc__ you want to reuse
- Returns
out
- Return type
decorator
Example
Here we decorate class B with class A’s docstring
>>> class A(object): >>> '''I got A docstring''' >>> def __init__(self): >>> self.x = 10
>>> @reuse_doc(A) >>> class B(A): >>> pass
>>> B.__doc__ == 'I got A docstring'