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.14.0.tar.gz (63.9 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.14.0-cp314-cp314-win_amd64.whl (263.8 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.14.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (379.4 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.14.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (380.4 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.14.0-cp314-cp314-macosx_11_0_arm64.whl (353.0 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.14.0-cp314-cp314-macosx_10_12_x86_64.whl (368.3 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.14.0-cp313-cp313-win_amd64.whl (264.4 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.14.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (379.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.14.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (381.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.14.0-cp313-cp313-macosx_11_0_arm64.whl (353.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.14.0-cp313-cp313-macosx_10_12_x86_64.whl (368.1 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.14.0.tar.gz
Algorithm Hash digest
SHA256 402975b1871997dd54f0e7b190cd488afc8cb217b1d24a3c88ad9cbf05de2a7e
MD5 750c638dfe4f538725d0d06889af523a
BLAKE2b-256 d8b33e910c83cf89401f18e96c0af5f4a8651e5af63e484fcae22b1dc0db1197

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.14.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 ce7fb59b8c3859fd5f7a5a8891c3e61c98e4ca467ecabe383f83f921a258c290
MD5 b00fa7e351f89c0d1b2875845993e4c4
BLAKE2b-256 293db71adff52efee2229be3536e506489c31631495e404d856a6e36a262f551

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.14.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 92b92c0dd8a3cda5469029355a60e681ee7413e316b73f6ed42e309b55166cc3
MD5 0f5b65779b6b546a92bbdad3990703f6
BLAKE2b-256 d6667dcf899dd1d1f74ca2085788babc3145b1df555a35493752bbe9cbe96f51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.14.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a61c57f49600ba48a366bca5d01b140cec4039bd87b0330efefa022b1747aca1
MD5 828cc200e83b29f242388dd9859ab00d
BLAKE2b-256 9450a05a77421a403d7fa58d94d13c31a3d7e12746600ea99bf0536bb6f439e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.14.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f4c1bc0a03b94abcd0e2e6fa7aa170ffddee9810835d7af321b281b9826c6acb
MD5 47de848c4fb5e8db4a1faa772803545d
BLAKE2b-256 453b5ac41824092427cb220151cd6f6c8858339e9f158ab48d1fafe0393d4951

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.14.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f9a0f4c8fc83efbfaa8400bb0bb5181378d5587018ed7043890bb4a81a61a92f
MD5 b34d9577fede4f2fc0e075a9c1385c47
BLAKE2b-256 f81a5eb393b4a1c059c3edb31520722be82a2f84abf3e24c8f9f6f3b2bb1c977

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.14.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a93aebd8469e581b12e654250fb4561f6cf0212efbd27656eea0318c806702b1
MD5 36ee27d88b99b34013a4892e0425dde6
BLAKE2b-256 62d2dea2b30a07a985c751681f2d0ddf443f5d57d0d03c709f88f1b11a935e22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.14.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 613b3e65063d4785672c2f0d480a0ae66d8ea60b1eb99b78b9de78db33cf8ac6
MD5 285fb2783815056cefbe66ad9f5e771a
BLAKE2b-256 7cb075441ea72b20a45264c23bb45fe2419b40b07d651535877efae88a2b5a8c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.14.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4ef1d9cf5e8f9435b445776d50b36c394538c5540ad2ad7f33330e5f36085458
MD5 375ad011b1c808afa4060ed6ae014792
BLAKE2b-256 795f53990d964edb1ebc2b718c2033800423ab6c06f95d28db2c3033e6d9f723

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.14.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 84f6202dac58275239b5115ef072604c3e493361cbfca2130f39e44a4a070379
MD5 6f5f44d3e2a7474b42e0f41108d43ba4
BLAKE2b-256 bfdc56b9265c24d65edeb3cbdc0ce909951d5045f60aff9ca7ba1740f932006d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.14.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 25aaf64b4d8ae7d51f9d918ee0a5880192b0b71ffaee2674c9a8e86e6e70609d
MD5 cfa6d070a40ce22c8f9d0f3400cb53cb
BLAKE2b-256 8a43f3aa58f93aafb77d91b2b26cc8bd85afa89ac3c5bbfefb04230b5267d04c

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