memoize_generator

Module: tools.memoize_generator

This module contains a class that provides memoization support for generator. Most likely there would be cleaner and more general ways of doing this but for the moment is suffices...

Meaning of this method: https://secure.wikimedia.org/wikipedia/en/wiki/Memoization

Author:Jan Hendrik Metzen (jhm@informatik.uni-bremen.de)
Created:2008/11/25

Inheritance diagram for pySPACE.tools.memoize_generator:

Inheritance diagram of pySPACE.tools.memoize_generator

Class Summary

MemoizeGeneratorNotRefreshableException
MemoizeGenerator(generator[, caching]) Object to encapsulate a generator so that one iterate over the generators output several time.

Classes

MemoizeGeneratorNotRefreshableException

class pySPACE.tools.memoize_generator.MemoizeGeneratorNotRefreshableException[source]

Bases: exceptions.Exception

__weakref__

list of weak references to the object (if defined)

MemoizeGenerator

class pySPACE.tools.memoize_generator.MemoizeGenerator(generator, caching=False)[source]

Bases: object

Object to encapsulate a generator so that one iterate over the generators output several time. The output is computed only once and then stored in a cache. Be careful in cases where the generator might produce memory-intensive outputs!

Class Components Summary

_fetch_from_generator() Fetches one fresh value from the generator, store it in the
fresh() Return one generator that yields the same values like the internal one that was passed to __init__.
__init__(generator, caching=False)[source]

Stores the generator and creates an empty cache

Note

Since the output of the generator is ordered, the cache is an ordered sequence of variable length like a list

_fetch_from_generator()[source]

Fetches one fresh value from the generator, store it in the cache and yield it

fresh()[source]

Return one generator that yields the same values like the internal one that was passed to __init__.

Note

It does not recompute values that have already been requested before but just uses these from the internal cache.

Note

Calling fresh invalidates all existing generators that have been created before using this method, i.e. there can only be one generator at a time

__weakref__

list of weak references to the object (if defined)