UtilCache Module

class poisson_approval.utils.UtilCache.DeleteCacheMixin[source]

Mixin used to delete cached properties.

Notes

Cf. decorator cached_property().

Examples

>>> class Example(DeleteCacheMixin):
...     @cached_property
...     def x(self):
...         print('Big computation...')
...         return 6 * 7
>>> a = Example()
>>> a.x
Big computation...
42
>>> a.x
42
>>> a.delete_cache()
>>> a.x
Big computation...
42
poisson_approval.utils.UtilCache.cached_property(f)[source]

Decorator used in replacement of @property to put the value in cache automatically.

Notes

The first time the attribute is used, it is computed on-demand and put in cache. Later accesses to the attributes will use the cached value.

Cf. DeleteCacheMixin for an example.

poisson_approval.utils.UtilCache.property_deleting_cache(hidden_variable_name, doc='')[source]

Define a property that deletes the cache when set or deleted.

Parameters
  • hidden_variable_name (str) – The name of the hidden variable used to store the value of the property.

  • doc (str) – The docstring of the property.

Notes

The class must inherit from DeleteCacheMixin.

Examples

>>> class MyClass(DeleteCacheMixin):
...     def __init__(self, some_parameter):
...         self.some_parameter = some_parameter
...     some_parameter = property_deleting_cache('_some_parameter')
...     @cached_property
...     def my_cached_property(self):
...         print('Computing my_cached_property...')
...         return 'Hello %s!' % self.some_parameter
>>> my_object = MyClass(some_parameter='World')
>>> my_object.my_cached_property
Computing my_cached_property...
'Hello World!'
>>> my_object.my_cached_property
'Hello World!'
>>> my_object.some_parameter = 'everybody'
>>> my_object.my_cached_property
Computing my_cached_property...
'Hello everybody!'