Skip to main content

Chitter: my take on iterator chaining for Python

Introduction

Chitter is a fork Evelyn H's iterchain package, but made to be mine. Anyway, this package makes working with iterators in python more ergonomic by allowing you to chain methods on them (that's easier to read).

Similarly to Evelyn's this package's design is inspired by the Rust iterators, stuff from python's own standard library (itertools).

Installation

With Poetry:

poetry add chitter

With Pip:

pip install chitter

For the development version:

git clone https://github.com/brunofauth/chitter.git
cd ./chitter/
poetry install

Everything below this line comes straight from iterchain's docs, so keep that in mind while reading...

Why would I need this?

Say we want to know the sum of all the squares of even numbers up to 100.
How can we do this?

Let's try some straightforward, procedural Python:

>>> total = 0
>>> for i in range(100):
...     if i % 2 is 0:
...         total += i ** 2
...
>>> total
161700

This works, but if you read this for the first time it can take a bit of effort to figure out what's happening, especially in slightly less trivial cases. So, how about we use iterators instead?

Well, let's see:

>>> sum(i**2 for i in range(100) if i % 2 is 0)
161700

That's pretty nice! Much shorter, and much easier to understand. But there's a problem, this pattern only works for relatively simple manipulations. In those cases you could try using the python map and filter builtins (and the slightly more hidden functools.reduce). They let you construct more complex processing chains.

Let's rewrite our iterator to use those functions instead:

>>> sum(map(lambda x: x**2, filter(lambda x: x % 2 is 0, range(100))))
161700

Okay, now that is a mess... I don't know about you, but it would take me quite a while to unravel what's happening here. The problem is that the whole expression is inside out. The filter gets applied first, but it's hidden in the middle of the expression, and the sum gets applied last but it is all the way in the front. Makes no sense...

So, how can we improve on this? iterchain of course!
(you probably saw this coming already)

So, let's see how it looks using iterchain:

>>> import iterchain
>>> (iterchain.count(stop=100)
...     .filter(lambda x: x % 2 is 0)
...     .map(lambda x: x**2)
...     .sum())
161700

Isn't this much better? The operations are listed in the order that they're executed, are clearly separated, and you can have as few or as many operations as you want. This is why you should use iterchain!

Generators

iterchain also provides handy methods that let you build new Iterator instances from scratch. These are contained in the iterchain.generators sub-module, but they're also accessible directly from the iterchain module, which is the preferred way of using them.

For example:

  >>> import iterchain
  >>> iterchain.count().take(4).map(lambda x: x**2).to_list()
  [0, 1, 4, 9]

Metadata

Release files for chitter 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chitter 0.2.2
File Size Uploaded
chitter-0.2.2.tar.gz 17.4 kB Details

Release files / chitter-0.2.2.tar.gz

Download URL chitter-0.2.2.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
41127a48875c5ffca8e579611bf3d064dfe2877a8d0ed63958ff7fd982efaf3a
BLAKE2b-256 checksum
How to use checksums
d20579a3fdd3d56e5acebfd10639e789e465affae8cd3fe9b2a2da6032b3a147
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release history Release notifications | RSS feed

This release

0.2.2 This release

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page