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.0.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.0-cp314-cp314-win_amd64.whl (262.0 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.12.0-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.0-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.0-cp314-cp314-macosx_11_0_arm64.whl (351.5 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.12.0-cp313-cp313-win_amd64.whl (262.6 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.12.0-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.0-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.0-cp313-cp313-macosx_11_0_arm64.whl (351.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.12.0-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.0.tar.gz.

File metadata

  • Download URL: pyochain-0.12.0.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.0.tar.gz
Algorithm Hash digest
SHA256 5e140df0d46e8608bb70e028cb43e26fd2d94c8fa1e0bb69c538184f51d1aa9a
MD5 afd9a31c8e22f0237f58b37d04da04d7
BLAKE2b-256 87934c7e04c3fa638bd526677a4cd7deea81d35c602915e128e481e1c0f25352

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 4815e5af8aec9ac0869f6a1e5d2f7b0aa9c8c94707c922fe9ce2990396ac5589
MD5 147df7fb1d912afd0daaed96668229bc
BLAKE2b-256 9da62f8ca2cf183ab9d60a847b02ca5f5ec38dad5ff3c409684cfbcf087fef71

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f279a6696d103bcb629aa3befbc60c5daa78c178b6d4ecd7c93ef01917ba11a5
MD5 2595c9c2ce35c9cfed96700a1c4199ff
BLAKE2b-256 a7fded532252136015ccf9d0bb90c7971b5eb0f43e9f2cbdd883aae84b38a045

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b13bfeefa6eb9e283661cc28e7b12fe5bdd0d8238834386ad83571c1f7759e8b
MD5 33d3983699b21a367270a613d6a6d0a7
BLAKE2b-256 75e82a749e5a8e327b3464749074956fd89cbd2b7e6300e2ac4a4223a2b93766

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b3653cbdd5ed951789427433f3c22365fb610ed3d88bb924857759380c18da86
MD5 3300f9395e3421216507663df3892372
BLAKE2b-256 d536c998eeefb89830d333d16d7ff409e7fb6fd8cbba8aea7a7700ffc03a83bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 69a00012ee14c80942c1137e903729c3c85ae1d67f09fb331c9f86e2f86ea2c5
MD5 51156fbab1e1ab0080e839a4a5fcb984
BLAKE2b-256 baa15fbba32a0e92d6d206c0d637be6649df0def31dafad9afaa820afa7dce80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 5c1841c38eac7d1a410b0f2e30f456cc2f133619f3304267b507527eff472840
MD5 1150ea0ceb456718c30246ce6aafcd6b
BLAKE2b-256 00f7c29e88277f715cb7c0bc2cf47fc74fb6cdb0044073391fc3450d1dd2b7e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 10231cb38a756671a43c502623697f08d15920043e9cf19f09ae73eed415ed66
MD5 6a2aaca275766105dc3d3c4a60bb380f
BLAKE2b-256 9c4367fa9fa291d817fc4a6bfcc42cafcdf78c2263a52427cd795c06f39c8238

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a6fd5e1f6c62f47dfe2d447dd689ba70b9c7660b99947ff8706b2034ba901749
MD5 618a14aa9e5adce5ccc3ca2d7a69c515
BLAKE2b-256 c34e6a5c5003ba5580755524652deb8795d4ca6535ba756ab7b21639d3505bf2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ef809c7bd9ac94cd0de90fb9040b53880ad43f2226c9dd7b405efb982b5fa8ff
MD5 eea21d727e01e9d2110dcffd1b1f2944
BLAKE2b-256 677f056683d41e6cd651626d4d263fbfea48970def4e7c3b93ffb2efed069084

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.12.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 65ae22e99592768bef9f825c604c22519c453156dad2ddf309011054c08531eb
MD5 ba2bfe5c8fa70a969288982faa58f27c
BLAKE2b-256 cd835839c74f4aae80914588b4b882cf37c3b8f599357284af80452d3f85834c

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