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 provide a set of classes with a fluent and declarative API, to work with 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 functions from cytoolz (Cython) and the stdlib itertools for maximum efficiency.
  • 🛡️ 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 traits — Extend your own classes with the methods in the mixins provided by the traits module.

Installation

uv add pyochain # or pip install pyochain

See the package page on Pypi

Quick Start

Iterations

>>> import pyochain as pc
>>> # Lazy processing with Iter
>>> res: pc.Seq[tuple[int, str]] = (
...     pc.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()
... )
>>> res
Seq((0, '1'), (1, '9'), (2, '25'), (3, '49'), (4, '81'))

For comparison, the above can be written in pure Python as the following (note that Pylance strict will complain because itertools.starmap has not the same overload exhaustiveness as pyochain's Iter.map_star):

>>> import itertools
>>>
>>> res: tuple[tuple[int, str], ...] = 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,
...     )
... )
>>>
>>> res
((0, '1'), (1, '9'), (2, '25'), (3, '49'), (4, '81'))

Result and Option Types

>>> import pyochain as pc
>>> def divide(a: int, b: int) -> pc.Option[float]:
...     return pc.NONE if b == 0 else pc.Some(a / b)
>>> divide(10, 2)
Some(5.0)
>>> divide (10, 0).unwrap_or(-1.0) # Provide a default value
-1.0
>>> # Convert between Collections -> Option -> Result
>>> data = pc.Seq([1, 2, 3])
>>> data.then_some() # Convert Seq to Option
Some(Seq(1, 2, 3))
>>> data.then_some().map(lambda x: x.sum()).ok_or("No values") # Convert Option to Result
Ok(6)
>>> pc.Seq[int](()).then_some().map(lambda x: x.sum()).ok_or("No values")
Err('No values')
>>> pc.Seq[int](()).then_some().map(lambda x: x.sum()).ok_or("No values").ok() # Get the Option back
NONE

Documentation

For comprehensive guides and examples:

🔍 Types Overview — Roles, comparisons and visual relationships

🔄 Interoperability - Converting between types

📖 Examples & Cookbook — Practical patterns and recipes

📚 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

Key Dependencies and credits

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

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 development, 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.8.3.tar.gz (59.4 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.8.3-cp313-cp313-win_amd64.whl (229.7 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.8.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (347.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.8.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (348.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.8.3-cp313-cp313-macosx_11_0_arm64.whl (323.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.8.3-cp313-cp313-macosx_10_12_x86_64.whl (332.5 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.8.3.tar.gz
Algorithm Hash digest
SHA256 9bb8ac77a8f72638f2ec022b210e2ddc1bf9cd16958252fb2e1ce80dde146886
MD5 22ca2a8f9794b7e7753a14efebc4115c
BLAKE2b-256 8880ea5283261cd614683d34fa618d01bc499261009e0b031ed664057b586f36

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyochain-0.8.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 229.7 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.11.5

File hashes

Hashes for pyochain-0.8.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1d4b91f650503bfc5460d60c9116a01665fed499828b2527d10a8b90a83b6970
MD5 377467b2962d2266857d9a202f320b35
BLAKE2b-256 613438b28b3df28a4985d4e14ca237281dd8a8d86e33096f334e04c059fc82a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.8.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bf1d0737abde73f7fcfa8752220c482452a95bf26d0f71fa8b0f5ec27d3e7244
MD5 2952751fa8c70bd9ce5190d173dcea65
BLAKE2b-256 56d82bf2dfae8b66b29dd4f2c85485b34d50970c58cfc862eb998b2e214f0b28

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.8.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6065d758ff77c4bdb60bac01a53f7afd3893bea55e26b65f7e1dfb2f72ba753f
MD5 8d235d3af8044bb6a2e048611ba18bfb
BLAKE2b-256 f91daf04e38ee6601b4ccb9933ddb9517f15ac3c9801952ea1eb7e281a6ed86e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.8.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ae8e46166e3eef1d1cdada14fae4da403e71da4a8996292991d3432f70613ddf
MD5 c414b21c11cd92840c1d1b5e8fae7c21
BLAKE2b-256 13f596ec6c6f6c0c3c086f68ec0d077e909431c0988c802c53813fd02a547a15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.8.3-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 bd8d1e9f99a83604537237d21f201eb15efda397e697786303a78d16ad1f8a22
MD5 f0afea4272e543ddbe38477066be806e
BLAKE2b-256 f450ebc131d6fb3a3f6cdb54b003a1e3d6b9c000bdcdeacea2a8270dede82a50

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