arsenal.iterextras package

Submodules

arsenal.iterextras.fair module

arsenal.iterextras.fair.fair_product(*iterables)[source]

Returns Cartesian product of iterables. Yields every element eventually.

Based closely on https://github.com/sympy/sympy/issues/17157

arsenal.iterextras.fair.fair_union(*iterables)

Merge iterators with a round-robin scheduler. Original implementation by George Sakkis.

>>> list(merge_roundrobin('ABC', 'D', 'EF'))
['A', 'D', 'E', 'B', 'F', 'C']
>>> from itertools import count
>>> from arsenal.iterextras.util import take
>>> list(take(10, merge_roundrobin('ABC', count(), 'E')))
['A', 0, 'E', 'B', 1, 'C', 2, 3, 4, 5]
arsenal.iterextras.fair.merge_roundrobin(*iterables)[source]

Merge iterators with a round-robin scheduler. Original implementation by George Sakkis.

>>> list(merge_roundrobin('ABC', 'D', 'EF'))
['A', 'D', 'E', 'B', 'F', 'C']
>>> from itertools import count
>>> from arsenal.iterextras.util import take
>>> list(take(10, merge_roundrobin('ABC', count(), 'E')))
['A', 0, 'E', 'B', 1, 'C', 2, 3, 4, 5]
arsenal.iterextras.fair.test_fair_product()[source]

arsenal.iterextras.sort module

class arsenal.iterextras.sort.Item(cost, index, elems)[source]

Bases: object

arsenal.iterextras.sort.main()[source]
arsenal.iterextras.sort.merge_sorted(*iterators)

Merge multiple sorted inputs into a single sorted output.

Equivalent to: sorted(itertools.chain(*iterables))

>>> list(merge_sorted([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25]))
[0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25]
arsenal.iterextras.sort.sorted_product(p, *iters)[source]

Sorted product of iters, where the output is sorted by a monotonic product operator p. Examples: tuples, or multiplication/addition of positive numbers.

arsenal.iterextras.sort.sorted_union(*iterators)[source]

Merge multiple sorted inputs into a single sorted output.

Equivalent to: sorted(itertools.chain(*iterables))

>>> list(merge_sorted([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25]))
[0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25]

arsenal.iterextras.sorted_intersection module

arsenal.iterextras.sorted_intersection.sorted_intersection(A, B)[source]

Baeza-Yates set intersection, aka double-binary search.

Let N = min(|A|, |B|) and M=max(|A|, |B|)

  • hash-based set intersection: O(N+M)
  • Baeza-Yates intersection: O(N log M) worst-case, better for most problems on “average”

References: - https://mail.python.org/pipermail/python-list/2008-April/508321.html

arsenal.iterextras.sorted_intersection.test()[source]

arsenal.iterextras.util module

arsenal.iterextras.util.argmax(f, seq)[source]
>>> argmax(lambda x: -x**2 + 1, range(-10,10))
0
arsenal.iterextras.util.argmax2(f, seq)[source]
>>> argmax2(lambda x: -x**2 + 1, range(-10,10))
(1, 0)
arsenal.iterextras.util.argmin(f, seq)[source]
>>> argmin(lambda x: x**2 + 1, range(-10,10))
0
arsenal.iterextras.util.argmin2(f, seq)[source]
>>> argmin2(lambda x: x**2 + 1, range(-10,10))
(1, 0)
arsenal.iterextras.util.atmost(k, seq)[source]
>>> atmost(1, [0,0,0])
True
>>> atmost(1, [0,1,0])
True
>>> atmost(1, [0,1,1])
False
arsenal.iterextras.util.batch(size, iterable)[source]

Yield a list of (up to) batchsize items at a time. The last element will be shorter if there are items left over. batch(s, 2) -> [s0,s1], [s2,s3], [s4, s5], …

>>> list(batch(2, range(5)))
[[0, 1], [2, 3], [4]]
arsenal.iterextras.util.breadth_first(tree, children=<built-in function iter>, depth=-1, queue=None)[source]

Traverse the nodes of a tree in breadth-first order.

  • (No need to check for cycles.)
  • The first argument should be the tree root;
  • children should be a function taking as argument a tree node and returning an iterator of the node’s children.
class arsenal.iterextras.util.buf_iter(x)[source]

Bases: object

Lazy, random access wrapper around an iterator.

arsenal.iterextras.util.compress(data, selectors)[source]
>>> list(compress('ABCDEF', [1,0,1,0,1,1]))
['A', 'C', 'E', 'F']
arsenal.iterextras.util.drop(iterator, n)[source]

Advance the iterator n-steps ahead.

arsenal.iterextras.util.groupby2(s, key=<function <lambda>>)[source]

Eager version of groupby which does what you’d expect groupby to do.

>>> groupby2(range(10), lambda x: x % 2)
{0: [0, 2, 4, 6, 8], 1: [1, 3, 5, 7, 9]}
class arsenal.iterextras.util.head_iter(iterator)[source]

Bases: object

arsenal.iterextras.util.k_fold_cross_validation(X, K, randomize=False)[source]

Generates K (training, validation) pairs from the items in X.

Each pair is a partition of X, where validation is an iterable of length len(X)/K. So each training iterable is of length (K-1)*len(X)/K.

If randomise is true, a copy of X is shuffled before partitioning, otherwise its order is preserved in training and validation.

>>> for train, test in k_fold_cross_validation(range(3), 3):
...     print('test:', test, ' train:', list(train), sep=' ')
test: [0]  train: [1, 2]
test: [1]  train: [0, 2]
test: [2]  train: [0, 1]
arsenal.iterextras.util.ncycles(seq, n)[source]

Returns the sequence elements n times

arsenal.iterextras.util.padnone(seq)[source]

Returns the sequence elements and then returns None indefinitely

arsenal.iterextras.util.partition(data, proportion)[source]

Deterministically partition data according to proportion. Note: assumes sum(proportion) <= 1.0

>>> partition(range(10), [0.3, 0.7])
[[0, 1, 2], [3, 4, 5, 6, 7, 8, 9]]
arsenal.iterextras.util.take(n, seq)[source]

Return the first n items in a sequence.

arsenal.iterextras.util.unique(iterable, key=None)[source]

List unique elements, preserving order. Remember all elements ever seen.

arsenal.iterextras.util.window(iterable, k)[source]

s -> (s[0],…,s[k]) (s[1],…,s[k+1]) … (s[i],…,s[i+k]) … (s[n-1-k],…,s[n-1])

>>> [x+y+z for x,y,z in window('abcdef', 3)]
['abc', 'bcd', 'cde', 'def']

Module contents