Iteration Tools

Implements itertools functions for earlier versions of Python.

class brownie.itools.chain(*iterables)[source]

An iterator which yields elements from the given iterables until each iterable is exhausted.

New in version 0.2.

classmethod from_iterable(iterable)[source]

Alternative constructor which takes an iterable yielding iterators, this can be used to chain an infinite number of iterators.

brownie.itools.izip_longest(*iterables, **kwargs)[source]

Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.

If one of the iterables is potentially infinite, then the izip_longest() function should be wrapped with something that limits the number of calls (for example itertools.islice() or itertools.takewhile().) If not specified, fillvalue defaults to None.

Note

Software and documentation for this function are taken from CPython, license details.

brownie.itools.permutations(iterable, r=None)[source]

Return successive r length permutations of elements in the iterable.

If r is not specified or is None, then r defaults to the length of the iterable and all possible full-length permutations are generated.

Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeating value in each permutation.

The number of items returned is n! / (n - r)! when 0 <= r <= n or zero when r > n.

Note

Software and documentation for this function are taken from CPython, license details.

brownie.itools.product(*iterables, **kwargs)[source]

Cartesian product of input iterables.

Equivalent to nested for-loops in a generator expression. For example, product(A, B) returns the same as ((x, y) for x in A for y in B).

The nested loops cycle like an odometer with the rightmost element advancing on every iteration. The pattern creates a lexicographic ordering so that if the input’s iterables are sorted, the product tuples are emitted in sorted order.

To compute the product of an iterable with itself, specify the number of repetitions with the optional repeat keyword argument. For example, product(A, repeat=4) means the same as product(A, A, A, A).

Note

Software and documentation for this function are taken from CPython, license details.

brownie.itools.starmap(function, iterable)[source]

Make an iterator that computes the function using arguments obtained from the iterable. Used instead of itertools.imap() when an argument parameters are already grouped in tuples from a single iterable (the data has been “pre-zipped”). The difference between itertools.imap() and starmap() parallels the distinction between function(a, b) and function(*c).

Note

Software and documentation for this function are taken from CPython, license details.

brownie.itools.combinations_with_replacement(iterable, r)[source]

Return r length sub-sequences of elements from the iterable allowing individual elements to be replaced more than once.

Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combinations tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, the generated combinations will also be unique.

The number of items returned is (n + r - 1)! / r! / (n - 1)! when n > 0.

Note

Software and documentation for this function are taken from CPython, license details.

brownie.itools.compress(data, selectors)[source]

Make an iterator that filters elements from the data returning only those that have a corresponding element in selectors that evaluates to True. Stops when either the data or selectors iterables have been exhausted.

Note

Software and documentation for this function are taken from CPython, license details.

brownie.itools.count(start=0, step=1)[source]

Make an iterator that returns evenly spaced values starting with start. Often used as an argument to imap() to generate consecutive data points. Also, used with izip() to add sequence numbers.

When counting with floating point numbers, better accuracy can sometimes be achieved by substituting multiplicative code such as: (start + step * i for i in count()).

Note

Software and documentation for this function are taken from CPython, license details.

brownie.itools.grouped(n, iterable, fillvalue=None)[source]

Groups the items in the given iterable to tuples of size n. In order for groups to always be of the size n the fillvalue is used for padding.

brownie.itools.unique(iterable, seen=None)[source]

Yields items from the given iterable of (hashable) items, once seen an item is not yielded again.

Parameters:seen

An iterable specifying already ‘seen’ items which will be excluded from the result.

New in version 0.5.

Changed in version 0.5: Items don’t have to be hashable any more.

brownie.itools.flatten(iterable, ignore=(<type 'basestring'>, ))[source]

Flattens a nested iterable.

Parameters:ignore – Types of iterable objects which should be yielded as-is.

New in version 0.5.

Navigation

Documentation overview

This Page

Fork me on GitHub