Reference¶
lazysequence¶
Lazy sequences.
>>> from lazysequence import lazysequence
>>>
>>> def load_records():
... return range(10) # let's pretend this is expensive
...
>>> records = lazysequence(load_records())
>>> if not records:
... raise SystemExit("no records found")
...
>>> first, second = records[:2]
>>>
>>> print("The first record is", first)
The first record is 0
>>> print("The second record is", second)
The second record is 1
>>>
>>> for record in records.release(): # do not cache all records in memory
... print("record", record)
...
record 0
record 1
record 2
record 3
record 4
record 5
record 6
record 7
record 8
record 9
- class lazysequence.lazysequence(iterable, *, _cache=None, _slice=_slice(start=None, stop=None, step=1))¶
A lazy sequence provides sequence operations on an iterable.
- Parameters
iterable (Iterable[_T_co]) – The iterable being wrapped.
_cache (Optional[MutableSequence[_T_co]]) –
_slice (_slice) –
- Return type
None
- release()¶
Iterate over the sequence without caching additional items.
The sequence should no longer be used after calling this function. The same applies to slices of the sequence obtained by using
s[i:j]
.- Yields
The items in the sequence.
- Return type
Iterator[lazysequence._T_co]