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

Uploaded CPython 3.14Windows x86-64

pyochain-0.20.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (407.0 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.20.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (406.8 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.20.0-cp314-cp314-macosx_11_0_arm64.whl (379.2 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.20.0-cp314-cp314-macosx_10_12_x86_64.whl (394.7 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.20.0-cp313-cp313-win_amd64.whl (285.6 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.20.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (407.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.20.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (407.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.20.0-cp313-cp313-macosx_11_0_arm64.whl (379.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.20.0-cp313-cp313-macosx_10_12_x86_64.whl (394.9 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.20.0.tar.gz
Algorithm Hash digest
SHA256 380dca3f10c3fcbd34af1ff83461d702603bb0f26dbae589bd92bb78f47dc16e
MD5 3895f939b2ff2055c42459c1a17dee47
BLAKE2b-256 32b21d453a61f6ea3e44f2a53305cef36bf9246ff2a8511452454db386bfa19d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.20.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 21cc2a60872e00364f303277a3afea3f6f77102d42b5df1e3e36f2384d342d3d
MD5 9d98b6b76d0180bb1d5eb4b7fc97fb48
BLAKE2b-256 4da30bcb81fd76a57a2f50e1512a417b60862765b1b795685540b77cf927cd9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.20.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ccff2fd563643a02aebb2f28faf2637e56b94656b078bc02e8152eefff91601d
MD5 bc1f20c6fe6a9ed63aed6a40c37299d3
BLAKE2b-256 14e2d63c9a5a8fb590e35f432dc4ce4bddf66e2e47ae0434f7f5a86279a43824

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.20.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 78ab30e58595097de51c5388762963141aa5a2e2ef42625f2d6bf757aa8a972f
MD5 795200fb5bc791187ec344da32a18a4a
BLAKE2b-256 44780cbc1888e4be3e60dfb6555624b0ca88e0ce5f4147395f80488385eba533

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.20.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 85914c62d18396c41ce91d229c2496dd8a45a2f33d687322fcd80dfaeee1f63e
MD5 55f572ed1110b3420ff29022e99e2736
BLAKE2b-256 dfd7f35bdf5709efe071cd66d8a2587852165fbd4eac95097974d2be81e5ba37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.20.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0702e90b292711bf79ebba2866c89e447830a297e9389a9f6765cb8ad971b0fd
MD5 22481cd24329dbc537c6785d00cf1f79
BLAKE2b-256 eb081d0261cdb518a55d0e59986c3c647496420d8e18b8b424805e3f16e4d57e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.20.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c00b575975c763cdbbfed0d64df5f44fe443d248b10e4a37453ada5b65a8fce0
MD5 f7d949b3a15d88ad372ff58dc284a332
BLAKE2b-256 ea8e18de862aad15bf2fc82ca32abc124b3477b58fb20b6e1f0a6871aae3769c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.20.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2eb25aa30f851567cac68a28db96bed4d984c187eb6f0192792afe9f288cb9b4
MD5 ea2e82f4f87e6b12c2b06793a90f277a
BLAKE2b-256 b9a9ba8186c6dbec157a4ee643e63c75e7a47c1de980354804776641fbd4e190

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.20.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 eb6a7cd6375e14e3938f4ef6612d465b27b15b3b3ff0d088e51eb5fc16717539
MD5 c78b103feb65abd50937597f50c2cf19
BLAKE2b-256 0864875b93c801ebb3b4f63d2ab646a996070f990974721fd2c3811c3cf112c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.20.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dbccd579830f3f748a1172792c65fde585d6223a4e89253861b225e8a9e1fddc
MD5 9f9cfdb412644ce762b0a9c7ce086441
BLAKE2b-256 9861542191e73f38c27bed206ccff6591a223a8c17ec898991e0e5a3fd93a88e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.20.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2de94abded5ef27e8c906a8cf6996e7577b287e58b98fc2ec1c1409a8cc2651a
MD5 1f46bba3a79cd796b1ae1b1351cb2a24
BLAKE2b-256 dd213bbeb860208aace585565b0e5872b0832f33326fa8b4b1410be59c316cf8

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