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
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 traits — Extend your own classes with the methods in the mixins provided by the
traitsmodule.
Installation
uv add pyochain # or pip install pyochain
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:
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.9.0.tar.gz.
File metadata
- Download URL: pyochain-0.9.0.tar.gz
- Upload date:
- Size: 59.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
557fd0310cf2c0242e1d85bd95a0222eb2f74926a262d0bcdcdfac975836dbe0
|
|
| MD5 |
12eefb2bac0e1050b8b1cbfaf27da2e0
|
|
| BLAKE2b-256 |
9cddc4bbf7493312dd66ec3770a29a563eead348ba169834194c59795e49f916
|
File details
Details for the file pyochain-0.9.0-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: pyochain-0.9.0-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 232.0 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f64689cdafd26840e1d8321328590a01ed6f0f4f7bae5a5495eecf4d425d248
|
|
| MD5 |
802b4d42b78d508e3258f8f2cfd1f9d0
|
|
| BLAKE2b-256 |
4ae4284c07378f2412d75f8217562ec5d50a75130d6d86444f0623901cda653b
|
File details
Details for the file pyochain-0.9.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: pyochain-0.9.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 350.7 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ffc06d9b9ed07dd8284f21cc800e6815caef807f01e4eeebca941dd0083ace96
|
|
| MD5 |
96cdde28d451325f99c72531e745ddc1
|
|
| BLAKE2b-256 |
403a2539dda65ea5452bb7ebc540bb04d9b3b155dd36c7f75073459854317718
|
File details
Details for the file pyochain-0.9.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: pyochain-0.9.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 352.9 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c057f2b0610b9c16f8a4ba4e0f37207230cc772aa107ed05d87cd63bbeaf552
|
|
| MD5 |
787ad541731f408df5296c0f3f7b70b4
|
|
| BLAKE2b-256 |
507baff89fda883add24f6c3150823d3750295b7635106ed33aa0d5aa80c7a1a
|
File details
Details for the file pyochain-0.9.0-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: pyochain-0.9.0-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 326.3 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f05e1859319cd24a0938f4dec976bd23287a8014f56219ee08283682a956a72
|
|
| MD5 |
cda1058f74b655e1ba5b232fe8b7e56d
|
|
| BLAKE2b-256 |
317f756cade0205fd9167f5dddbefc27f1411ee7a9377987643e4bb8eb057e4c
|
File details
Details for the file pyochain-0.9.0-cp313-cp313-macosx_10_12_x86_64.whl.
File metadata
- Download URL: pyochain-0.9.0-cp313-cp313-macosx_10_12_x86_64.whl
- Upload date:
- Size: 335.6 kB
- Tags: CPython 3.13, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cef19ceee45860c105e54ec0fdd7e612c15246bae7f6dae8c90c1abe05a9236e
|
|
| MD5 |
46fc6fe108df32eee9d62ea3c2b42efd
|
|
| BLAKE2b-256 |
5d06636d82e51c852b23e666ad41dabd295722eea353686d8a8df7632caea31c
|