3.1.1. horton.cache – Avoid recomputation of earlier results and reallocation of existing arrays

In principle, the JustOnceClass and the Cache can be used independently, but in some cases it makes a lot of sense to combine them. See for example the density partitioning code in horton.part.

class horton.cache.JustOnceClass

Bases: object

Base class for classes with methods that should never be executed twice.

In typically applications, these methods get called many times, but only during the first call, an actual computation is carried out. This way, the caller can safely call a method, just to make sure that a required result is computed.

All methods in the subclasses that should have this feature, must be given the just_once decoratore, e.g.

class Example(JustOnceClass):
    @just_once
    def do_something():
        self.foo = self.bar

When all results are outdated, one can call the clear method to forget which methods were called already.

clear()
class horton.cache.Cache

Bases: object

Object that stores previously computed results.

The cache behaves like a dictionary with some extra features that can be used to avoid recomputation or reallocation.

clear(**kwargs)

Clear all items in the cache

Optional arguments:

dealloc
When set to True, the items are really removed from memory.
tags
Limit the items cleared to those who have at least one tag that matches one of the given tags. When this argument is used and it contains at least one tag, items with no tags are not cleared.
clear_item(*key, **kwargs)

Clear a selected item from the cache

Optional arguments:

dealloc
When set to True, the item is really removed from memory.
dump(*args, **kwargs)

Store an object in the cache.

Arguments:

key1 [, key2, ...]
The positional arguments (except for the last) are used as a key for the object.
value
The object to be stored.

Optional argument:

tags
Tags to be associated with the object
iteritems(tags=None)

Iterate over all valid items in the cache.

iterkeys(tags=None)

Iterate over the keys of all valid items in the cache.

itervalues(tags=None)

Iterate over the values of all valid items in the cache.

load(*key, **kwargs)

Get a value from the cache

Arguments:

key0 [key1 ...]
All positional arguments are used as keys to identify the cached value.

Optional arguments:

alloc
Parameters used to allocate a cached value if it is not present yet. This argument can take two forms. When integer or a tuple of integers is given, an array is allocated. Alternatively, a tuple may be given whose first element is a constructor, and further elements are arguments for that constructor.
default
A default value that is returned when the key does not exist in the cache. This default value is not stored in the cache.
tags
When alloc is used and a new object is thereby created or reused, it will get these tags. This argument is only allowed if the alloc argument is present. In case no new object is allocated, the given tags must match those already present.

The optional argument alloc and default are both meant to handle situations when the key has not associated value. Hence they can not be both present.

horton.cache.just_once(fn)