Skip to main content

Method chaining for iterables and dictionaries in Python.

Project description

pyochain ⛓️

Fluent method chaining for Python.

Inspired by Rust's Iterator, Result, Option, and DataFrame libraries like Polars, pyochain is a no-dependency library providing a set of classes with a fluent API, to work with iterations, collections, handle optional values, or manage errors.

Key Features

  • ⛓️ Declarative & fluent chaining — Replace for loops, None checks, and error handling with chainable methods.
  • 🦥 Lazy-first, 🔒 explicit mutabilityIter[T] for lazy, efficient iterations; Seq and Set for immutable data; Vec and SetMut when you need to mutate.
  • 🎯 Result and Option types - Handle None and exceptions in a fluent, explicit way.
  • 🔥 Blazingly fast — Core Option and Result types are written in Rust for minimal overhead, and iterators use almost always compiled C or Rust level code, from the builtins, itertools or custom Pyo3 implementations.
  • 🛡️ 100% type-safe — Full generic support and autocompletion in your IDE.
  • 📚 Accurate Documentation — Every method is documented and tested with runnable examples. Every code example in the website (or this README) is also tested, ensuring accuracy and reliability.
  • 🔄 Interoperable — Seamlessly convert to/from types with various methods like .into() and .collect(), convert Iterables to Option or Result based on their truthiness, and more.
  • 🐍 Mixins and ABC's — Extend your own classes with the mixins, Protocol and ABC's provided by the abc module.

Installation

uv add pyochain # or pip install pyochain

See the package page on Pypi

Quick Start

Iterations

Below is an example of how to use Iter, and how it compares to a pure Python implementation using itertools:

from pyochain import Iter, Seq
import itertools

wanted = ((0, "1"), (1, "9"), (2, "25"), (3, "49"), (4, "81"))

pyochain_res = (
    Iter
    .from_count(1)
    .filter(lambda x: x % 2 != 0)
    .map(lambda x: x**2)
    .take(5)
    .enumerate()
    .map_star(lambda idx, value: (idx, str(value)))
    .collect(tuple)
)
py_res = tuple(
    itertools.islice(
        itertools.starmap(
            lambda idx, val: (idx, str(val)),
            enumerate(
                map(lambda x: x**2, filter(lambda x: x % 2 != 0, itertools.count(1)))
            ),
        ),
        5,
    )
)
assert pyochain_res == py_res == wanted

Result and Option Types

from pyochain import Option, NONE, Some, Seq, Vec, Set, Ok, Err, Result
from pyochain.abc import PyoIterable


def divide(a: int, b: int) -> Option[float]:
    return NONE if b == 0 else Some(a / b)


assert divide(10, 2) == Some(5.0)
# Provide a default value
assert divide(10, 0).unwrap_or(-1.0) == -1.0
# Convert between Collections -> Option -> Result
tup = (1, 2, 3)
seq = Seq(tup)
assert seq.then_some() == Some(Seq(tup))
assert seq.then_some().ok_or("No values").unwrap() == Seq(tup)


# Accept any Pyochain Iterable
def _process(data: PyoIterable[int]) -> str:
    return data.iter().map(str).join(", ")


# Process only if non-empty, convert Option to Result
assert seq.then(_process).ok_or("No values").unwrap() == "1, 2, 3"
assert (
    Vec(()).then(_process).ok_or("No values").expect_err("expected error")
    == "No values"
)
# Create empty Set, convert to Result, then back to Option
assert Set(()).then(_process).ok_or("No values").ok() is NONE

Type safe exhaustive handling with pattern matching

Type checkers will ensure that all cases are handled when matching on Option and Result types.

from pyochain import Result, Ok, Err


def try_parse_int(s: str) -> Result[int, ValueError]:
    try:
        return Ok(int(s))
    except ValueError as e:
        return Err(e)


def handle_result(res: Result[int, ValueError]) -> str:
    match res:
        case Ok(value):
            return f"Parsed value: {value}"
        case Err(_):
            return f"Error parsing int!"


assert try_parse_int("123").into(handle_result) == "Parsed value: 123"
assert try_parse_int("abc").into(handle_result) == "Error parsing int!"

Documentation

For comprehensive guides and examples:

🔍 Types Overview — Roles, comparisons and visual relationships

📚 Full API Reference — Complete API documentation

Notice on Stability ⚠️

pyochain is currently in early development (< 1.0), and the API may undergo significant changes multiple times before reaching a stable 1.0 release.

Contributing

Want to contribute? Read our contributing guide

Credits

Most of the custom computation algorithms have been inspired by implementations from itertools, cytoolz and more-itertools.

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

pyochain-0.24.0.tar.gz (82.0 kB view details)

Uploaded Source

Built Distributions

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

pyochain-0.24.0-cp314-cp314-win_amd64.whl (324.3 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.24.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (448.1 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.24.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (448.7 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.24.0-cp314-cp314-macosx_11_0_arm64.whl (419.8 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.24.0-cp314-cp314-macosx_10_12_x86_64.whl (435.9 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.24.0-cp313-cp313-win_amd64.whl (324.9 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.24.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (448.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.24.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (449.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.24.0-cp313-cp313-macosx_11_0_arm64.whl (420.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.24.0-cp313-cp313-macosx_10_12_x86_64.whl (435.9 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

Details for the file pyochain-0.24.0.tar.gz.

File metadata

  • Download URL: pyochain-0.24.0.tar.gz
  • Upload date:
  • Size: 82.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for pyochain-0.24.0.tar.gz
Algorithm Hash digest
SHA256 81be35e241ed6496a9c4f8e1e2008795f3154ab116f4d748fe53fdb68e5e5efb
MD5 e9e1b76505f7eb033afebd91753940bb
BLAKE2b-256 67f08c6b61acb947e1214cd22e479078533550734776f40aba3c267d788cfe90

See more details on using hashes here.

File details

Details for the file pyochain-0.24.0-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for pyochain-0.24.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 22d708c465e1ca689c4bca5b6d321c97017d6263d457658bc24326dc6723666f
MD5 2da0b5f75f5d135c798295d7414cb9a4
BLAKE2b-256 a1be0244dd9f4111deba9fb7bd0fec4f31c93be7f07f9f35ba31bfe80af1e36a

See more details on using hashes here.

File details

Details for the file pyochain-0.24.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyochain-0.24.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a05ee26ebac3501b3f01eb87b6e6c9fe144b743c1a7e4097d5475854f59d8b03
MD5 34ddd777d4d629955964ebd91c4a7d45
BLAKE2b-256 4b331ecc6dea287ab08858c09006683fb9c54ee0fac6838cc0ed24476ccefe39

See more details on using hashes here.

File details

Details for the file pyochain-0.24.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pyochain-0.24.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 bec69ab8592ae5d2b310ff1d2ad026451854a691e4a8b41117c9780fbf282430
MD5 407ca7d7a08ecbf9303a8ae3e73d1cfb
BLAKE2b-256 9301cb09d96b65ee91e70e3ffaa0413368cb588c839a8916eb61ccc07065ac67

See more details on using hashes here.

File details

Details for the file pyochain-0.24.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyochain-0.24.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 915dcddca4bda48ebdff1195fbbe54b7fc30ccdba78a254f053efdb12866b069
MD5 db030afa67bad0fa1464bea19d60bfb0
BLAKE2b-256 2d3853c54e14690f885584cfb39af1698b9e434600f55c6b6faf9d9a436afd9e

See more details on using hashes here.

File details

Details for the file pyochain-0.24.0-cp314-cp314-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyochain-0.24.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4ebab9a8e16e4dc0b74f2674af999bc49d89ba354f2e070b28cdffa1f139c9f9
MD5 8e604ae4bbf02d632afbcaf602460c0b
BLAKE2b-256 6cbae86ff68b51e1ae826768ce5d1df9449a03cdbf3661b48530897bdb6bc132

See more details on using hashes here.

File details

Details for the file pyochain-0.24.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for pyochain-0.24.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 55cea5ee9b247fb85fa40a2e9440cbb3c3563a6535e079bfa87ba667d49fdc18
MD5 32f26203f45fc51b6209369679a219e7
BLAKE2b-256 54b1a512a23c8e2cf38cfc36f3b2ea076cb0053c9ad2a7596071dae2823bd38f

See more details on using hashes here.

File details

Details for the file pyochain-0.24.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyochain-0.24.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 62eae588c49039a2c14bea421641bcdae7d3c5d16e684dbcffc8408b386920d6
MD5 7f82977577fbaccb17a19e9d40458018
BLAKE2b-256 bfc298cbd33cdf8059ab16c069e423d49c5b629b362b504f7fafcf2653c46e52

See more details on using hashes here.

File details

Details for the file pyochain-0.24.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pyochain-0.24.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 127848d6e51b8c79c2cf1d52b60230411aa2410f1acfa3ef23341ed18a81d48c
MD5 4a53d2e5f8ca893f6d4112a3b62ed907
BLAKE2b-256 052c3f519db66596197b6ce15455ad66b338b2eaaab63269999d401d11382d9f

See more details on using hashes here.

File details

Details for the file pyochain-0.24.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyochain-0.24.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 651fced5c1079b9468fbbe0b331f2ef11bdd300b6dbefe56ec8aeed314f490cd
MD5 444ba6f8ba653949aa8ce578c7cca7d6
BLAKE2b-256 a42e6c2653737c1d2994f16a1cb32b52e2da635597989d60ab8f95e429e0c344

See more details on using hashes here.

File details

Details for the file pyochain-0.24.0-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyochain-0.24.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 981f1e5eff2c72a8838aba4087daa8a12f8660fc5bc1d540f5886a414106b6f1
MD5 f6490f40c6b1be57ee70c4d9dc48e8bc
BLAKE2b-256 38e853762be57beb7c3a4b5c5339a70406fa3342b5d42013bd26d1b201b6a688

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