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.10.0.tar.gz (61.7 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.10.0-cp314-cp314-win_amd64.whl (258.4 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.10.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (373.8 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.10.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (373.3 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.10.0-cp314-cp314-macosx_11_0_arm64.whl (348.6 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.10.0-cp314-cp314-macosx_10_12_x86_64.whl (362.0 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.10.0-cp313-cp313-win_amd64.whl (258.6 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.10.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (373.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.10.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (373.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.10.0-cp313-cp313-macosx_11_0_arm64.whl (348.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.10.0-cp313-cp313-macosx_10_12_x86_64.whl (361.5 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.10.0.tar.gz
Algorithm Hash digest
SHA256 31063cec335cc0684fdc1f964d9e6f4c9ec1fe4613f64a03d3d7af6c5b115583
MD5 cd572722f9f384b5884609a6a44a2bb2
BLAKE2b-256 9906f69adc82375366ad86a3e58610b0c5938b1733544121fb1d67dff09343d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.10.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 149fc5388c5fa31b8e1cfc8c6c273e34fad30ad1a4a8ac2702b69853248b47b8
MD5 45cce3bba7fa71e3cd59aa25da883084
BLAKE2b-256 1db1ef018ce9193aa025f1ba34d4887b496ff5bcf8ccc48490a703022cf46f80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.10.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5f95b56da9a6a3658240276b3cd6fb1453ece5b75ef4ae88403151b2c7e7c90f
MD5 edb03c87c1b61f3747697e1c29f66051
BLAKE2b-256 0e784de196e2feca4d19c3040515cbcda408a41a989752dcb7ff50b9cb156a97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.10.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 79ec0d0df71bdd8f67e5c92f574fa6af84dc544f2fc0eea734250b2393f99894
MD5 4d7fb27f910be68471bb06496e42ceb8
BLAKE2b-256 245587038b52ec5366f8578929bf3ed10918dfb53c1f777423aed2263314d4f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.10.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 79a88a40dbf706230ab0f254d0ac760dbc6953f9d278869acb3722c98ff53e95
MD5 cbf328fb45e7991b7c2f16e0199f977c
BLAKE2b-256 599ccc732b7d678542ff8d5d959069cbfd7645cae629ade864b0260de718f412

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.10.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 39fcbc88fe1f995f524c135e9b0b056c57628415db96a127c842471f8f06a1b8
MD5 6ca4e44ddcfcb0dd1c9f6e365e9017f7
BLAKE2b-256 e2a42c50c8bb5b23461006230d5c18be0cc64811b4a996f99ba5962272f29c1d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.10.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ede6ccde549e51dc6c7d1b12df33da81663aacd2e179afa1a2f92552666a983c
MD5 4ea83e920fd344e4e43cd2dc2f53d0c3
BLAKE2b-256 804f996e566aeac6fa47808e7ac7d34ae978fbab37541c64ee1137ea91c91ca9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.10.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4ee5ab8ac0d9a451aa8c3468962e766fe5f8245a1601738e1068fb015b943468
MD5 2d926758f1d93891d0ed509433484885
BLAKE2b-256 e18091eaebad61fb2e7b7735ae6e825bf19a4858a0798d1736bf8ff2125effe1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.10.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f608ac1e90cd4ecd92e815f8e5e29a3ad94d11d11ee8bdf5664eda661719ccfb
MD5 4fe7beba79355ec66f14918396b624c9
BLAKE2b-256 df3392f77a7e255738d4836e76f7d45acf80c549af6193c289f6992074c45a21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.10.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c046d46f64b91a069c3867c66a325c6bcb37536f6066d853a8bfe249f02cbd0e
MD5 0477ea8457ddad2f7e3d7a041addc523
BLAKE2b-256 c1304239ad6516cbaca06edc8eee1595540a0ee4da18fce3bdc28ac0bc03dada

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.10.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cbe60059dd304bfcf35a1d4745a058e7813890347b0d7464d17c742cd4bf2fd7
MD5 e759a6b38e86a2f5cc96fe211b76c707
BLAKE2b-256 b3eaf368420a29a442a038a44f1166971ad084300181270b51bbb77aba72c1a3

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