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.

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.9.2.tar.gz (61.0 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.9.2-cp314-cp314-win_amd64.whl (261.6 kB view details)

Uploaded CPython 3.14Windows x86-64

pyochain-0.9.2-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (378.2 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyochain-0.9.2-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (379.5 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

pyochain-0.9.2-cp314-cp314-macosx_11_0_arm64.whl (356.8 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyochain-0.9.2-cp314-cp314-macosx_10_12_x86_64.whl (367.4 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

pyochain-0.9.2-cp313-cp313-win_amd64.whl (254.6 kB view details)

Uploaded CPython 3.13Windows x86-64

pyochain-0.9.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (375.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyochain-0.9.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (374.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pyochain-0.9.2-cp313-cp313-macosx_11_0_arm64.whl (350.0 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyochain-0.9.2-cp313-cp313-macosx_10_12_x86_64.whl (361.6 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pyochain-0.9.2.tar.gz
Algorithm Hash digest
SHA256 4b7c31aba04728fb653448276a3ca9f81d89b7b69f5fda0c690a26406e2ad544
MD5 ada0cd859458397a0250bc1e27d6b6ba
BLAKE2b-256 53535a7888856cbf29f8c6861c7bfe8d4d68a9bc0508ea751760f040be781516

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyochain-0.9.2-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 261.6 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for pyochain-0.9.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 9e045a46842a1b22afe5bf24a32b999a296992a311c56e4adea710b851126359
MD5 d70b238d2c2191274c132d5e99029222
BLAKE2b-256 b40588a64026a4d36824f80f385f5d27d0325f7dc6730670a47f436ed2501007

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.2-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d07bc870848cade4c7804429bfc3dcc7a15dbc85deb1f3d158f29b14c6a28f0c
MD5 8cd60e770b5a30a57dd67e7d3c6aa093
BLAKE2b-256 274fe8378a8f990ac8c531cf3b070941d2fd4d61090ea7e1fa3af22777cab765

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.2-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fc96ff85dbcd659ce2d07193ce0bfce803cf894f8a9118d01426aaa7a8da0d10
MD5 10598d1130405de637f30ab09c92ec38
BLAKE2b-256 64698c3fb7c24ff325928d84ac7cd55b5b76a7e713bf5e144104ff537bf42f29

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d9e9f24d67eb9fc8a71e2bb518b5b9f3a32afc0af22572deb39acce1746ef165
MD5 408aab1a624780c667aba5644b10f992
BLAKE2b-256 639fb81f2efd02cebad447554ab42410257ac951c8695a15174ed1ab6c3ec3c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.2-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1b4b70e7d2369bb5eae8b4bdcfce6b298314bca972f46c21787921a7f2da57d4
MD5 123e4b38cdae702efe5be912b88497e1
BLAKE2b-256 1911174a9a2c0fa7dd0f6e3e064f3a76a5eafecf3f410cea16a28c2e7416a2fd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyochain-0.9.2-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 254.6 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.11.5

File hashes

Hashes for pyochain-0.9.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2347f7c88bf0cc81da65b3ab4809750299b117eb464233aa7b7da4f99b07b83e
MD5 619a097339fa79b66d6040edfeba86e2
BLAKE2b-256 a3448cf800bec254a4826f17a06021e96fc06bacd9ed5783a90fa03059a74ea4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6753ad6f8adeb4118e6097dd83ea11fe1e8fa767e77a1ea06ea4fb5fc4d8e3fd
MD5 7f8eee5026a5973639b1df8673ac3416
BLAKE2b-256 b3279b10872f633a659fb7f13fce94c36b9a917e3425d851ddbeff40b59d75ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8270a2a5d11a724687630fbb273edf981ccdf97c5d55579f5a5cef5f51efbe7b
MD5 835070cc4ef22b2d12c45eab4c24ba82
BLAKE2b-256 63b4020d13321cf52b88e2921cf1bbf3d77408e73e005063d29b9234f7a1e442

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d88d30537319f877cf3c5a78ac5b1cd6dfc3039bbd9fbef3064f0cf25a54c9e
MD5 29b128d2625826be1b4f43b10ef664c4
BLAKE2b-256 646e7ee06f8db69193f15293ae8cea5507564a071872f0d7bfeeddfef27ca3a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyochain-0.9.2-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6f3fb456fe24742ab89658326270581d74f31a36b00a94c204c2fe1cf8c54fcc
MD5 1d8a1164dd3137238d8494bec9f241b2
BLAKE2b-256 85ac6347d198a5ea4cfef39c1272f895694c13a3aaf4bde0d83c5772337e5bcb

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