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.

For a quick overview of the core types and their relationships, see the core types overview page.

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.13.0.tar.gz (63.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.13.0-cp314-cp314-win_amd64.whl (263.4 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.13.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (378.9 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.13.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (379.9 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.13.0-cp314-cp314-macosx_11_0_arm64.whl (352.6 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.13.0-cp314-cp314-macosx_10_12_x86_64.whl (367.9 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.13.0-cp313-cp313-win_amd64.whl (264.0 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.13.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (379.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.13.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (380.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.13.0-cp313-cp313-macosx_11_0_arm64.whl (352.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.13.0-cp313-cp313-macosx_10_12_x86_64.whl (367.7 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.13.0.tar.gz
Algorithm Hash digest
SHA256 f2a2697267d7fb237b50536ccfa402e1b3cc6c9dee217462dbdd457250f0fb3c
MD5 aa440fbc48afb595c818a398dbb4103d
BLAKE2b-256 0a782f2c298feba4e48c8267408f06b371daea76b0b18a94bd7ce762836150bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.13.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 d744fbb980c919bbce51c09a276b45ca221e83972a0b98ef1d05751b0b51321d
MD5 a4ae8091270a527c55ec707128c59015
BLAKE2b-256 ecce099f711b5ef60dac15c665760426f9fffad5c5046ddd467f21e87c0206e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.13.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 436fa2956ba6114b13bd7de40ed78aab9f6ec858a96d48490602c19756de0937
MD5 98f143d343ac377cf7b58a74e965000b
BLAKE2b-256 6aee75711955bc8dd40a5d7cfb508c81b5e861f7e3da8ca14cee6ea231506a27

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.13.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 dba90b4d5e3090a5682c54113b9ef1f531c16d69a35f8437f466bdb1d19fb753
MD5 0fb2fcea130533b9f98d571c453791aa
BLAKE2b-256 4c84c807c3d549871d216b9500d440eff1a9d6a6f40e346b38d82f73d24f1c50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.13.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8de8be8f1b4d285695febaf85d7f7742cd2c3610d3ed7fea29edd66d81634210
MD5 2e014cb1fa5350cd6e506182b15e223e
BLAKE2b-256 1b2a7e9e7487aef65fab8bdaf360e3e81cd02145b2fa70dc281783070827d11e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.13.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b447415d11f9cf8c02b18d2b12a590ffd1f4416f2ae7f9d52444e6b1ee7d445c
MD5 18cff3d23b2a6c351f2c0cecf1ff19a5
BLAKE2b-256 e6fb6fac63cd5e1405edec0cff7ec000a1db28d4281dc91bfd12e65a275b0795

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.13.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 914e6f8b6e5b39f4704810dcb56ac7cabdd24f389975a1fe4f9995e0703c34c2
MD5 1378b342c3a2247c8ec8612a34ee0348
BLAKE2b-256 0ba2ad0f811c71778efceb5e6a07b03a183304613792f2d477bbe13eaa58640d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.13.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d5413d2c1edce6c6a2be3119e4c68da2b4e1aee75381b5bc628a7a8ff9c34097
MD5 547b6fa7bff180574a3ef32bb2745045
BLAKE2b-256 e674041cd8f3f55f7e47b9fec86110f8081127856767892f96f55cfb031371c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.13.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 bfd4334950307bb0a3f453538579ead4c3949872cd40f6fbf68bf010d0ea9e5d
MD5 335ad4094b2f9cd648604bdaeb8802fd
BLAKE2b-256 e84283019fb9e2df8db72585aa27cde8d9ae2d68f6dcdfb814d18f006eec98e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.13.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9f4615b70b7c0d80df4ceab19f9230bfaf12f3b0e9f2d268d2fc38a14033dd8d
MD5 affdc5848a4f22b8755f82d0ddd70bef
BLAKE2b-256 b13a0d86aaed5855f8d944911523c8f841e7f39b0d359cc14fa1643339ee7004

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.13.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 540c756e60196cd117ca6a79819641bd2775c5a6d4fd85ec6817f9da0999a15e
MD5 0d4ad39cf4fbf42682fe101edc4eab89
BLAKE2b-256 4d58bd262b995f228358dd24389f6550da73da7fc0dc408bbabab8bbc99f6515

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