Skip to main content

chainit

Documentation available here: https://lukapeschke.github.io/chainit/

This library provides the ChainIt class, a wrapper around stdlib's itertools module, allowing to chain operations on iterables, resulting in easier-to-read code.

import typing as t

def fib() -> t.Iterable[int]:
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

# Allows to write things like this...
(
    ChainIt(fib())
    .filter(lambda x: x % 2 == 0)
    .map(lambda x: x // 2)
    .flat_map(range)
    .take_while(lambda x: x < 6)
    .collect_list()
)

# ...rather than like this
from itertools import chain as ichain, islice, takewhile

list(
    takewhile(
        lambda x: x < 6,
        ichain.from_iterable(
            map(lambda x: range(x // 2), filter(lambda x: x % 2 == 0, fib()))
        ),
    )
)

Installation

pip install chainit

Examples

Decorator

In addition to ChainIt, the library provides a chainit decorator. It makes a function returning an iterable return a ChainIt instead:

@chainit
def fac():
    n = 0
    fac = 1
    while True:
        yield fac
        n += 1
        fac *= n

assert fac().enumerate().take(5).collect() == ((0, 1), (1, 1), (2, 2), (3, 6), (4, 24))

Using a ChainIt instance as an iterable

assert list(fac().take(3)) == [1, 1, 2]

for idx, x in fac().enumerate():
    if idx > 3:
        break
    print(x)

Metadata

Release files for chainit 0.5.0

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

Source distribution (sdist)

Source distribution for chainit 0.5.0
File Size Uploaded
chainit-0.5.0.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chainit 0.5.0
File Interpreter ABI Platform
chainit-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / chainit-0.5.0.tar.gz

Download URL chainit-0.5.0.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
10aebe64b5cb49260e960946e48a8e52d665f83a2fc9d7c93c6c39df404af5af
BLAKE2b-256 checksum
How to use checksums
1a381e2b43f08976dfe7dd37fd8ed586ad3fa349633cee356fffb46216dd2b2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / chainit-0.5.0-py3-none-any.whl

Download URL chainit-0.5.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f128881eb24cece989738b7965729bc85c83256ba1523b3b6969a31eb0002ddb
BLAKE2b-256 checksum
How to use checksums
cb062bed4db1eb6960f1e5ab95923d2b8ca2af065a31b0a030a31c1d17369927
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

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