Package gavo :: Package utils :: Module codetricks :: Class CachedGetter
[frames] | no frames]

Class CachedGetter

source code

object --+
         |
        CachedGetter

A cache for a callable.

This is basically memoization, except that these are supposed to be singletons; CachedGetters should be used where the construction of a resource (e.g., a grammar) should be deferred until it is actually needed to save on startup times.

The resource is created on the first call, all further calls just return references to the original object.

You can also leave out the getter argument and add an argumentless method impl computing the value to cache.

Using a CachedGetter also serializes generation, so you can also use it when getter isn't thread-safe.

At construction, you can pass a f(thing) -> bool in an isAlive keyword argument. If you do, the function will be called with the cache before the cache is being returned. If it returns false, the resource is re-made (no concurrency control is enforced here).

Instance Methods
 
__init__(self, getter, *args, **kwargs)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
__call__(self) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, getter, *args, **kwargs)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)