Skip to main content

Method chaining for iterables and dictionaries in Python.

Project description

pyochain ⛓️

Fluent method chaining for Python.

Summary

Inspired by Rust's Iterator, Result, Option, and DataFrame libraries like Polars, pyochain provide a set of classes with a fluent,declarative API, to work with collections, handle optional values, or manage errors.

Quick Start

Install the latest release from PyPI:

uv add pyochain # or pip install pyochain

Import the library and start chaining methods:

import pyochain as pc

# Lazy processing with Iter
res: pc.Seq[int] = (
    pc.Iter.from_count(1)
    .filter(lambda x: x % 2 != 0)
    .map(lambda x: x**2)
    .take(5)
    .collect()
)
# → Seq(1, 9, 25, 49, 81)


# Type-safe error handling with Result
def divide(a: int, b: int) -> pc.Result[float, str]:
    return pc.Err("Division by zero") if b == 0 else pc.Ok(a / b)


divide(10, 0).unwrap_or(0.0)  # → 0.0

Philosophy

  • Declarative over Imperative: Replace explicit for and while loops with sequences of high-level operations (map, filter, group, join...).
  • Fluent Chaining: Most methods transforms the data and returns a new wrapper instance, allowing for seamless chaining.
  • Lazy first: All methods on collections that use an Iterator (think most for loop) and do not need to materialize data immediately are in Iter[T]. Only methods that directly returns booleans or single values are shared between the Iterable classes (Iter, Seq, Vec, Set, SetMut) via their common base class. This encourages the use of lazy processing by default (since you have to explicitly call iter() to get access to most methods), and collecting only at the last possible moment.
  • Explicit mutability: Seq is the usual return type for most methods who materialize data, hence improving memory efficiency and safety, compared to using list everytime. Vec is provided when mutability is required. Same for Set and SetMut. In python, set is the "default" set type (constructor and shorter name), but in pyochain Set is a frozenset.
  • 100% Type-safe: Extensive use of generics and overloads ensures type safety and improves developer experience. The library is fully typed and autocompletion is a central concern.
  • Documentation-first: Each method is thoroughly documented with clear explanations, and usage examples. Before any commit is made, each docstring is automatically tested to ensure accuracy. This also allows for a convenient experience in IDEs, where developers can easily access documentation with a simple hover of the mouse, with a guarantee that the examples work as intended.
  • Functional and chained paradigm: Design encourages building complex data transformations by composing simple, reusable functions on known buildings blocks, rather than implementing customs classes each time.
  • Interoperability: pyochain types can be created from and converted back to their Python equivalents seamlessly, allowing for easy integration into existing codebases.
  • Performance-conscious: While prioritizing readability and maintainability, pyochain leverage the cytoolz (Cython), more-itertools, and stdlib itertools (C) libraries for efficient implementations of core algorithms.
  • User-friendly API: Method names and behaviors are designed to be intuitive and consistent with Python and Rust standard libraries, reducing the learning curve for new users. pyochain also provides improvements over standard library functions where applicable, such as Namedtuples for Iter.enumerate(), Dict.iter() (dict.items()), Iter.group_by() (over itertools.groupby), etc.

Documentation

For comprehensive guides and examples:

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

Key Dependencies and credits

Most of the computations are done with implementations from the cytoolz and more-itertools libraries.

An extensive use of the itertools stdlib module is also to be noted.

pyochain acts as a unifying API layer over these powerful tools.

https://github.com/pytoolz/cytoolz

https://github.com/more-itertools/more-itertools

The stubs used for the developpement, made by the maintainer of pyochain, can be found here:

https://github.com/OutSquareCapital/cytoolz-stubs

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.6.1.6.tar.gz (41.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyochain-0.6.1.6-py3-none-any.whl (45.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyochain-0.6.1.6.tar.gz
  • Upload date:
  • Size: 41.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyochain-0.6.1.6.tar.gz
Algorithm Hash digest
SHA256 4e0cb6d86b5adbdc1f055ab7331e129d1075d0712cd045e22171acc449355a49
MD5 eb0f30394f2ef407d26762c87af5d986
BLAKE2b-256 3b25b9bac9ea466a9341a5bd546eaf95ffe24076d53f4232da7335977672e60d

See more details on using hashes here.

File details

Details for the file pyochain-0.6.1.6-py3-none-any.whl.

File metadata

  • Download URL: pyochain-0.6.1.6-py3-none-any.whl
  • Upload date:
  • Size: 45.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyochain-0.6.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1822b5684da86f4efb15c4a243d6356017b527c65f2f3d68ae5b2f8ec961b16d
MD5 9c3705decdeb43113c071108d6f58561
BLAKE2b-256 3cc07d5b06e3535c70004bdfe057d7f25cc3d2acc1b2e37396c25ea99e9f228b

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