LazyList¶
-
class
menpo.base.
LazyList
(callables)[source]¶ Bases:
Sequence
,Copyable
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. -
copy
()[source]¶ Generate an efficient copy of this LazyList - copying the underlying callables will be lazy and shallow (each callable will not be called nor copied) but they will reside within in a new list.
Returns: type(self)
– A copy of this LazyList.
-
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
.
-
classmethod
init_from_iterable
(iterable, f=None)[source]¶ Create a lazy list from an existing iterable (think Python list) and optionally a callable that expects a single parameter which will be applied to each element of the list. This allows for simply creating a LazyList from an existing list and if no callable is provided the identity function is assumed.
Parameters: - iterable (collections.Iterable) – An iterable object such as a list.
- f (callable, optional) – Callable expecting a single parameter.
Returns: lazy (LazyList) – A LazyList where each element returns each item of the provided iterable, optionally with f applied to it.
-
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.Alternatively,
f
may be a list of callable, one per entry in the underlying list, with the same specification as above.Parameters: f (callable or iterable of callable) – Callable to wrap each element with. If an iterable of callables (think list) is passed then it must by the same length as this LazyList. Returns: lazy (LazyList) – A new LazyList where each element is wrapped by (each) f
.
-
repeat
(n)[source]¶ Repeat each item of the underlying LazyList
n
times. Therefore, if a list currently hasD
items, the returned list will containD * n
items and will return immediately (method is lazy).Parameters: n (int) – The number of times to repeat each item. Returns: lazy (LazyList) – A LazyList where each element returns each item of the provided iterable, optionally with f applied to it. Examples
>>> from menpo.base import LazyList >>> ll = LazyList.init_from_list([0, 1]) >>> repeated_ll = ll.repeat(2) # Returns immediately >>> items = list(repeated_ll) # [0, 0, 1, 1]
-