iterative

Functions for iteration, looping etc.

New in version 1.4.0.

Data:

AnyNum

Invariant TypeVar constrained to float and complex.

Functions:

Len(obj[, start, step])

Shorthand for range(len(obj)).

chunks(l, n)

Yield successive n-sized chunks from l.

count([start, step])

Make an iterator which returns evenly spaced values starting with number start.

double_chain(iterable)

Flatten a list of lists of lists into a single list.

extend(sequence, minsize)

Extend sequence by repetition until it is at least as long as minsize.

extend_with(sequence, minsize, with_)

Extend sequence by adding with\_ to the right hand end until it is at least as long as minsize.

extend_with_none(sequence, minsize)

Extend sequence by adding None to the right hand end until it is at least as long as minsize.

flatten(iterable[, primitives])

Flattens a mixed list of primitive types and iterables of those types into a single list, regardless of nesting.

groupfloats(iterable[, step])

Returns an iterator over the discrete ranges of values in iterable.

make_tree(tree)

Returns the string representation of a mixed list of strings and lists of strings, similar to tree(1).

natmax(seq[, key, alg])

Returns the maximum value from seq when sorted naturally.

natmin(seq[, key, alg])

Returns the minimum value from seq when sorted naturally.

permutations(data[, n])

Return permutations containing n items from data without any reverse duplicates.

ranges_from_iterable(iterable[, step])

Returns an iterator over the minimum and maximum values for each discrete ranges of values in iterable.

split_len(string, n)

Split string every n characters.

AnyNum = TypeVar(AnyNum, float, complex)

Type:    TypeVar

Invariant TypeVar constrained to float and complex.

Len(obj, start=0, step=1)[source]

Shorthand for range(len(obj)).

Returns an object that produces a sequence of integers from start (inclusive) to len(obj) (exclusive) by step.

New in version 0.4.7.

Parameters
  • obj (Sized) – The object to iterate over the length of.

  • start (int) – The start value of the range. Default 0.

  • step (int) – The step of the range. Default 1.

Return type

range

Changed in version 1.4.0: Moved from domdf_python_tools.utils

chunks(l, n)[source]

Yield successive n-sized chunks from l.

Parameters
  • l (Sequence[~_T]) – The objects to yield chunks from.

  • n (int) – The size of the chunks.

Return type

Iterator[Sequence[~_T]]

Changed in version 1.4.0: Moved from domdf_python_tools.utils

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

Make an iterator which returns evenly spaced values starting with number start.

Often used as an argument to map() to generate consecutive data points. Can also be used with zip() to add sequence numbers.

New in version 2.7.0.

Parameters
  • start (~AnyNum) – Default 0.

  • step (~AnyNum) – The step between values. Default 1.

Return type

Iterator[~AnyNum]

See also

itertools.count().

The difference is that this returns more exact floats, whereas the values from itertools.count() drift.

A demonstration of the drift can be seen in this file: count_demo.py.

double_chain(iterable)[source]

Flatten a list of lists of lists into a single list.

Literally just:

chain.from_iterable(chain.from_iterable(iterable))

Will convert

[[(1, 2), (3, 4)], [(5, 6), (7, 8)]]

to

[1, 2, 3, 4, 5, 6, 7, 8]

New in version 0.4.7.

Parameters

iterable (Iterable[Iterable[Iterable[~_T]]]) – The iterable to chain.

Return type

Iterator[~_T]

Changed in version 1.4.0: Moved from domdf_python_tools.utils

extend(sequence, minsize)[source]

Extend sequence by repetition until it is at least as long as minsize.

New in version 2.3.0.

Parameters
Return type

List[~_T]

extend_with(sequence, minsize, with_)[source]

Extend sequence by adding with\_ to the right hand end until it is at least as long as minsize.

New in version 2.3.0.

Parameters
Return type

List[~_T]

extend_with_none(sequence, minsize)[source]

Extend sequence by adding None to the right hand end until it is at least as long as minsize.

New in version 2.3.0.

Parameters
Return type

Sequence[Optional[~_T]]

See also

extend() and extend_with()

flatten(iterable, primitives=(<class 'str'>, <class 'int'>, <class 'float'>))[source]

Flattens a mixed list of primitive types and iterables of those types into a single list, regardless of nesting.

New in version 1.4.0.

Parameters
  • iterable (Iterable[~_T])

  • primitives (Tuple[Type, …]) – The primitive types to allow. Default (<class 'str'>, <class 'int'>, <class 'float'>).

Return type

Iterator[~_T]

groupfloats(iterable, step=1)[source]

Returns an iterator over the discrete ranges of values in iterable.

For example:

>>> list(groupfloats(
...     [170.0, 170.05, 170.1, 170.15, 171.05, 171.1, 171.15, 171.2],
...     step=0.05,
... ))
[(170.0, 170.05, 170.1, 170.15), (171.05, 171.1, 171.15, 171.2)]
>>> list(groupfloats([1, 2, 3, 4, 5, 7, 8, 9, 10]))
[(1, 2, 3, 4, 5), (7, 8, 9, 10)]

New in version 2.0.0.

Parameters
  • iterable (Iterable[float])

  • step (float) – The step between values in iterable. Default 1.

Return type

Iterable[Tuple[float, …]]

See also

ranges_from_iterable(), which returns an iterator over the min and max values for each range.

make_tree(tree)[source]

Returns the string representation of a mixed list of strings and lists of strings, similar to tree(1).

New in version 1.4.0.

Parameters

tree (Union[Sequence[str], Sequence[Union[Sequence[str], Sequence]]])

Return type

Iterator[str]

natmax(seq, key=None, alg=<ns.DEFAULT: 0>)[source]

Returns the maximum value from seq when sorted naturally.

New in version 1.8.0.

Parameters
  • seq (Iterable[~_T])

  • key (Optional[Callable[[Any], Any]]) – A key used to determine how to sort each element of the iterable. It is not applied recursively. The callable should accept a single argument and return a single value. Default None.

  • alg (int) – This option is used to control which algorithm natsort uses when sorting. Default <ns.DEFAULT: 0>.

Return type

~_T

natmin(seq, key=None, alg=<ns.DEFAULT: 0>)[source]

Returns the minimum value from seq when sorted naturally.

New in version 1.8.0.

Parameters
  • seq (Iterable[~_T])

  • key (Optional[Callable[[Any], Any]]) – A key used to determine how to sort each element of the iterable. It is not applied recursively. The callable should accept a single argument and return a single value. Default None.

  • alg (int) – This option is used to control which algorithm natsort uses when sorting. Default <ns.DEFAULT: 0>.

Return type

~_T

permutations(data, n=2)[source]

Return permutations containing n items from data without any reverse duplicates.

If n is equal to or greater than the length of the data an empty list of returned.

Parameters
Return type

List[Tuple[~_T, …]]

Changed in version 1.4.0: Moved from domdf_python_tools.utils

ranges_from_iterable(iterable, step=1)[source]

Returns an iterator over the minimum and maximum values for each discrete ranges of values in iterable.

For example:

>>> list(ranges_from_iterable([170.0, 170.05, 170.1, 170.15, 171.05, 171.1, 171.15, 171.2], step=0.05))
[(170.0, 170.15), (171.05, 171.2)]
>>> list(ranges_from_iterable([1, 2, 3, 4, 5, 7, 8, 9, 10]))
[(1, 5), (7, 10)]
Parameters
  • iterable (Iterable[float])

  • step (float) – The step between values in iterable. Default 1.

Return type

Iterable[Tuple[float, float]]

split_len(string, n)[source]

Split string every n characters.

Parameters
  • string (str)

  • n (int) – The number of characters to split after

Return type

List[str]

Returns

The split string

Changed in version 1.4.0: Moved from domdf_python_tools.utils