bronx.syntax.iterators

Useful iterators and associated tools..

Functions

bronx.syntax.iterators.interleave(*iterables)[source]

Take several iterable objects and iterate in a roundrobin manner.

Example:

>>> print(','.join(
...     interleave('abcd', '1234', 'ABCD')
... ))
a,1,A,b,2,B,c,3,C,d,4,D
>>> print(','.join(
...     interleave('abcd', '12', 'ABCD')
... ))
a,1,A,b,2,B,c,C,d,D
>>> print(','.join(
...     interleave('ab', '1234', 'ABCD')
... ))
a,1,A,b,2,B,3,C,4,D
bronx.syntax.iterators.izip_pcn(*iterables)[source]

Like izip but also returns the Previous, Current and Next values.

Example:

>>> for p, c, n in izip_pcn([], []):
...     print(p, c, n)
>>> for p, c, n in izip_pcn([1, ], [10, ]):
...     print(p, c, n)
(None, None) (1, 10) (None, None)
>>> for p, c, n in izip_pcn([1, 2], [10, 11]):
...     print(p, c, n)
(None, None) (1, 10) (2, 11)
(1, 10) (2, 11) (None, None)
>>> for p, c, n in izip_pcn([1, 2, 3, 4], [10, 11, None, 13]):
...     print(p, c, n)
(None, None) (1, 10) (2, 11)
(1, 10) (2, 11) (3, None)
(2, 11) (3, None) (4, 13)
(3, None) (4, 13) (None, None)
bronx.syntax.iterators.pcn(iterable, fillvalue=None)[source]

Iterate over iterable but also return the previous and next values.

Example:

>>> for p, c, n in pcn([]):
...     print(p, c, n)
>>> for p, c, n in pcn([1, ]):
...     print(p, c, n)
None 1 None
>>> for p, c, n in pcn([1, 2, 3, 4, 5]):
...     print(p, c, n)
None 1 2
1 2 3
2 3 4
3 4 5
4 5 None
>>> for p, c, n in pcn([1, 2, None, 4, 5], fillvalue='foo'):
...     print(p, c, n)
foo 1 2
1 2 None
2 None 4
None 4 5
4 5 foo