Skip to main content

Chainable lazy iterators

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chainit-0.5.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chainit-0.5.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file chainit-0.5.0.tar.gz.

File metadata

  • Download URL: chainit-0.5.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for chainit-0.5.0.tar.gz
Algorithm Hash digest
SHA256 10aebe64b5cb49260e960946e48a8e52d665f83a2fc9d7c93c6c39df404af5af
MD5 46724001d38d56a5b7efe77feafeab41
BLAKE2b-256 1a381e2b43f08976dfe7dd37fd8ed586ad3fa349633cee356fffb46216dd2b2d

See more details on using hashes here.

File details

Details for the file chainit-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: chainit-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for chainit-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f128881eb24cece989738b7965729bc85c83256ba1523b3b6969a31eb0002ddb
MD5 d57362a78a907773231957e53e07c7a6
BLAKE2b-256 cb062bed4db1eb6960f1e5ab95923d2b8ca2af065a31b0a030a31c1d17369927

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page