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.

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 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 traits module.

Installation

uv add pyochain # or pip install pyochain

See the package page on Pypi

Quick Start

Iterations

>>> from pyochain import Iter, Seq
>>> # Lazy processing with Iter
>>> res: Seq[tuple[int, str]] = (
...     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

>>> from pyochain import Option, NONE, Some, Seq, Vec, Set, Ok, Err, Result
>>> from pyochain.traits import PyoIterable
>>>
>>> def divide(a: int, b: int) -> Option[float]:
...     return NONE if b == 0 else Some(a / b)
>>> divide(10, 2)
Some(5.0)
>>> # Provide a default value
>>> divide(10, 0).unwrap_or(-1.0)
-1.0
>>> # Convert between Collections -> Option -> Result
>>> data = Seq((1, 2, 3))
>>> # Convert Seq to Option
>>> data.then_some()
Some(Seq(1, 2, 3))
>>> # 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
>>> data.then(_process).ok_or("No values") 
Ok('1, 2, 3')
>>> # Use new() to create an annotated empty Vec without brace and parentheses mixup
>>> Vec[int].new().then(_process).ok_or("No values")
Err('No values')
>>> # Create empty Set, convert to Result, then back to Option
>>> Set[int](()).then(_process).ok_or("No values").ok()
NONE
>>> def try_parse_int(s: str) -> Result[int, ValueError]:
...     try:
...         return Ok(int(s))
...     except ValueError as e:
...         return Err(e)
>>> # Type safe exhaustive handling with pattern matching
>>> def handle_result(res: Result[int, ValueError]) -> str:
...     match res:
...         case Ok(value):
...             return f"Parsed value: {value}"
...         case Err(error):
...             return f"Error parsing int!"
>>>
>>> try_parse_int("123").into(handle_result)
'Parsed value: 123'
>>> 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.19.0.tar.gz (67.0 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.19.0-cp314-cp314-win_amd64.whl (282.2 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.19.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (404.0 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.19.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (403.7 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.19.0-cp314-cp314-macosx_11_0_arm64.whl (376.3 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.19.0-cp314-cp314-macosx_10_12_x86_64.whl (391.8 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.19.0-cp313-cp313-win_amd64.whl (282.5 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.19.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (404.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.19.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (404.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.19.0-cp313-cp313-macosx_11_0_arm64.whl (376.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.19.0-cp313-cp313-macosx_10_12_x86_64.whl (391.9 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.19.0.tar.gz
Algorithm Hash digest
SHA256 beafdb26acc2962abdd521164337816541df8a5bee9353a3178c09aeea1266bd
MD5 8616bb3719da11f7b1409419a3b25601
BLAKE2b-256 aeba55e6dd9fc7454c8522f657f0772b019505d6699f8fc755f62985ddad174b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.19.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 1746fbc78e3a88db0c27aa00f10ff75180b098353b291646e36c4c849c9cd529
MD5 0c46a8fe4232f4214a85b3ffeb96cc95
BLAKE2b-256 6b41ac363e0640d8a49717ca2cfdf33db1206b348e2a649b1d7dbb966e5a9bf1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.19.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bc8faa9cd9b0d486f82ee0965bbd09032cb6f3049a9a1c2a88530c6b28984e1a
MD5 c92bb48c827a8a014f3fa1212fc05872
BLAKE2b-256 9ae5920230e2deee9bc13391d65d3d1b137d8878e502bb6d35dd45fddbf05d9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.19.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4c9f54a34c3c58acd7c75c059ed9ccd7073a7153deea65f016f4b2d93cd0c832
MD5 2a79e97b64d5a432ed43f7cec52b7a3b
BLAKE2b-256 020c9526c9b5a13e466a780c2639b306b131665230635c8080f0a9e6211f32be

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.19.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 113d5058f78cf8d894c4742ebc4b9a07aecdb30a1176055a4563c61cd5b90427
MD5 6bdfd51e454419aeec36fe80d86ab6a0
BLAKE2b-256 80985a06966d675410a5663a7cded7875ed05aa49da307b3185b32510c52aee3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.19.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 10fbdeef2941cc26c0787e45e4736a55ba50b103b831b10f32cdf2ccdc752bcf
MD5 ec4e6926c03ff0324aa9febc690be47f
BLAKE2b-256 9ae4a4177d874970ef0a8ef6714b079cce48d8a6b139235876547ebeab4208bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.19.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 86b83b542cab812cf9b9b77ff64e823da7abb320114de87d91bd5714aecbae20
MD5 f18f5b05cb7e303cd4a11c62a22f854a
BLAKE2b-256 7c4777e09876f1c42ffcda6b3742e08c058f9c131945a5a3b640527ba1d72204

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.19.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4372f6e65cac4f1779a48338fcbf66c6a69aeb60f50ca9c82c47e342393f4410
MD5 f6f52c07060a7f6c2949aebfc2c84fc0
BLAKE2b-256 f7948e10ea509a26648067cc611d319a048237c4a8fb8371a7b68badf6a17ca3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.19.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 60e4b33caf51a07f20d27665ed0d8c4125cbf98b5dda791e4ef94309027bb589
MD5 8a9e1ce9ad7f5191f990abe1071cd5f5
BLAKE2b-256 2018304c7cb51a026309d10224a6d26712a69c96eaae613d3e88b72c4d7e159d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.19.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a97da459beedf498d8935499c2b29430b4566302ed38d24502b710b393b1e2c5
MD5 0fa244a6458d8746e1cc6bf04b73f5c3
BLAKE2b-256 43432f96ccbd4888ca1a21b2b1febaead4d709482ad01536bceb470cbfc4ffc6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.19.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a022b19a5f8ebedd2cd9a37a61dba47f3bb05500c028c52a5359e6adcf9e4cec
MD5 00adaa5234eb85deb82678191b90b78a
BLAKE2b-256 8ab017e4afbe848f2946b9d08f515e710723efc8fbf5fa34c0551e55e97f2afd

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