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 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
forloops, None checks, and error handling with chainable methods. - 🦥 Lazy-first, 🔒 explicit mutability —
Iter[T]for lazy, efficient iterations;SeqandSetfor immutable data;VecandSetMutwhen you need to mutate. - Memory efficient - Almost all methods from
Iter[T]operate in streaming fashion, andVec[T]provides in-place methods with more memory efficiency than standard list methods (e.g.x.extend_move(y)won't create intermediate allocations likex.extend(y)followed byy.clear()). - 🎯 Result and Option types - Handle
Noneand exceptions in a fluent, explicit way. - 🔥 Blazingly fast — Core
OptionandResulttypes are written in Rust for minimal overhead, and iterators use functions fromcytoolz(Cython) and the stdlibitertoolsfor 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(), convertIterablestoOptionorResultbased on their truthiness, and more. - 🐍 Mixins and ABC's — Extend your own classes with the mixins, Protocol and ABC's provided by the
traitsmodule.
Installation
uv add pyochain # or pip install pyochain
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.traits 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')
>>> # Use new() to create an annotated empty Vec without brace and parentheses mixup
>>> Vec[int].new().then(_process).ok_or("No values")
Err('No values')
>>> # Create empty Set, convert to Result, then back to Option
>>> Set[int](()).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
🔄 Interoperability - Converting between types
📚 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:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyochain-0.16.0.tar.gz.
File metadata
- Download URL: pyochain-0.16.0.tar.gz
- Upload date:
- Size: 66.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b57e1e7f61501b00f3ed894e81943ab7198c229233dea7cba337c1e874ec8bdc
|
|
| MD5 |
1aac2a345bd4d15c9dae02313cb02bbd
|
|
| BLAKE2b-256 |
5f280093f78c416df9fd49197b8e536412cc8592ac5cb30298a4c448549b8f0f
|
File details
Details for the file pyochain-0.16.0-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: pyochain-0.16.0-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 273.5 kB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58c273af8d7d8792fa6359f0b0ea0b411f0085843538c135d7bd4c51e1ff7415
|
|
| MD5 |
9e780c03e34ee72cc4c6b4d32b61d480
|
|
| BLAKE2b-256 |
aa81a1076af48f4c57704f223b98e366185db50d5bc4637ee51cdbe5a2a9af4b
|
File details
Details for the file pyochain-0.16.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: pyochain-0.16.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 387.4 kB
- Tags: CPython 3.14, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b717807b8a78326874d6be825c715383a61258e40e324ca429be3794d3d40529
|
|
| MD5 |
4d7af16f0d4c114e538e3db720650ded
|
|
| BLAKE2b-256 |
0393eb2b8bfc57c321f93ea4e343885f0d8f2caea4cdca981832fc1cac6513ea
|
File details
Details for the file pyochain-0.16.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: pyochain-0.16.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 389.7 kB
- Tags: CPython 3.14, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8c672613091524ce629af5a64a452faa10198569060663ee30e2f523da7094c
|
|
| MD5 |
557498c1a1e7556a1fcc72ff2b0ae109
|
|
| BLAKE2b-256 |
2a393d7e1c2719efcad3a32422bd2c2401d45414b1b75e6fa0e8779363af60fd
|
File details
Details for the file pyochain-0.16.0-cp314-cp314-macosx_11_0_arm64.whl.
File metadata
- Download URL: pyochain-0.16.0-cp314-cp314-macosx_11_0_arm64.whl
- Upload date:
- Size: 361.1 kB
- Tags: CPython 3.14, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c81665ec614951afbf4d08c02d59efc5871bb246209847ff4f3baa1dcf771419
|
|
| MD5 |
6cbec999e11a2bb3c460cf92773d2f29
|
|
| BLAKE2b-256 |
db9d9180f0c20981d89aab759e59a4c238ad867e9df6aae5dfd6ed68914f3041
|
File details
Details for the file pyochain-0.16.0-cp314-cp314-macosx_10_12_x86_64.whl.
File metadata
- Download URL: pyochain-0.16.0-cp314-cp314-macosx_10_12_x86_64.whl
- Upload date:
- Size: 378.1 kB
- Tags: CPython 3.14, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37db198e80cd396f1891584ab593e5fc3b14acafd918fc2a66c49f51a0d5c818
|
|
| MD5 |
135fc8c376bd9189d731fe912d80bd92
|
|
| BLAKE2b-256 |
9d5e5c19e269055e89fe19a2c479870016e6b8a1fcc25ea8ed9b0874be90caa1
|
File details
Details for the file pyochain-0.16.0-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: pyochain-0.16.0-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 273.3 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ba318f5e1cb83e4df6e106b728c99aff3219222bd1c68fab241d06644fcd5c1
|
|
| MD5 |
96fec96ced0e84fca91da92aed721391
|
|
| BLAKE2b-256 |
df0a42e1bab7b5144515a511ea79dd8b62615afed5d329b344c8f686e6272eae
|
File details
Details for the file pyochain-0.16.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: pyochain-0.16.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 387.5 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b9d37651f0c563b2907438ff2b46fe46fae97289697172e54b52922dea1c072
|
|
| MD5 |
ae4269f86a5973cf0aa79d70b00915f6
|
|
| BLAKE2b-256 |
4b5603d1a7a0c8a5a5ceae15176ec8b46e29fd611971bf83e486f491199fd834
|
File details
Details for the file pyochain-0.16.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: pyochain-0.16.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 390.3 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
027c9200f4defee266b035e1bed2b1ebe7811ff8918134d08652388322eebde0
|
|
| MD5 |
4537b1469de6c0e1ea8c1442ecd18c8e
|
|
| BLAKE2b-256 |
51642ae3a22dce173e9af80332244b72e48032d4afb9418f9bcfc1d900f15889
|
File details
Details for the file pyochain-0.16.0-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: pyochain-0.16.0-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 361.5 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8dba59d53a8a7c838a11ffbc48fc35b02d32075cb00a4a7fefa255f6449df0d
|
|
| MD5 |
1865cb1d145c86700eeca9b2c4aa42fb
|
|
| BLAKE2b-256 |
7092b9c6072d0a5a168b6a70eba53a58f393e55c7eb7b09a253d7bc184268199
|
File details
Details for the file pyochain-0.16.0-cp313-cp313-macosx_10_12_x86_64.whl.
File metadata
- Download URL: pyochain-0.16.0-cp313-cp313-macosx_10_12_x86_64.whl
- Upload date:
- Size: 377.4 kB
- Tags: CPython 3.13, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f88ef4d5f17ad6ddda0b128ee840a80190b59cedd30e48ec91174c39c0d739a
|
|
| MD5 |
a74c5658c54f26da97725c439d63c6de
|
|
| BLAKE2b-256 |
da2d8b818ed90a58b1e55bbc6015c3e8d300ec82183e08edd457593d107a2405
|