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 .pipe() 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").pipe(handle_result) == "Parsed value: 123"
assert try_parse_int("abc").pipe(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.26.0.tar.gz (82.5 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.26.0-cp314-cp314-win_amd64.whl (318.3 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.26.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (443.4 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.26.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (443.6 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.26.0-cp314-cp314-macosx_11_0_arm64.whl (416.1 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.26.0-cp314-cp314-macosx_10_12_x86_64.whl (432.4 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.26.0-cp313-cp313-win_amd64.whl (318.3 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.26.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (442.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.26.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (443.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.26.0-cp313-cp313-macosx_11_0_arm64.whl (416.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.26.0-cp313-cp313-macosx_10_12_x86_64.whl (432.0 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.26.0.tar.gz
Algorithm Hash digest
SHA256 36ade76e7414aac60f3c554be433fd679569197ab4c45b0eddaf20627a6c5781
MD5 da0533bfe23564df8dfd8921cc88b903
BLAKE2b-256 861ec70e81c6424d1c75f663a546e2841576b9299b5a88070391d0718e0a8d5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.26.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 3cee4165473cf0909b49665490681aab30a55d8d2e17a2754486e283646fa137
MD5 6beb9d56d7f75bac81caf583ff0f10a6
BLAKE2b-256 7322902bf0e95dc7787fd698ec0e95772385a84c44e4d60842a941a347fcb59f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.26.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f91bd737b9e654a882b17d0c7db7d0d2645506722f7b31528bf6b0ed317159de
MD5 8fa11be3b825f818c13a55be1b29c4b2
BLAKE2b-256 55f43271afdbf2e9e8b4ce4b308a4ac42efa08768bdba4b01a3efe279abe090a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.26.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 65758cae75982ddb00968bc383e71164f0685bc790bc48269d72e442c5e52ff1
MD5 5a0e96ec71f9c73fe18ea24c956a6297
BLAKE2b-256 2bd16668bed1b10561571a5e070588c6303447648a3e60c2ca34cab8b442f45a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.26.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e640e696acbc737540add0f7ec9e91e59b276641e622ba2ada31b6fd7b88d535
MD5 0c75a0581c07929f07637fcb7056a756
BLAKE2b-256 efa3c80b8f7575cfbc0762ccf3396c1ebd759ee15392917e928ee627da6ef562

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.26.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0385034f99d55ad1c1b83555023fb9aacc6ac912dd2df38273c97008c97b6674
MD5 4bee24c238227860592aff8e716e9b8e
BLAKE2b-256 9d349f78bd8625198d6cba97fa322e5cc6caf7e1c5bcbb00d17063da45b4bdca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.26.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d83d2fcc66465383bb93f56f732f3d5f31ef91799683ecd66ff1807819cd49b1
MD5 805a372e677bb319fc6ae9f2caee1408
BLAKE2b-256 98c7cce62aa569389882564def68bad0e336eccc95500b740776cd8a3778911f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.26.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7eb536e0c62b9ee79afbb271fdb70a70dbff6fa6c3cc346b8b3e17a5bc373f71
MD5 0432eb5cadd6dd85b9ce6f80adb5e1aa
BLAKE2b-256 a64ccb09d7b677f50a3175cff70eb108e05c31ce71a568ad68ba828bb6358fc9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.26.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 40afbeca49876d1abea001c13e23fe5c33271ca2beb077a06ac7567689933d21
MD5 7c684810297d37596551602aadaa817c
BLAKE2b-256 1f976cf6a20cc2695a2ecfebe0339e10344980f6f73ce32515a3c06c321f87c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.26.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 71514326b4b322d2019555ad3b2d81e5771a4a1e1c3c1f1cb88538c19f36978e
MD5 71bab4c1ca157111e732ce235e4aac14
BLAKE2b-256 3649a10e0dbc86fad679958e4fa9a08ffa6152978be0ede7ccdd7e081f5bc49e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.26.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1e090c5aeaf3b71c344b9be9a3fff891e7351cf26eec7c0cb0c559a276c8d491
MD5 45f4488878ec9caf6fccc7a0161c19c7
BLAKE2b-256 88e17ae7a9f3220acd8c27e65e0cc98f3f000f23f70720a351438d7915af5280

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