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.12.1.tar.gz (63.3 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.12.1-cp314-cp314-win_amd64.whl (262.0 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.12.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (377.4 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.12.1-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (377.6 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.12.1-cp314-cp314-macosx_11_0_arm64.whl (351.5 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.12.1-cp314-cp314-macosx_10_12_x86_64.whl (365.8 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.12.1-cp313-cp313-win_amd64.whl (262.7 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.12.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (377.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.12.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (378.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.12.1-cp313-cp313-macosx_11_0_arm64.whl (351.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.12.1-cp313-cp313-macosx_10_12_x86_64.whl (365.4 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.12.1.tar.gz
Algorithm Hash digest
SHA256 966d947138db13ebc6596cef489ea446ec36587f86f2fd28bf6995353c99623f
MD5 269927ee23d235ba410a62965b4e88ce
BLAKE2b-256 1a6abfca2f475b03c60dbd5269dca4c2ee66a5e9a20b3b418433da6cd69531de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 1f4fa199d8a66cc3f068b5276d2dc6ad41443b3d75616953448ae08a3bf9cdd6
MD5 38b2fa0657466bd63d856053c556ca09
BLAKE2b-256 8f6a635304cc378cefae237036c03c042181cccee261210aad1ac6ef84abb692

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9d720ee3e0f083642672861eed5cc6fb0aee8449643ada216ebce7e1b4042261
MD5 db0be08b60f0dd60238562b49fcfd24d
BLAKE2b-256 9e4662d5a6e85ea6fd9767dc83e832a80ac4ead5109f26d60e318f15a5fe456f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.1-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 13ab3375fcb982f773bff962ba119be68b3c78d33e4e4ce9e170edb007ea775e
MD5 50e3df7f02930043dccaf7577e01de7d
BLAKE2b-256 be226f094edbf020ca7353775a95778f757fae35c26bbbd1a195e2af6324fd05

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1e21e2407c15f0b9d587d041b3b024774fb92b2464e51ff999003f3754d21ca6
MD5 1c0c2accb0bf960a11580385b62d57c5
BLAKE2b-256 73e1c78d093dc68598e82892e11a796e56a63fb82693a7fdd3ecec8fcdfb7eba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.1-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ed3a708eab669f5ff62b73a78d2a38f989f245a09ffaecdba45786659436cbcb
MD5 0295c19afee8c09d0a32ad5f0712b488
BLAKE2b-256 0c796d283301be2f06f8df5682bff72f292f75f9cb5b437aa3004be7d5607c1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c0d42f8287c8a95b933f4d8fa5a6957725744c23e8bfb8bbdf2789903fec754b
MD5 802b6995bda9c264a22641d3b6591ecd
BLAKE2b-256 f12446301b4b8e06e90980f10d38641e3423b03b3654df25e68c385228932e78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8fd514f94bc406efb056cb81ad479deaecf302d724e313e71e03aa2a3ed7b8f5
MD5 32f692d4245335c6dcc7d48d8b4204b5
BLAKE2b-256 cc2ca5426859be463d8fa2c46129863f90702267c029a25750e254d4a843dce5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1bf942b2048fa59e584654a71a2c0501246297e209e324d23583479541a34cf2
MD5 88d511919a76971a2d053760052b29cf
BLAKE2b-256 6e04d326698b2c0674aec1c414117da77d3fb08b13efa893adf2a1bf2bed7c55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d940edf5e5635d51580ae111d64d6c58246a1a42802c737658018665f938961f
MD5 21f0ae386b31e5e6a8f3e333b830dfda
BLAKE2b-256 702966eddccb70b5a206e1ecc24973551d9a1797786b81274c2d804266296a6a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.1-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ce44afd5771353a767ed0dc2356876306f4d817e1cf6ff0e77ec223434cce987
MD5 5e6c395efe278b4dca589f5b783ab2dd
BLAKE2b-256 998bc1a36ce8aa57544fb661d8e636951559dad5a48584f7b7b76f3df4e59c92

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