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.0.tar.gz (81.9 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.0-cp314-cp314-win_amd64.whl (315.4 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.23.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (436.9 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.23.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (439.3 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.23.0-cp314-cp314-macosx_11_0_arm64.whl (411.2 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.23.0-cp314-cp314-macosx_10_12_x86_64.whl (426.9 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.23.0-cp313-cp313-win_amd64.whl (315.8 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.23.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (436.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.23.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (439.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.23.0-cp313-cp313-macosx_11_0_arm64.whl (411.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.23.0-cp313-cp313-macosx_10_12_x86_64.whl (426.5 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.23.0.tar.gz
Algorithm Hash digest
SHA256 357f2277a7e0bbee7445323dac2aa7e2f1b247b230b1ebcf7ced8a0e6adb1020
MD5 92b4b9948ea605802c0ca225078cc5a8
BLAKE2b-256 52490d7823c916b1b68ef6d58dedd687c4bcc27719d62aa24fb5a2a4eb80327b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 e0c3b5187c9f8b6990ee5e2221d81a1371e401e37767ee2d87c603596608f855
MD5 cf3bf816cea0391b255fbf5f65cfe867
BLAKE2b-256 d548d06e834207a96d588431c6e500c6dfa817d3758543e26e259d00cf66b614

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 554e8546360ce3cd10df78e8a702dccf7f7ba1e8971f4e2b19fa25eed0d9785f
MD5 160ddd0e54067ef0554cc23549dbc01e
BLAKE2b-256 c8263b2da266d107a9c1e3b711952245fba0a417fbde2b879024e5c3ddcd84ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4c63134f48f5107d6b74c896cc1722530e86a303a05bc009857696c44ff4cfa2
MD5 3cad00a31f5685432860835183b28549
BLAKE2b-256 1d002c06710dbc77c2887289ab58e5473ca0b25de3d53106afeecfe4e7744ba7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 25fd378948f8a7dc1a554ea8996f7c39fcf5213522b0f5d246e9391630709923
MD5 84077478916197f6f577637dc8904740
BLAKE2b-256 794f07d9bdfe95249e92a526af7e7c7ad9842994ba191066798c5a0479e9ef85

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 480ac62580a7c35170c66b75a444bf33d59a364d0668f4d366ead91a828b3d35
MD5 b9ddd3ee7aaace5770d41afa81e8304c
BLAKE2b-256 9b770cae77cb0d57faefd0d645c18b0fadd417ed22434d69c3d81b2f7a49f13c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e0117355af7807928a04c7252b61ee8230465b6acc5c75340b1b9c1f607ccbab
MD5 a015a6c29a07d466c4addae42d9a8c1e
BLAKE2b-256 d8af06ba839f12b2e8a5818fd620aebb6ff22454e9238925c2ac670688b97ee1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a5691dbf7cf9c5af2dc64e7ccb230989877c7cd9e4eabbe04fd23ac8e4b41ade
MD5 8703534f52555fb5a3066a4dfd6b3cff
BLAKE2b-256 a7d2c8e8b5c5f7800631f0bfa3c870e328353d63cf478de0e889d08700368ab1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0ed74e70444debc733623faa10158495a84a22bafa2028141745bb901ce76700
MD5 e39ce2217b941db04135cd5841ddd3fd
BLAKE2b-256 8e39abe5fda1c2309e5327936ab3a81804be5786ab12530e60e792acf984e274

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 47547e22caddece96bd292f419bbe3ad6fe9f64db0d2e59f21eeaa390ed63909
MD5 b0c1877b67218a2518645e3dd0a6af59
BLAKE2b-256 90aea081d647d0b2f24dab84d73d1d3ffa6cb9c90ce1c6dfc4d0e54404098c75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.23.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c3e88890fabed975bc5a7081972200bac8faf87913113522aa21c13a8a37ec30
MD5 97be98359d95f9a8fd68f7b8b3b3a374
BLAKE2b-256 2b186c5d7d14f19eda10cc245e618f1ddf25290b57f04d8db2741005136499c6

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