gpf.common.iterutils module

This module only exists in the Python 2 version of the Geocom Python Framework (GPF). It contains functions from the more_itertools module that are not available in the standard Python 2.7.14 library (ArcGIS 10.6 version).

To avoid dependencies in the GPF, these functions have been copied from their original source on GitHub and have been slightly modified for better integration. The state of the functions listed below matches Git commit ec9e743.

Warning

This module should not be used in the Python 3 version of the Geocom Python Framework. It is recommended to use the built-in more_itertools module in this case.

gpf.common.iterutils.collapse(iterable, base_type=None, levels=None)[source]

Flatten an iterable with multiple levels of nesting (e.g., a list of lists of tuples) into non-iterable types.

>>> iterable = [(1, 2), ([3, 4], [[5], [6]])]
>>> list(collapse(iterable))
[1, 2, 3, 4, 5, 6]

String types are not considered iterable and will not be collapsed. To avoid collapsing other types, specify base_type:

>>> iterable = ['ab', ('cd', 'ef'), ['gh', 'ij']]
>>> list(collapse(iterable, base_type=tuple))
['ab', ('cd', 'ef'), 'gh', 'ij']

Specify levels to stop flattening after a certain level:

>>> iterable = [('a', ['b']), ('c', ['d'])]
>>> list(collapse(iterable))  # Fully flattened
['a', 'b', 'c', 'd']
>>> list(collapse(iterable, levels=1))  # Only one level flattened
['a', ['b'], 'c', ['d']]

Note

Function copied from more_itertools package (Python 3 built-in).

gpf.common.iterutils.first(iterable, default=<object object>)[source]

Return the first item of iterable, or default if iterable is empty.

>>> first([0, 1, 2, 3])
0
>>> first([], 'some default')
'some default'

If default is not provided and there are no items in the iterable, raise ValueError.

first() is useful when you have a generator of expensive-to-retrieve values and want any arbitrary one. It is marginally shorter than next(iter(iterable), default).

Note

Function copied from more_itertools package (Python 3 built-in).