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.11.0.tar.gz (62.1 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.11.0-cp314-cp314-win_amd64.whl (259.2 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.11.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (375.1 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.11.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (374.8 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.11.0-cp314-cp314-macosx_11_0_arm64.whl (349.4 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.11.0-cp314-cp314-macosx_10_12_x86_64.whl (362.9 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.11.0-cp313-cp313-win_amd64.whl (259.7 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.11.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (375.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.11.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (375.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.11.0-cp313-cp313-macosx_11_0_arm64.whl (349.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.11.0-cp313-cp313-macosx_10_12_x86_64.whl (362.5 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.11.0.tar.gz
Algorithm Hash digest
SHA256 2ea6edfac0b3e5ba0fec93b3ccb33df9b07c70921fe4a3b76c51fc94c543bd7c
MD5 9066727d9d76ce54b4c388dc94e26808
BLAKE2b-256 9914d877c79f5c00f6fb1573d999392dff376f8ce425bde3b18b526a81b0a726

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.11.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 3e1922c2deea1c8438e502e2f5c02d8b78433d57257bce8067c0dc11b2185868
MD5 e3a1112e74547494d8729900b3635400
BLAKE2b-256 7067b2e303c6c29c00fa55c2af3a54fc08185e7c473a5c983c8a10dc1c88c450

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.11.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fffbb78aefce9cc82770d2e1f989b9da655c9a44f421f964b4ff39c5447560bc
MD5 bab51b9760a6bdc436522ee4378ed065
BLAKE2b-256 88d7517adf84739e5480322fa1463973414e75c12e07c0c757f8f1bfe0a7622f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.11.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8466971c046e7364423241ea0f663d7a1a098ece23ca9deb61b385dd2c4d76c8
MD5 7fb41ca5a1c44fb1998223121eadbc19
BLAKE2b-256 49ad986d74903160a63c8b519c0f4c348fb6ac9f6cb17283ca159981707ff374

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.11.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f0048304ffb8a4ab71cb6ba520c13d39a9fe20b889f404ea355b0735d693ef0d
MD5 10323a27b2b883033391682aa682cb58
BLAKE2b-256 ab299c944f64feccd807ca40deebc0beb92946ce2b3f55aa4f5a436897ccbcd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.11.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c505c9289a4eab173a176e65f1df170489e26cd7efb006c033ca0d6385e61381
MD5 3c98fb144c72d16d4e61ffe47ee05723
BLAKE2b-256 c694f98f9569e8fc74b6ca983c3a1e05c3a350a8b4c2532d6a7c56fe6324cbe1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.11.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c9dff36b4906155d7719e0a6101d2da9e5de12de025105f59989580a7e78a72f
MD5 45a87b63933f7cc665d75ebb5d41a8de
BLAKE2b-256 2c42a8b32365c4105e93fb4e053a9a155a2a27b4252d192dbe26aa10ce3d47b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.11.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7151c5cd5bb95d8c8fdac3094881251ebfd5670f0cbd691d6d564c466a6e27c3
MD5 1daeb761964c16588dce787a1c6d2170
BLAKE2b-256 981bcb32bc220bbd6ec2ddfb9e62a6db5f762499da5864d40e7ad3cb8cec48f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.11.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8e56a8f825665a21e6e376f47736e444f7b69f9d61d1074f2b37a77aa65a62f8
MD5 8e8c1e3c8fe29e9a096c10ef385453f4
BLAKE2b-256 3f763430e31f25ad125591d08b751109596d83af2461265d64eb76884c66fa70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.11.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 216083d03ef6ed41ecd321f71c1537acfc90008bf80deb5d57f8d66b7a844ce1
MD5 5b27cd7e9b100d1a931506342ece14a4
BLAKE2b-256 edbacb47f83c1ee73b1a6dd715c341fac141d61f0e42b0e51914d37791481864

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.11.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 245dc41c29767efdc7af8870a61efdd2f08b07ef889b054cd727d7da3e1678bd
MD5 5e2c857a6347cc7554317a82fb72f0a3
BLAKE2b-256 a79a0ee3935de1c67a5f38073e0a7151433e6b41c6f4fbe1a0ac0eafffe33a83

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