LazyList

class menpo.base.LazyList(callables)[source]

Bases: Sequence

An immutable sequence that provides the ability to lazily access objects. In truth, this sequence simply wraps a list of callables which are then indexed and invoked. However, if the callable represents a function that lazily access memory, then this list simply implements a lazy list paradigm.

When slicing, another LazyList is returned, containing the subset of callables.

Parameters:callables (list of callable) – A list of callable objects that will be invoked if directly indexed.
count(value) → integer -- return number of occurrences of value
index(value) → integer -- return first index of value.

Raises ValueError if the value is not present.

classmethod init_from_index_callable(f, n_elements)[source]

Create a lazy list from a callable that expects a single parameter, the index into an underlying sequence. This allows for simply creating a LazyList from a callable that likely wraps another list in a closure.

Parameters:
  • f (callable) – Callable expecting a single integer parameter, index. This is an index into (presumably) an underlying sequence.
  • n_elements (int) – The number of elements in the underlying sequence.
Returns:

lazy (LazyList) – A LazyList where each element returns the underlying indexable object wrapped by f.

map(f)[source]

Create a new LazyList where the passed callable f wraps each element.

f should take a single parameter, x, that is the result of the underlying callable - it must also return a value. Note that mapping is lazy and thus calling this function should return immediately.

Parameters:f (callable) – Callable to wrap each element with.
Returns:lazy (LazyList) – A new LazyList where each element is wrapped by f.