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 is a no-dependency library providing a set of classes with a fluent API, to work with iterations, 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 almost always compiled C or Rust level code, from the builtins, itertools or custom Pyo3 implementations.
  • 🛡️ 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 and ABC's — Extend your own classes with the mixins, Protocol and ABC's provided by the abc module.

Installation

uv add pyochain # or pip install pyochain

See the package page on Pypi

Quick Start

Iterations

>>> from pyochain import Iter, Seq
>>> # Lazy processing with Iter
>>> res: Seq[tuple[int, str]] = (
...     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

>>> from pyochain import Option, NONE, Some, Seq, Vec, Set, Ok, Err, Result
>>> from pyochain.abc import PyoIterable
>>>
>>> def divide(a: int, b: int) -> Option[float]:
...     return NONE if b == 0 else Some(a / b)
>>> divide(10, 2)
Some(5.0)
>>> # Provide a default value
>>> divide(10, 0).unwrap_or(-1.0)
-1.0
>>> # Convert between Collections -> Option -> Result
>>> data = Seq((1, 2, 3))
>>> # Convert Seq to Option
>>> data.then_some()
Some(Seq(1, 2, 3))
>>> # Accept any Pyochain Iterable
>>> def _process(data: PyoIterable[int]) -> str:
...     return data.iter().map(str).join(", ")
>>> # Process only if non-empty, convert Option to Result
>>> data.then(_process).ok_or("No values") 
Ok('1, 2, 3')
>>> Vec(()).then(_process).ok_or("No values")
Err('No values')
>>> # Create empty Set, convert to Result, then back to Option
>>> Set(()).then(_process).ok_or("No values").ok()
NONE
>>> def try_parse_int(s: str) -> Result[int, ValueError]:
...     try:
...         return Ok(int(s))
...     except ValueError as e:
...         return Err(e)
>>> # Type safe exhaustive handling with pattern matching
>>> def handle_result(res: Result[int, ValueError]) -> str:
...     match res:
...         case Ok(value):
...             return f"Parsed value: {value}"
...         case Err(error):
...             return f"Error parsing int!"
>>>
>>> try_parse_int("123").into(handle_result)
'Parsed value: 123'
>>> try_parse_int("abc").into(handle_result)
'Error parsing int!'

Documentation

For comprehensive guides and examples:

🔍 Types Overview — Roles, comparisons and visual relationships

📚 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

Credits

Most of the custom computation algorithms have been inspired by implementations from itertools, cytoolz and more-itertools.

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.21.0.tar.gz (68.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.21.0-cp314-cp314-win_amd64.whl (285.5 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.21.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (407.1 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.21.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (407.0 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.21.0-cp314-cp314-macosx_11_0_arm64.whl (379.3 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.21.0-cp314-cp314-macosx_10_12_x86_64.whl (394.9 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.21.0-cp313-cp313-win_amd64.whl (285.8 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.21.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (407.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.21.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (407.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.21.0-cp313-cp313-macosx_11_0_arm64.whl (379.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.21.0-cp313-cp313-macosx_10_12_x86_64.whl (395.1 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.21.0.tar.gz
Algorithm Hash digest
SHA256 248770e3b6d127c8a094e26de5384321003372255f5ed11a762e5d202bc6312b
MD5 c6efd87e4b8b7ba1246eab58ef7be921
BLAKE2b-256 11a758cfbac74615b3071aa10d5ab733c0f68e19d1236bef30fc52e3e62c614c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.21.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 ce20fb0f067e4a356f5e9f8e0d2bc88aff260449c110dc27d04554194cc85155
MD5 a8da00bcb49fb91e34b4f0b697fae532
BLAKE2b-256 e31c06b4ebc82ab3f3cc7ca3110aace90f67dc902728ac34ac9e299421722a54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.21.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 08b56c9f7637ff0538909e6378e86015779f3b8dc1073181f140f851b81ea1ae
MD5 4049c9b68b111ab81cb682c035c6fb5e
BLAKE2b-256 519cb90586cd851d7b4b4a2b1b3c02f290c1f5186978e2be98eef92548c14ce8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.21.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1efeaad6d51be9c2fdc3c6630fa6c08234e6f5ff822108ed96c3ad824abfd30b
MD5 d32e9a75a03f1d13d05881ece9b529c8
BLAKE2b-256 fbc5a2038b1bfeb9425bd6224d7958d37913d69a969c89d0e5e98150f56c3e8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.21.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a08dbfebc252929e28036015317d9a88480704baf7722c392544edc0f5eaa043
MD5 73c596f506c4a7e0f0fb00c287448ee3
BLAKE2b-256 ffe297ae004a8f1c262c0e91282527d65884bff341404cbdf1c761b6b238fc0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.21.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7d1e49c14f8ed2b92a2273091c2a47d4df449bf339d0b8222ebf88ed943ff130
MD5 cb7790148aaa9ae3bcbcf6fcb5547260
BLAKE2b-256 c5b6a073f95587f7aa3a4b5df422c39532dff190b67434751a805ecdcce28319

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.21.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c26718cfb67b153f9061d02d921ed1ec69c9638fb2776e9b356f174b5a6b29b5
MD5 8ea9434d3aafe695febaefe36edf0799
BLAKE2b-256 770ff292fdb90769d5af9dce7e7652f47a9c6bdbd7deb66b2b4e67315e315985

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.21.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7d457f564445169ead0aea2d1b067814871ed31454ce96c780e856c5945ea7e2
MD5 da24539c9458b64e035537f5388711fa
BLAKE2b-256 22591fa89bf8864834d8b2bb0b23227bd717d6855aeaffa8d1683d48a8429b61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.21.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6cace62617b02b00a6f51a19e981d7f3f08beeea6eccc14f70eac5e07a03f889
MD5 7e746cd61044c9edf63309d24d052ec3
BLAKE2b-256 1b47ddfb0d66dc079875961e5ae262446fceba5bf11372f5c018cba37eb047e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.21.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 72d79ade4ee36be4ea4a6afbc05eb06c4ce6aeb98a212df1e01546e77e81f795
MD5 8b31c11a06dce42a658409036e714855
BLAKE2b-256 b6217b5d99f6595b1097b161a81d17416127d1644450a86b24b865293a5b9665

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.21.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a66cc414961f8295596ce3264148430228880248bbdb3ffda3fdc924765ca71a
MD5 b40c9bb5532f26a5213e41c2e3c8cc2f
BLAKE2b-256 03804fe5dcc98692eb5f53610a823ce5efec07528bf8f4d97cfded33db3a0fed

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