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 and declarative API, to work with 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 traits — Extend your own classes with the methods in the mixins provided by the traits module.

Installation

uv add pyochain # or pip install pyochain

See the package page on Pypi

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:

https://github.com/OutSquareCapital/cytoolz-stubs

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.15.0.tar.gz (64.9 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.15.0-cp314-cp314-win_amd64.whl (264.9 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.15.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (380.5 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.15.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (381.5 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.15.0-cp314-cp314-macosx_11_0_arm64.whl (354.2 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.15.0-cp314-cp314-macosx_10_12_x86_64.whl (369.5 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.15.0-cp313-cp313-win_amd64.whl (265.5 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.15.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (380.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.15.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (382.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.15.0-cp313-cp313-macosx_11_0_arm64.whl (354.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.15.0-cp313-cp313-macosx_10_12_x86_64.whl (369.3 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.15.0.tar.gz
Algorithm Hash digest
SHA256 7f988c9683f6821ccd41b01c1c2ed187a8262f6f048bb0f49c95abd10dd61963
MD5 1bc5be137127c266c4729c25dd521ade
BLAKE2b-256 6c7e536067bff3e8b68b4b8bf7469a80328030d92f968156fafa95f387c3855d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.15.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 41aaddde8057a7b9797c4bd6ecb2c7daae7c264ed4542bdd5469cba07bc254aa
MD5 85e67dd15920fa83e3576b68965ae6bc
BLAKE2b-256 0041a8139cee15226c63cca4d7eaa7320e7c480386fa153bddfe8e404963e15f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.15.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ead71787a2ec274194cb874d5b860fef351ad7916e47a867aa9764000120a8ef
MD5 33511637165c1f38b662813c718afdf3
BLAKE2b-256 685a6df72ef844fc1cae380f661cc4b25b38e9a298616c64c74dc0fcf0532373

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.15.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0fdbb4af5680cc6de533af339a43911736c118f177a3bbf6bd675c07369cfdd5
MD5 d2ca021d95b7e1da35603738df20e75c
BLAKE2b-256 9c58822b33dbd4b41511e9438683e033eff2a0a54350b487bbd8f30ba0d4d3ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.15.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 96de3e837f7d991b42313aebf9b0029063e3a5443f1dbd8be434096fa4e73118
MD5 8db37e807029b284087c1cff229f5b9f
BLAKE2b-256 371ca85aed44ab6e64459b25d0cc93b180dc2c10f03a39c8a97812317981f177

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.15.0-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ed199d8d073abea0d25143d9e7dced38bf9ff6d2534579cd9fa1bfd361dcb595
MD5 3a97db955a264884a9e09954e136399f
BLAKE2b-256 f4f34aae91e9f95f0bb164234152a0fad5c9db15dbc8e5682ad5b9579437dd02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.15.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2386c649d29f81d4c35375309f5ea307bc7794d3fbe5dc71f975ae526edbd6d3
MD5 574acc167d0e0fff3e7782f33a1a357e
BLAKE2b-256 5ee591ffdc970a7c7abb50ec10027f1242cbbe23b88c157ddb625b6ad3236ef6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.15.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 274f847fa4cfb8adecf62fdf4d3a89e0d3e355dd7b7e649c468d1238f9767f34
MD5 431a2d684b2e046b1c69c0f8bd545e8b
BLAKE2b-256 e9dc394c5c3f9ba3552bbd3f46647765b22c09fb66400ac8b6a12fc2b42ce261

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.15.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c6e55337220bfdc81440054785547163ae69bb39c44cf8280b0254fd0c919720
MD5 fa3e448a2bb5c00f82ec4f2e1e3036a1
BLAKE2b-256 ecaf3464e477f0088c230a847880fe744028febada68664cc1e9cc21b7dd30eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.15.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7ef65564cb2cec50421f7946a4120fd867976ecec26e86cb053afec587e0fe74
MD5 fb532fcd5bbc02e20b22fdddf335f1f1
BLAKE2b-256 308dc249534e9dfa9d442a9f408e74cf084d872068b0a94082643c651b3ceea3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.15.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 63da2aaed9961e056288a5849e5bc087b27af17ee0c0b96bfcb61537cb7ce65e
MD5 bed50b775093d69651ae854339a25728
BLAKE2b-256 469e3f7953b2dd877e6f4222ec8daca9d48cec81f88c9e85b5287d5f4237efb0

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