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.
  • Memory efficient - Almost all methods from Iter[T] operate in streaming fashion, and Vec[T] provides in-place methods with more memory efficiency than standard list methods (e.g. x.extend_move(y) won't create intermediate allocations like x.extend(y) followed by y.clear()).
  • 🎯 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.23.1.tar.gz (82.3 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.23.1-cp314-cp314-win_amd64.whl (315.9 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.23.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (437.4 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.23.1-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (439.9 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.23.1-cp314-cp314-macosx_11_0_arm64.whl (411.8 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.23.1-cp314-cp314-macosx_10_12_x86_64.whl (427.4 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.23.1-cp313-cp313-win_amd64.whl (316.3 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.23.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (437.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.23.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (439.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.23.1-cp313-cp313-macosx_11_0_arm64.whl (411.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.23.1-cp313-cp313-macosx_10_12_x86_64.whl (427.0 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.23.1.tar.gz
Algorithm Hash digest
SHA256 df3dc680f7ab9f6d3759d9248374d45bb12ee879aef7fe9dfb3bc65728e36a20
MD5 123373b7a4e060205958dec6e0ad4773
BLAKE2b-256 2fe5809e0b83d6db1899b081ce0802599c48545d2c36fae2d1f993d86360c22b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 f58eb61f56edfc7e47d3cf223a70a7b8292da53b8ac8e1677cd309cb8c6b1c38
MD5 2c4e5acbf53e37e2c9d7363aa7ad76a2
BLAKE2b-256 3285b756e0a9c1af89510e36c7e7684bd972882d2e5a8501659a5b5b9c7869e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6237bc857b8af61416b1dbbd54924a64a6fe84c6c70176dae415af1afd6374ed
MD5 934a1b3050be1d3f5b68fa3d92684850
BLAKE2b-256 15b0c2c5dfd1415bdc047f2ccd04e42baf41a48348d619a912f438dcd71f915d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.1-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 508367c8c9ff2796f3d1310de0dca1c3ff7253fe048a5c887d8cb331bad39f09
MD5 722b849e27af5f1748e3f0ebfae5e464
BLAKE2b-256 f3527b22b5f0e583c9f3d2ea6311e6f0e87f44af0e7b70ac06fa08fd86b83d42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 256371ca0849dcfce13a9be548056d24854e9a7c7694c4cfbecddcff264ec01d
MD5 a97a5c2eb3458c7909641d878ba8d537
BLAKE2b-256 81852e3640c0363910fc92966f2038003ad3be7687056c04f0318084affd31e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.1-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f66944ac17beb252153b9bf7946bf0e04fb5c8a99a9069a8f545cb62afd17b67
MD5 c1ad8f57ef1dfd5e6bcaab742eb4e5b7
BLAKE2b-256 011757ea54362126a3e25eebe5e4934080fc11ebb0b4901b90b9e9c38724a237

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2c1aaaa537bdc9003c524802b55706623a94f913a8870f69c0c60805e66f2fe7
MD5 6cfce53730f46f888bc904f10bc66a61
BLAKE2b-256 1f76d1e381924fbe593b603eec9cb07fe1b5088e261e165a0cd4db8619dbda25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e286a33baef096ddc212dce0378f76c0ee2a88c419032eb8d8ea64e1e0d1c99c
MD5 548d6855a9899f6466fd616d925d47c3
BLAKE2b-256 23f0137ab0b7c5ad564e8db947d98d03455d8288b7374b5751e72bd13733b114

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 70bfe2ff0c353a8f1a3d96971410c784ce075604c5afc92e5b9e2faad4bc8fe1
MD5 97055978d6e2bb33719194802a03d579
BLAKE2b-256 0defac8751c95c39610309aadd83f175c9fa1b3dd1a6ac5b2a0d95c1d502e546

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 82254c42039ff6fdf816a6c3ca0a2240e148ad17b64cf20cfa7004195adea967
MD5 e6af9012900e8b2a2eba5fc89516843f
BLAKE2b-256 d71046a098700b50a5fdd4961dbd389ba82a069728bffffcf55817aebf59f339

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.1-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0878d4698dc0840c9b6477a342a6236538926cd559e53a886d75e7fa63ee99d2
MD5 b523d5395a0d88849d14aa2c23e463b6
BLAKE2b-256 460a81a355d899124dbfdf6ad6314f1c777daaaf7c47c0aa69b3afbeeeae0af2

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