snakeoil.iterables module¶
Collection of functionality to make using iterators transparently easier
- class snakeoil.iterables.caching_iter(iterable, sorter=None)[source]¶
Bases:
object
On demand consumes from an iterable so as to appear like a tuple
>>> from snakeoil.iterables import caching_iter >>> i = iter(range(5)) >>> ci = caching_iter(i) >>> print(ci[0]) 0 >>> print(ci[2]) 2 >>> print(i.next()) 3
- cached_list¶
- iterable¶
- sorter¶
- class snakeoil.iterables.expandable_chain(*iterables)[source]¶
Bases:
object
chained iterables, with the ability to add new iterables to the chain as long as the instance hasn’t raised
StopIteration
already. This is fairly useful for implementing queues of things that must be processed.>>> from snakeoil.iterables import expandable_chain >>> l = range(5) >>> i = expandable_chain(l) >>> print(i.next()) 0 >>> print(i.next()) 1 >>> i.appendleft(range(5, 7)) >>> print(i.next()) 5 >>> print(i.next()) 6 >>> print(i.next()) 2
- snakeoil.iterables.iter_sort(sorter, *iterables)[source]¶
Merge a number of sorted iterables into a single sorted iterable.
- Parameters:
sorter (callable.) – function, passed a list of [element, iterable].
iterables – iterables to consume from. It’s required that each iterable to consume from is presorted already within that specific iterable.
- Returns:
yields items one by one in combined sorted order
For example:
>>> from snakeoil.iterables import iter_sort >>> iter1 = range(0, 5, 2) >>> iter2 = range(1, 6, 2) >>> # note that these lists will be consumed as they go, >>> # sorted is just being used to compare the individual items >>> sorted_iter = iter_sort(sorted, iter1, iter2) >>> print(list(sorted_iter)) [0, 1, 2, 3, 4, 5]
- snakeoil.iterables.partition(iterable, predicate=<class 'bool'>)[source]¶
Partition an iterable into two iterables based on a given filter.
Taking care that the predicate is called only once for each element.
- Parameters:
iterable – target iterable to split into two
predicate – filtering function used to split the iterable
- Returns:
A tuple of iterators, the first containing items that don’t match the filter and the second the matched items.