2015-03-22 06:18:45 +00:00
|
|
|
``iterutils`` - ``itertools`` improvements
|
|
|
|
==========================================
|
2015-03-22 00:12:50 +00:00
|
|
|
|
|
|
|
.. automodule:: boltons.iterutils
|
2015-04-06 02:12:15 +00:00
|
|
|
|
2019-01-10 18:01:11 +00:00
|
|
|
.. contents:: Sections
|
|
|
|
:depth: 3
|
|
|
|
:local:
|
|
|
|
|
2015-09-19 07:44:18 +00:00
|
|
|
.. _iteration:
|
|
|
|
|
2015-04-06 02:12:15 +00:00
|
|
|
Iteration
|
|
|
|
---------
|
|
|
|
|
|
|
|
These are generators and convenient :class:`list`-producing
|
2015-09-19 07:44:18 +00:00
|
|
|
counterparts comprising several common patterns of iteration not
|
|
|
|
present in the standard library.
|
2015-04-06 02:12:15 +00:00
|
|
|
|
|
|
|
.. autofunction:: split
|
|
|
|
.. autofunction:: split_iter
|
2020-06-22 05:19:35 +00:00
|
|
|
.. autofunction:: strip
|
|
|
|
.. autofunction:: strip_iter
|
|
|
|
.. autofunction:: lstrip
|
|
|
|
.. autofunction:: lstrip_iter
|
|
|
|
.. autofunction:: rstrip
|
|
|
|
.. autofunction:: rstrip_iter
|
2015-04-06 02:12:15 +00:00
|
|
|
.. autofunction:: chunked
|
|
|
|
.. autofunction:: chunked_iter
|
2015-05-18 22:58:36 +00:00
|
|
|
.. autofunction:: pairwise
|
|
|
|
.. autofunction:: pairwise_iter
|
2015-04-06 02:12:15 +00:00
|
|
|
.. autofunction:: windowed
|
|
|
|
.. autofunction:: windowed_iter
|
2015-09-18 20:46:38 +00:00
|
|
|
.. autofunction:: unique
|
|
|
|
.. autofunction:: unique_iter
|
2015-09-19 07:44:18 +00:00
|
|
|
|
2015-09-21 09:40:33 +00:00
|
|
|
Nested
|
|
|
|
------
|
|
|
|
|
2015-09-24 10:36:22 +00:00
|
|
|
Nested data structures are common. Yet virtually all of Python's
|
|
|
|
compact iteration tools work with flat data: list comprehensions,
|
|
|
|
map/filter, generator expressions, itertools, even other
|
2015-09-24 18:51:21 +00:00
|
|
|
iterutils.
|
|
|
|
|
|
|
|
The functions below make working with nested iterables and other
|
|
|
|
containers as succinct and powerful as Python itself.
|
2015-09-21 09:40:33 +00:00
|
|
|
|
|
|
|
.. autofunction:: remap
|
2017-03-02 09:37:51 +00:00
|
|
|
.. autofunction:: get_path
|
2017-03-12 03:42:53 +00:00
|
|
|
.. autofunction:: research
|
2019-02-11 17:58:17 +00:00
|
|
|
.. autofunction:: flatten
|
|
|
|
.. autofunction:: flatten_iter
|
2015-09-21 09:40:33 +00:00
|
|
|
|
2015-09-19 07:44:18 +00:00
|
|
|
Numeric
|
|
|
|
-------
|
|
|
|
|
|
|
|
Number sequences are an obvious target of Python iteration, such as
|
|
|
|
the built-in :func:`range`, :func:`xrange`, and
|
|
|
|
:func:`itertools.count`. Like the :ref:`iteration` members above,
|
|
|
|
these return iterators and lists, but take numeric inputs instead of
|
|
|
|
iterables.
|
|
|
|
|
|
|
|
.. autofunction:: backoff
|
|
|
|
.. autofunction:: backoff_iter
|
2015-08-19 08:05:03 +00:00
|
|
|
.. autofunction:: frange
|
|
|
|
.. autofunction:: xfrange
|
|
|
|
|
2015-09-19 07:44:18 +00:00
|
|
|
|
2015-04-06 02:12:15 +00:00
|
|
|
Categorization
|
|
|
|
--------------
|
|
|
|
|
2015-09-19 07:44:18 +00:00
|
|
|
These functions operate on iterables, dividing into groups based on a
|
|
|
|
given condition.
|
2015-04-06 02:12:15 +00:00
|
|
|
|
|
|
|
.. autofunction:: bucketize
|
|
|
|
.. autofunction:: partition
|
2016-03-06 23:31:23 +00:00
|
|
|
|
2019-02-11 17:58:17 +00:00
|
|
|
Sorting
|
|
|
|
-------
|
|
|
|
|
|
|
|
The built-in :func:`sorted()` is great, but what do you do when you want to
|
|
|
|
partially override the sort order?
|
|
|
|
|
|
|
|
.. autofunction:: soft_sorted
|
|
|
|
|
2016-03-06 23:31:23 +00:00
|
|
|
Reduction
|
|
|
|
---------
|
|
|
|
|
|
|
|
:func:`reduce` is a powerful function, but it is also very open-ended
|
|
|
|
and not always the most readable. The standard library recognized this
|
|
|
|
with the addition of :func:`sum`, :func:`all`, and :func:`any`. All
|
|
|
|
these functions take a basic operator (``+``, ``and``, and ``or``) and
|
|
|
|
use the operator to turn an iterable into a single value.
|
|
|
|
|
|
|
|
Functions in this category follow that same spirit, turning iterables
|
|
|
|
like lists into single values:
|
|
|
|
|
2015-07-30 08:58:47 +00:00
|
|
|
.. autofunction:: one
|
2015-07-30 09:07:00 +00:00
|
|
|
.. autofunction:: first
|
2016-03-06 23:31:23 +00:00
|
|
|
.. autofunction:: same
|
2015-04-06 02:12:15 +00:00
|
|
|
|
|
|
|
Type Checks
|
|
|
|
-----------
|
|
|
|
|
|
|
|
In the same vein as the feature-checking builtin, :func:`callable`.
|
|
|
|
|
|
|
|
.. autofunction:: is_iterable
|
|
|
|
.. autofunction:: is_scalar
|
2015-07-17 20:52:44 +00:00
|
|
|
.. autofunction:: is_collection
|