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 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 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 and ABC's — Extend your own classes with the mixins, Protocol and ABC's provided by the traits 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.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.

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


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.18.0.tar.gz (65.6 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.18.0-cp314-cp314-win_amd64.whl (263.5 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.18.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (383.4 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.18.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (384.0 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.18.0-cp314-cp314-macosx_11_0_arm64.whl (355.6 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.18.0-cp314-cp314-macosx_10_12_x86_64.whl (372.1 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.18.0-cp313-cp313-win_amd64.whl (263.4 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.18.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (383.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.18.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (384.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.18.0-cp313-cp313-macosx_11_0_arm64.whl (356.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.18.0-cp313-cp313-macosx_10_12_x86_64.whl (371.5 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.18.0.tar.gz
Algorithm Hash digest
SHA256 e40f3ba240e4d6c7d0c43e149557cb76c8e06b9ee45b9c182945a538fe20398c
MD5 135c88e431a08bc70f7da229fc7a6f14
BLAKE2b-256 c3604bde313d0c3ad5aee1937647a740d8c7dc120b8310e9a79d3cbe698ae398

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.18.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 e2ef4b5f7ff5fe9fee8782b6f0a8e74faa4d908929302d5edcd2542e51fa84fe
MD5 6f743946c5b43df3a2b0afda21feb3c0
BLAKE2b-256 624439cced2ed0564802d78d289e1322e6c212e8d4f683b1c8c9cf1914441835

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.18.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 37fd62e9caea17eb63378f2f14ef85700011281c9960514c7842842f8d73d364
MD5 ddde72fc0c4037b463c01610461393aa
BLAKE2b-256 d84526be94b2e681e4082340787b9703936652d8763ea921f7fee7d3309aa33e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.18.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9cde69fe77c8e2dba85c4a185b64b35d7157fc5b1784b8f5ca7ebc9aa6005efd
MD5 28103f9eb2ffe18635d38ccd9c4356da
BLAKE2b-256 940df1b8beb5c6914ee7d25ee1c53dec708552812cdbd9ce0122745e8617db37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.18.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1f84039d6ef2f762a80efc41d99a064777b49abac7025d29a5edd0184f2aa412
MD5 62c921493a7418e06163d9506be658c9
BLAKE2b-256 862f6041bc3f37a6ad761e79dc8da1d2c97d4c4481c7172230fc33c97d9ad8d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.18.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1b098543a089e2e1385cd8668e46b66924d6023a63231f647fdcb9d4d0e2bcaa
MD5 ee9194449e24f179f8c9982232834c18
BLAKE2b-256 16d162e6fa6f7eb867584dfc7de0dfc77f30a4133dde03ca93dc85b2409493f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.18.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7bf13aad280044cf512b48056e39e282f498b89a6d0e8f59076819ecbd08e824
MD5 3e480b12839dd85b23e4f8b2b152aebf
BLAKE2b-256 ac2ea993b4d7818dcda1814f27f49dada259c921ad24bd3fa52b6d9a2497d769

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.18.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dcf59f6f5e1b016101fe2c02546d6428152c20f3ca34c1845689e2c09aec21ac
MD5 4499207d50593184843bbedd30c4f3bc
BLAKE2b-256 1a84ecc446d5ad4cbb1836b8ea0436983592f3bde46b0ecd0f1048803be0db7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.18.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d21b497411b60bce7d630495adafe9616e545d367025ea728b0fe6ecf5d1d725
MD5 6b0272e0693d1887ab92091d7845a16e
BLAKE2b-256 ac3d2458c9a97df570ee627285ffc076f09bf7e6d44f0869990ced4641396cfe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.18.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d5bb0cf92f82c2d7fca66d24324afd5a3b2f423d177ce64b67d179aab2f12844
MD5 73d0d9fb4a7e59a91363ae13922fde69
BLAKE2b-256 b0e1d6632c0924f97c018a116243a59649041d71ac6d98e620b094f0eeb2c677

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.18.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 07d480ad2cc981f151a539e2b0ad1a3fb93c7b0172d3ee6854d1a24e1a6d8ccd
MD5 dedd91f318f9de7613cce40e15feb1b9
BLAKE2b-256 a7941d2b84cba5771e0386f5be29e9523ba90bf991a471e1f601f88d46fe82a2

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