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 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 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 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

🔄 Interoperability - Converting between types

📚 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.17.0.tar.gz (66.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.17.0-cp314-cp314-win_amd64.whl (268.0 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.17.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (386.0 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.17.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (387.5 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.17.0-cp314-cp314-macosx_11_0_arm64.whl (359.0 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.17.0-cp314-cp314-macosx_10_12_x86_64.whl (376.1 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.17.0-cp313-cp313-win_amd64.whl (268.2 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.17.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (386.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.17.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (388.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.17.0-cp313-cp313-macosx_11_0_arm64.whl (359.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.17.0-cp313-cp313-macosx_10_12_x86_64.whl (375.7 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.17.0.tar.gz
Algorithm Hash digest
SHA256 472ae3264ad5c38c369a7a44a34586715366a899c5ae4db438b2f9de7ac55dd4
MD5 c02da22899f4d5f9dadbd3b446339953
BLAKE2b-256 4c53895fd6f1ac0f224483a4474c6f838a55cdc33d9525e75e7f0bbd284d2a14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.17.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 13fa2e6638d118c9e0bf6176ac6c3747301acebde0ec2d44eb26a59b04e5e3c6
MD5 a261ed4632e7f2f8db9a08a18e7785a3
BLAKE2b-256 a1d48304baf811f701a33e5ce4d1fa2a9562e53b9ea9be891fccd0cbffc5d579

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.17.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f30f10dbb508b6221455248827f50a79a5dfc01cd2b7024bcb6e4a4c5fe7c0ee
MD5 2af1f3c43844d0e0fba85039d51340bd
BLAKE2b-256 7dbce3681b3a19650958b39428343560e728172c88bb14296d66284dc6f44148

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.17.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d5689f4114a908e6f867c61135da449891321b1d4d92a3a53345ef6921fe0a35
MD5 d49dc2edf4a2c9992b87d8d50a519b39
BLAKE2b-256 849a638afad2a8b05b766937e40bd10cbb6bc10afd7bd3edcce93488cefcca13

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.17.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0d0a4df435b0311ed8e6822075b73edaad0daa76b6843eebb7c79582e3852ebd
MD5 f9ed3dc69006ed4f730544c4e78afa2e
BLAKE2b-256 1a71680cd53b965b2204013ebd37db7dde15ad3b809237125c68106957f1f260

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.17.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f1349b6c457345a2ff54632d0242799424253ae307ca337b1b6be023c707e312
MD5 b2dc3ba203a4e90c33cb36f29e5adcc8
BLAKE2b-256 ec29e7ec173b396ff720a8b5750005ae692bd7d83f71550a4f548f3495216340

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.17.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3cdd000f3d5c832020488642c4e983bbeb37c91fb724864269611369044f1e7b
MD5 23530fb296275a0c372eda844ace6791
BLAKE2b-256 d88cfd2e7bcfe368bbc60782c14115c4482baa3c7bd677501af8076b8449428c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.17.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ec95b34855dedef615f486bd18bd3b4e3ce38e2847cbd1dc9946a60b52026868
MD5 d9ed59579f4012996fff01ec58162218
BLAKE2b-256 75fe69da5183e36c3dbdbaf11113afa3ac5bb44e7ce8cfdaad558df475e75503

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.17.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fd749eea3c7c4666753963372b049c2628d578eaff1a7c7c6d32f010441813f8
MD5 87c1ade24556514f15ee15cb1194a43f
BLAKE2b-256 f7faa77c24bfa9ff141ba0941169f17a8fbf8e09b2bf282da07ec2ca7adfe0f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.17.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 86943116364b8ef7e968bf2fa75e3707cb1a39b048d9c4d8c874c2efe35c5d95
MD5 a9d8527717ac89e7f6538add5e4b6fc8
BLAKE2b-256 ea0e8e5896e6fe8e5525a04798a7cd6eefcc5576c895fcb843235916c903e226

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.17.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 36999dc1dd83ee845915481c48a8eb8ca7e1cf16200180350f6837bb76c8b7e0
MD5 5ec989d033d904fb3f9b94e3f223bd43
BLAKE2b-256 39c34cacdf3ca8001240b22d1016b780279574fa0ce0c0195f79a32858f74e8e

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