core.cache module

Provides caching functionality.

core.cache.cached_property(fget=None, *args, **kwargs)[source]

Decorator used to register or wrap a cached property.

core.cache.cached_result(fget)[source]

Decorator used to register a cached method.

Parameters:fget (function) – Method that is only called once and its result cached for subsequent calls.
Return type:CachedProperty
core.cache.CachedProperty
class _core._cache.CachedProperty

Bases: Boost.Python.instance

__init__((object)self[, (object)fget=None[, (object)fset=None[, (object)fdel=None[, (object)doc=None[, (tuple)args=()[, (object)kwargs=None]]]]]]) → None :

Represents a property attribute that is only computed once and cached.

Parameters:
  • fget (function) –

    Optional getter function. Once the value has been computed, the result is cached and returned if this property is requested again.

    Getter signature: self, *args, **kwargs

  • fset (function) –

    Optional setter function. When a new value is assigned to this property, that function is called and the cache is invalidated if nothing was returned otherwise the cached value is updated accordingly.

    Setter signature: self, value, *args, **kwargs

  • fdel (function) –

    Optional deleter function. When this property is deleted, that function is called and the cached value (if any) is invalidated.

    Deleter signature: self, *args, **kwargs

  • doc (str) – Documentation string for this property.
  • args (tuple) – Extra arguments passed to the getter, setter and deleter functions.
  • kwargs (dict) – Extra keyword arguments passed to the getter, setter and deleter functions.
Raises:

TypeError – If the given getter, setter or deleter is not callable.

Note

Generator values cannot be cached.

Example:

from random import randint
from core.cache import cached_property

class Test:
    @cached_property(kwargs=dict(range=(0, 1000)))
    def test(self, range):
        return randint(*range)

    @test.setter
    def set_test(self, value, range):
        return int(value / 2)

test = Test()

# Compute and cache the value for the first time
i = test.test

# The first computed value was cached, so it should always be the same
assert i is test.test
assert i is test.test
assert i is test.test
assert i is test.test

# Deleting the property is invalidating the cache
del test.test
assert i is not test.test

# The cache will be updated to 5, because our setter computes value / 2
test.test = 10
assert test.test is 5

# The new value should be 1, because we updated our userdata
Test.test['range'] = (1, 1)
del test.test
assert test.test is 1
bind((object)self, (object)owner, (str)name) → object :

Binds this property to the given class as the given name.

Parameters:
  • owner (class) – The class the property should be bound to.
  • name (str) – The name of this property.
Return type:

CachedProperty

delete_cached_value((CachedProperty)self, (object)instance) → None :

Deletes the cached value for the given instance.

Parameters:instance (object) – The instance to delete the cached value for.
deleter((CachedProperty)self, (object)fdel) → object :

Decorator used to register the deleter function for this property.

Parameters:fdel (function) – The function to register as deleter function.
Return type:function
get_cached_value((CachedProperty)self, (object)instance) → object :

Returns the cached value for the given instance.

Parameters:instance (object) – The instance to get the cached value for.
Raises:KeyError – If the given instance didn’t have a cached value.
Return type:object
getter((CachedProperty)self, (object)fget) → object :

Decorator used to register the getter function for this property.

Parameters:fget (function) – The function to register as getter function.
Return type:function
set_cached_value((CachedProperty)self, (object)instance, (object)value) → None :

Sets the cached value for the given instance.

Parameters:
  • instance (object) – The instance to set the cached value for.
  • value (object) – The value to set as cached value.
setter((CachedProperty)self, (object)fset) → object :

Decorator used to register the setter function for this property.

Parameters:fset (function) – The function to register as setter function.
Return type:function
static wrap_descriptor((object)arg1[, (object)owner=None[, (str)name=''[, (tuple)args=()[, (object)kwargs=None]]]]) → CachedProperty :

Wraps a descriptor as a cached property.

Parameters:
  • descriptor (property) – Property descriptor to wrap. Must have a __get__, __set__ and a __delete__ methods bound to it, either callable or set to None.
  • owner (class) – The class the wrapped property should be bound to.
  • name (str) – The name of this property.
  • args (tuple) – Extra arguments passed to the getter, setter and deleter functions.
  • kwargs (dict) – Extra keyword arguments passed to the getter, setter and deleter functions.
Raises:
  • AttributeError – If the given descriptor doesn’t have the required methods.
  • TypeError – If the getter, setter or deleter are not callable.
args

The extra arguments passed to the getter, setter and deleter functions.

Return type:tuple
fdel

The deleter function used to delete this property.

Return type:function
fget

The getter function used to compute the value of this property.

Return type:function
fset

The setter function used to assign the value of this property.

Return type:function
kwargs

The extra keyword arguments passed to the getter, setter and deleter functions.

Return type:dict
name

The name this property is registered as.

Return type:str
owner

The owner class this property attribute was bound to.

Return type:type