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.22.0.tar.gz (69.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.22.0-cp314-cp314-win_amd64.whl (287.5 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.22.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (409.1 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.22.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (409.0 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.22.0-cp314-cp314-macosx_11_0_arm64.whl (381.3 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.22.0-cp314-cp314-macosx_10_12_x86_64.whl (396.9 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.22.0-cp313-cp313-win_amd64.whl (287.8 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.22.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (409.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.22.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (409.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.22.0-cp313-cp313-macosx_11_0_arm64.whl (381.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.22.0-cp313-cp313-macosx_10_12_x86_64.whl (397.1 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.22.0.tar.gz
Algorithm Hash digest
SHA256 a20892fd20745f4d482164a3b73b574300f3435cc853b55a02d4d98368a16666
MD5 a07c051c3e5f898abf384ec7c9e25a5f
BLAKE2b-256 17f411bd3671344a398635ceb2b387f234b8bc3fde65f6f9a7d398d3bb7c3f32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.22.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 a8ec74952f7316e5c97be461c37e6fdec40e32eaa7984a24f17c254741ade5ef
MD5 adc37a8c32a35a11d1fde773685d7df6
BLAKE2b-256 b8bcbe0c773aac5d9da659fb9c2a744346bce9795494aeb3d3fdd04ab46c4e22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.22.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 606aff49191716e81d2462ae0bec1e8c650bf9aeb64a5808dfe69f9f126389a6
MD5 f6bef5b204ee74d68872e4a73b6212c0
BLAKE2b-256 c7275e8e4937238126e58ca27f41383bf8ff143af10b68483f5ea64438fd7284

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.22.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7b38a6776a40d59fb04e1c0f059ebe8fd44e91bdc132b1322ed4f29d9cab32fc
MD5 2109cc603b1ce1f172542e5a0c7c7d10
BLAKE2b-256 7e72d1c92e3ce6a2d817e5261e1dbdbbdd724b63bac603a7232bbaa52d77460d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.22.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fa2fbee761a4636224bd95803357884452e4a2cc6b1c253e4cefdab7e807a11f
MD5 da6b367910df03f1f0bc1e9023933ed3
BLAKE2b-256 f137e5864a1a05a5a7d86ae03eaea22b01e59c36d1c21966181d60c575bb0ebf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.22.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c37a4745af8a9b2ae2e74ed3f5523819a147d9bc3a1157cdaf78141a17a2ff45
MD5 c73dfe3f9668bb94026f417a83808b18
BLAKE2b-256 7e12db7c50226c9f1f13d81b2cd871331d9da2ff161942a016a6d0719df706b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.22.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1c6bd22091da8d9c6768aae5c8c4fecdc5d61142997aa2dbedb2bef6d42e4359
MD5 07787985420b560e7fc51d37c3adeafb
BLAKE2b-256 181a961c165c603c2aae57f66a90ed792b4a738b01c9334765c565fd4935ffc5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.22.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6cf03913487966948b2838911785bb9b57a8188381ee76da739de156cf4d1276
MD5 e55151b2f6ac7ef4fd74c9968238e165
BLAKE2b-256 f1125ab6635fbfc332d16efa943aed0d49bdf29980b77adadd57c134451c3f16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.22.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 17ea71d04fda79ac5c4e967285c93a59b82810a5668ba98534e9fb19cdf39894
MD5 70f9d7c1139dd896ceca10fe59e0ddf1
BLAKE2b-256 5df6f4f2931ec853d5bb8bd6df1296cb38d3e3268fca4ce445b50d56e5baa767

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.22.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b600cc4a8bd1d9665e39e9fb4ca0b1c2ea242969af5f06579ba956f5ae7e4e23
MD5 1efcd356ad2b75aab0bfc0262b51762d
BLAKE2b-256 51f78c4a4c698bded106fb61c32e68ef190d87f298e2e18a2ca07376b0d4e6c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.22.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 01c30bbb43c2e7a1c04d51bc63389fe89aed64fef75c3d1be7cd23e6d6541d50
MD5 14dcd78b821afed3577e2472452f053c
BLAKE2b-256 4da44dc0c673be479e50920d51788fe767f2b3d36f4acc98c8e1367a967a2122

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