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.9.3.tar.gz (61.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.9.3-cp314-cp314-win_amd64.whl (261.6 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.9.3-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (378.2 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.9.3-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (379.5 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.9.3-cp314-cp314-macosx_11_0_arm64.whl (356.7 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.9.3-cp314-cp314-macosx_10_12_x86_64.whl (367.4 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.9.3-cp313-cp313-win_amd64.whl (256.5 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.9.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (374.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.9.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (373.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.9.3-cp313-cp313-macosx_11_0_arm64.whl (352.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.9.3-cp313-cp313-macosx_10_12_x86_64.whl (362.6 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.9.3.tar.gz
Algorithm Hash digest
SHA256 72a9ffb426632f2de69e615dd0060e7c90063cd820ed47e583929e16d50da865
MD5 fa0611ee75dbc3b3756a3032eea26404
BLAKE2b-256 6baf5aa790bf9197e09f44302ea1212e558d61009e5f08ee80803b0af95294c7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyochain-0.9.3-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 261.6 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for pyochain-0.9.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 738987b8683d8b66e7a012c0b31cebf601834ed5ce219d429ba904c16dea0eb4
MD5 1f811e08b5883b932422b6203c07d8d3
BLAKE2b-256 8c6f42de748e946862d06f3e86c28d35e34b786b03e8af1550b768d9124592a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.3-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9647aecc29f75e06fbd79a5a5061750e6a8f1801a7bad4ed1b35e25ba3df9d0a
MD5 106c6ed5d048c73a92238826f89dd67e
BLAKE2b-256 ff2a1fd26d66290023ceb7cc694a65dfdc10b812f3d7434ab78f0735fc12be5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.3-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8790d6127c5a00717bbf1d36c672e9fb45b7aeb2ce76aa1ccbdcd2d638e3ff00
MD5 c2103e71763d137ad62d58373f0822e6
BLAKE2b-256 02fe2a8312242f35ffad9c6f625330d0c533342d3668652d71077defab5eb25d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.3-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e1603e0345a058e3a2b0a4de24c7b9fbbca351613bdb133ce5325c60e5d17984
MD5 f5d2979157de1399f4425e4645d03799
BLAKE2b-256 fec181d4ef1d6e06d312234e4be3b6c9a08128e30be784386d4a5bcecdaf45eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.3-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1b5f0eef0ef0b40cdea4f52c785c9470e9033383909dbdd6bd7bbd375baf1eb0
MD5 1f539738978b5096b685ff1223374f80
BLAKE2b-256 b60e131e2ce56e0a8a83cc06b47e351b24d3908c395d56980ea238f2bd7cbed5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyochain-0.9.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 256.5 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for pyochain-0.9.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b80f99377ef7b3f27fb8f22584567fece2ec95df81fc43a9bbbda7355b0d184c
MD5 26cf6c466eae6f5cb844b7ae57e61388
BLAKE2b-256 73aafee2a33938c938dc978a62888b18f5bbc709de63f9807934ee6256c1e3c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2796f6203b5af090b76c91d4c8d5cca155c9924a858843316a0dd3874bf3e8f9
MD5 6e92c305c50f4bfca9a087057733152a
BLAKE2b-256 2072f28a4a1da48d8e27d3d1be84b0c6f00946f01afbb8fe7bf7f00c525956e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 16b947c91fd22940122d2f5d4278bd9d6ca90ca4833cebdd1c71265b488ce665
MD5 a2c57fcee3e1a6e5a95970a0d621a8d5
BLAKE2b-256 3d69f32110df06d4497e65126023cb429995c7b929f11118ac645351c6236b74

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f97408b1250ef50622b3af5bf50a3a67d8ba0ff7cc0dadf651bf16635969ec57
MD5 0d12a77019baff37876d439d71c52703
BLAKE2b-256 31c1bf4de33368bef8b9e51d50fdb34ab5dfb4682565b3bc4c4f6f4c2e4fc9a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.3-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7f50c383bffaaa96313f9994bdcb4028b693b7577c7d0e6a19d929170f245ad0
MD5 c2a177fe2bfdfcac6c189866b428e6c4
BLAKE2b-256 07d5b2fe41e04d256c398294ed3d4f770e25b462e87ee15207e7cbc070da0c72

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