picos.caching¶
Caching helpers.
Outline¶
Classes¶
A read-only property whose result is cached. |
|
A read-only, self-inverse property whose result is cached. |
Functions¶
Copy cached values from one object to another. |
|
Make a self-inverse unary operator method cache its result. |
|
Make a unary operator method cache its result. |
|
Clear all cached values of an object. |
|
Unlock the setters of cached instance attributes. |
Classes¶
cached_property¶
cached_selfinverse_property¶
-
class
picos.caching.
cached_selfinverse_property
(fget=None, fset=None, fdel=None, doc=None)[source]¶ Bases:
picos.caching.cached_property
A read-only, self-inverse property whose result is cached.
Functions¶
borrow_cache¶
cached_selfinverse_unary_operator¶
-
picos.caching.
cached_selfinverse_unary_operator
(operator)[source]¶ Make a self-inverse unary operator method cache its result.
This is supposed to be used for property-like special methods such as
__neg__
wherecached_property
can’t be used.Warning
The result returned by the wrapped operator must be a fresh object as it will be modified.
cached_unary_operator¶
-
picos.caching.
cached_unary_operator
(operator)[source]¶ Make a unary operator method cache its result.
This is supposed to be used for property-like special methods such as
__neg__
wherecached_property
can’t be used.
unlocked_cached_properties¶
-
picos.caching.
unlocked_cached_properties
(obj)[source]¶ Unlock the setters of cached instance attributes.
Normally, cached attributes are read-only properties. When the user first reads them, the cache is populated with the value returned to the user, and successive reads will return the cached value.
The user is allowed to empty the cache by using
del
on the variable, but they may not assign a value to it. This context allows the programmer to manually populate the cache by assigning a value to the property.- Example
>>> from picos.caching import cached_property, unlocked_cached_properties >>> class A: ... @cached_property ... def p(self): ... return 1 ... >>> a = A() >>> try: ... a.p = 2 ... except AttributeError: ... print("Not possible.") ... Not possible. >>> with unlocked_cached_properties(a): ... a.p = 2 # Populate the cache of a.p. ... >>> a.p 2