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.25.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.25.0-cp314-cp314-win_amd64.whl (327.0 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.25.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (451.1 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.25.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (451.8 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.25.0-cp314-cp314-macosx_11_0_arm64.whl (422.6 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.25.0-cp314-cp314-macosx_10_12_x86_64.whl (438.6 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.25.0-cp313-cp313-win_amd64.whl (327.5 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.25.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (451.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.25.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (452.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.25.0-cp313-cp313-macosx_11_0_arm64.whl (423.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.25.0-cp313-cp313-macosx_10_12_x86_64.whl (438.7 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: pyochain-0.25.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.25.0.tar.gz
Algorithm Hash digest
SHA256 19b627e722316bcc1e8e2efbf7e4b5b4d4ef579fa2009e86eea7c7d23cb2e38e
MD5 c520126d495df3ab24e9af407ce57597
BLAKE2b-256 3ceacffbec9dacc35b1202baf1fe3b2d99620c6ae2fe0dd8ac6b306051211a88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.25.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 d8717fe8b9f67b4797f764606cd085a5572227a24ace92a1260801b471d51f8a
MD5 b5e07bb229d7c3a2f0f5b86cdb56ef40
BLAKE2b-256 765d2fa8d96f636d33c9fb507cc04355ddb2f05e177898d45f57ab2d22269457

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.25.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8333ee6174e4ab9a5634812c55d87e98f82dc90f0ec3765ba44ee7ee46b6b3d0
MD5 d07c7d680fdfa577fe7798753e66304b
BLAKE2b-256 0972bc2a6ebe51d7d23837f8d03693290772bafdbb8c4a219addb5f8c83e030f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.25.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0b95ec19f44271168f1deddfe3c05340fb10fdec637909ef60bc09db727ab3f1
MD5 773188fd3b03fdce9e6691c23dfae96d
BLAKE2b-256 d04abdc2ca91e397b2bf4cac04473e4b308497210259ea457d928ee0bb80cecf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.25.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7e1b1d416967db143d847f3dee059f5a1b4a8e49673c2764be52d86793dda1de
MD5 adfcf7eade5797749f8d234bfc6a12d5
BLAKE2b-256 61873c0196072f33e3cc32201eaf89f27e521fe179c7bfc963ddd18d6dc53f43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.25.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a39fbef0c85dde11d08caa89bc6865ce0f02bcea9970502fb8c137a5681b86cb
MD5 f78f9e31cd882a5f9c32bc27b1ae1411
BLAKE2b-256 39729b36f1ca7fc4c3504235da5bd78f64950e2adeaf85a85a490c2096c61bb6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.25.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e1f5b0f986e731dd1ea79c3002f8d9a062f3140514f35e7ef60f53ce302e5f6f
MD5 9a5f55b948139074f6b724484a1d38c9
BLAKE2b-256 5e91d2f33f1b02bb10328cab73a24abb536372dfc4a53549444e9c7c6fb49a64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.25.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 18d208b26732392c2763bd6c34142988e4afd1bcd4775d3c6e5ddc375bb30bfd
MD5 bba2928ffeb2da34e25ef721f3257fba
BLAKE2b-256 b6527f8c52147d9558d40023852aa7f600468207f4e711d2243c391473578cb7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.25.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 91fb8fa726d12690cd425a1c1938ef6ac9884ab41b6d8d39a0849d471700472b
MD5 d93f5c6f5c64f1befb803cf97178926c
BLAKE2b-256 86c432c24ccd4514256dafecae6f9598980675eaf18404f309716bc13e17d937

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.25.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 26ba6781c27ed50b6bc550a56b55efb5547e9e04fb96d0cf854f3a4fc6953584
MD5 629e4df7532d6b4054299f537b11f80b
BLAKE2b-256 5cd49195b2015234ec25c48c188f53154ada2ee5c20a369b0835252495227f97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.25.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7931c7b77aaf7164ac89bdbd3aec6274715a5dfd2b47c89f4a94be92315adce5
MD5 252448bbc1e86a72ed099dab1ffdf9f9
BLAKE2b-256 c36b1f877ab9ed1ab85e33f0d0504287c76aea8f164023b3189053cbe42a6a8e

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