| Name | pyochain JSON |
| Version |
0.5.42
JSON |
| download |
| home_page | None |
| Summary | Method chaining for iterables and dictionaries in Python. |
| upload_time | 2025-11-11 17:35:43 |
| maintainer | None |
| docs_url | None |
| author | None |
| requires_python | >=3.12 |
| license | None |
| keywords |
|
| VCS |
|
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# pyochain ⛓️
**_Functional-style method chaining for Python data structures._**
`pyochain` brings a fluent, declarative API inspired by Rust's `Iterator` and DataFrame libraries like Polars to your everyday Python iterables and dictionaries.
Manipulate data through composable chains of operations, enhancing readability and reducing boilerplate.
## 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.
## Installation
```bash
uv add pyochain
```
## API Reference 📖
The full API reference can be found at:
<https://outsquarecapital.github.io/pyochain/>
## Overview
### Philosophy
* **Declarative over Imperative:** Replace explicit `for` and `while` loops with sequences of high-level operations (map, filter, group, join...).
* **Fluent Chaining:** Each method transforms the data and returns a new wrapper instance, allowing for seamless chaining.
* **Lazy and Eager:** `Iter` operates lazily for efficiency on large or infinite sequences, while `Seq` represents materialized sequences for eager operations.
* **100% Type-safe:** Extensive use of generics and overloads ensures type safety and improves developer experience.
* **Documentation-first:** Each method is thoroughly documented with clear explanations, and usage examples. Before any commit is made, each docstring is automatically tested to ensure accuracy. This also allows for a convenient experience in IDEs, where developers can easily access documentation with a simple hover of the mouse.
* **Functional paradigm:** Design encourages building complex data transformations by composing simple, reusable functions on known buildings blocks, rather than implementing customs classes each time.
### Inspirations
* **Rust's language and Rust `Iterator` Trait:** Emulate naming conventions (`from_()`, `into()`) and leverage concepts from Rust's powerful iterator traits (method chaining, lazy evaluation) to bring similar expressiveness to Python.
* **Python iterators libraries:** Libraries like `rolling`, `cytoolz`, and `more-itertools` provided ideas, inspiration, and implementations for many of the iterator methods.
* **PyFunctional:** Although not directly used (because I started writing pyochain before discovering it), also shares similar goals and ideas.
### Core Components
#### `Iter[T]`
To instantiate it, wrap a Python `Iterator` or `Generator`, or take any Iterable (`list`, `tuple`, etc...) and call Iter.from_ (which will call the builtin `iter()` on it).
All operations that return a new `Iter` are **lazy**, consuming the underlying iterator on demand.
Provides a vast array of methods for transformation, filtering, aggregation, joining, etc..
#### `Seq[T]`
Wraps a Python `Sequence` (`list`, `tuple`...), and represents **eagerly** evaluated data.
Exposes a subset of the `Iter` methods who operate on the full dataset (e.g., `sort`, `union`) or who aggregate it.
It is most useful when you need to reuse the data multiple times without re-iterating it.
Use `.iter()` to switch back to lazy processing.
#### `Dict[K, V]`
Wraps a Python `dict` (or any Mapping via ``Dict.from_``) and provides chainable methods specific to dictionaries (manipulating keys, values, items, nesting, joins, grouping).
Promote immutability by returning new `Dict` instances on each operation, and avoiding in-place modifications.
Can work easily on known data structure (e.g `dict[str, int]`), with methods like `map_values`, `filter_keys`, etc., who works on the whole `dict` in a performant way, mostly thanks to `cytoolz` functions.
But `Dict` can work also as well as on "irregular" structures (e.g., `dict[Any, Any]`, TypedDict, etc..), by providing a set of utilities for working with nested data, including:
* `pluck` to extract multiple fields at once.
* `flatten` to collapse nested structures into a single level.
#### `Wrapper[T]`
A generic wrapper for any Python object, allowing integration into `pyochain`'s fluent style using `pipe`, `apply`, and `into`.
Can be for example used to wrap numpy arrays, json outputs from requests, or any custom class instance, as a way to integrate them into a chain of operations, rather than breaking the chain to reference intermediate variables.
### Core Piping Methods
All wrappers inherit from `CommonBase`:
* `into[**P, R](func: Callable[Concatenate[T, P]], *args: P.args, **kwargs: P.kwargs) -> R`
Passes the **unwrapped** data to `func` and returns the raw result (terminal).
* `apply[**P, R](func: Callable[Concatenate[T, P]], *args: P.args, **kwargs: P.kwargs) -> "CurrentWrapper"[R]`
Passes the **unwrapped** data to`func` and **re-wraps** the result for continued chaining.
* `pipe[**P, R](func: Callable[Concatenate[Self, P]], *args: P.args, **kwargs: P.kwargs) -> R`
Passes the **wrapped instance** (`self`) to `func` and returns the raw result (can be terminal).
* `println()`
Prints the unwrapped data and returns `self`.
### Rich Lazy Iteration (`Iter`)
Leverage dozens of methods inspired by Rust's `Iterator`, `itertools`, `cytoolz`, and `more-itertools`.
```python
import pyochain as pc
result = (
pc.Iter.from_count(1) # Infinite iterator: 1, 2, 3, ...
.filter(lambda x: x % 2 != 0) # Keep odd numbers: 1, 3, 5, ...
.map(lambda x: x * x) # Square them: 1, 9, 25, ...
.take(5) # Take the first 5: 1, 9, 25, 49, 81
.into(list) # Consume into a list
)
# result: [1, 9, 25, 49, 81]
```
### Typing enforcement
Each method and class make extensive use of generics, type hints, and overloads (when necessary) to ensure type safety and improve developer experience.
Since there's much less need for intermediate variables, the developper don't have to annotate them as much, whilst still keeping a type-safe codebase.
### Convenience mappers: itr and struct
Operate on iterables of iterables or iterables of dicts without leaving the chain.
```python
import pyochain as pc
nested = pc.Iter.from_([[1, 2, 3], [4, 5]])
totals = nested.itr(lambda it: it.sum()).into(list)
# [6, 9]
records = pc.Iter.from_(
[
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
]
)
names = records.struct(lambda d: d.pluck("name").unwrap()).into(list)
# ['Alice', 'Bob']
```
## Key Dependencies and credits
Most of the computations are done with implementations from the `cytoolz`, `more-itertools`, and `rolling` libraries.
An extensive use of the `itertools` stdlib module is also to be noted.
pyochain acts as a unifying API layer over these powerful tools.
<https://github.com/pytoolz/cytoolz>
<https://github.com/more-itertools/more-itertools>
<https://github.com/ajcr/rolling>
The stubs used for the developpement, made by the maintainer of pyochain, can be found here:
<https://github.com/py-stubs/cytoolz-stubs>
---
## Real-life simple example
In one of my project, I have to introspect some modules from plotly to get some lists of colors.
I want to check wether the colors are in hex format or not, and I want to get a dictionary of palettes.
We can see here that pyochain allow to keep the same style than polars, with method chaining, but for plain python objects.
Due to the freedom of python, multiple paradigms are implemented across libraries.
If you like the fluent, functional, chainable style, pyochain can help you to keep it across your codebase, rather than mixing object().method().method() and then another where it's [[... for ... in ...] ... ].
```python
from types import ModuleType
import polars as pl
import pyochain as pc
from plotly.express.colors import cyclical, qualitative, sequential
MODULES: set[ModuleType] = {
sequential,
cyclical,
qualitative,
}
def get_palettes() -> pc.Dict[str, list[str]]:
clr = "color"
scl = "scale"
df: pl.DataFrame = (
pc.Iter.from_(MODULES)
.map(
lambda mod: pc.Dict.from_object(mod)
.filter_values(lambda v: isinstance(v, list))
.unwrap()
)
.into(pl.LazyFrame)
.unpivot(value_name=clr, variable_name=scl)
.drop_nulls()
.filter(
pl.col(clr)
.list.eval(pl.element().first().str.starts_with("#").alias("is_hex"))
.list.first()
)
.sort(scl)
.collect()
)
keys: list[str] = df.get_column(scl).to_list()
values: list[list[str]] = df.get_column(clr).to_list()
return pc.Iter.from_(keys).with_values(values)
# Ouput excerpt:
{'mygbm_r': ['#ef55f1',
'#c543fa',
'#9139fa',
'#6324f5',
'#2e21ea',
'#284ec8',
'#3d719a',
'#439064',
'#31ac28',
'#61c10b',
'#96d310',
'#c6e516',
'#f0ed35',
'#fcd471',
'#fbafa1',
'#fb84ce',
'#ef55f1']}
```
However you can still easily go back with for loops when the readability is better this way.
In another place, I use this function to generate a Literal from the keys of the palettes.
```python
from enum import StrEnum
class Text(StrEnum):
CONTENT = "Palettes = Literal[\n"
END_CONTENT = "]\n"
...# rest of the class
def generate_palettes_literal() -> None:
literal_content: str = Text.CONTENT
for name in get_palettes().iter_keys().sort().unwrap():
literal_content += f' "{name}",\n'
literal_content += Text.END_CONTENT
...# rest of the function
```
Since I have to reference the literal_content variable in the for loop, This is more reasonnable to use a for loop here rather than a map + reduce approach.
Raw data
{
"_id": null,
"home_page": null,
"name": "pyochain",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.12",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/2d/fb/9fbd1b48135d334ce05d5b6a55d4d580bbfe3c84208e31fed68e326b19aa/pyochain-0.5.42.tar.gz",
"platform": null,
"description": "# pyochain \u26d3\ufe0f\n\n**_Functional-style method chaining for Python data structures._**\n\n`pyochain` brings a fluent, declarative API inspired by Rust's `Iterator` and DataFrame libraries like Polars to your everyday Python iterables and dictionaries.\n\nManipulate data through composable chains of operations, enhancing readability and reducing boilerplate.\n\n## Notice on Stability \u26a0\ufe0f\n\n`pyochain` is currently in early development (< 1.0), and the API may undergo significant changes multiple times before reaching a stable 1.0 release.\n\n## Installation\n\n```bash\nuv add pyochain\n```\n\n## API Reference \ud83d\udcd6\n\nThe full API reference can be found at:\n<https://outsquarecapital.github.io/pyochain/>\n\n## Overview\n\n### Philosophy\n\n* **Declarative over Imperative:** Replace explicit `for` and `while` loops with sequences of high-level operations (map, filter, group, join...).\n* **Fluent Chaining:** Each method transforms the data and returns a new wrapper instance, allowing for seamless chaining.\n* **Lazy and Eager:** `Iter` operates lazily for efficiency on large or infinite sequences, while `Seq` represents materialized sequences for eager operations.\n* **100% Type-safe:** Extensive use of generics and overloads ensures type safety and improves developer experience.\n* **Documentation-first:** Each method is thoroughly documented with clear explanations, and usage examples. Before any commit is made, each docstring is automatically tested to ensure accuracy. This also allows for a convenient experience in IDEs, where developers can easily access documentation with a simple hover of the mouse.\n* **Functional paradigm:** Design encourages building complex data transformations by composing simple, reusable functions on known buildings blocks, rather than implementing customs classes each time.\n\n### Inspirations\n\n* **Rust's language and Rust `Iterator` Trait:** Emulate naming conventions (`from_()`, `into()`) and leverage concepts from Rust's powerful iterator traits (method chaining, lazy evaluation) to bring similar expressiveness to Python.\n* **Python iterators libraries:** Libraries like `rolling`, `cytoolz`, and `more-itertools` provided ideas, inspiration, and implementations for many of the iterator methods.\n* **PyFunctional:** Although not directly used (because I started writing pyochain before discovering it), also shares similar goals and ideas.\n\n### Core Components\n\n#### `Iter[T]`\n\nTo instantiate it, wrap a Python `Iterator` or `Generator`, or take any Iterable (`list`, `tuple`, etc...) and call Iter.from_ (which will call the builtin `iter()` on it).\n\nAll operations that return a new `Iter` are **lazy**, consuming the underlying iterator on demand.\n\nProvides a vast array of methods for transformation, filtering, aggregation, joining, etc..\n\n#### `Seq[T]`\n\nWraps a Python `Sequence` (`list`, `tuple`...), and represents **eagerly** evaluated data.\n\nExposes a subset of the `Iter` methods who operate on the full dataset (e.g., `sort`, `union`) or who aggregate it.\n\nIt is most useful when you need to reuse the data multiple times without re-iterating it.\n\nUse `.iter()` to switch back to lazy processing.\n\n#### `Dict[K, V]`\n\nWraps a Python `dict` (or any Mapping via ``Dict.from_``) and provides chainable methods specific to dictionaries (manipulating keys, values, items, nesting, joins, grouping).\n\nPromote immutability by returning new `Dict` instances on each operation, and avoiding in-place modifications.\n\nCan work easily on known data structure (e.g `dict[str, int]`), with methods like `map_values`, `filter_keys`, etc., who works on the whole `dict` in a performant way, mostly thanks to `cytoolz` functions.\n\nBut `Dict` can work also as well as on \"irregular\" structures (e.g., `dict[Any, Any]`, TypedDict, etc..), by providing a set of utilities for working with nested data, including:\n\n* `pluck` to extract multiple fields at once.\n* `flatten` to collapse nested structures into a single level.\n\n#### `Wrapper[T]`\n\nA generic wrapper for any Python object, allowing integration into `pyochain`'s fluent style using `pipe`, `apply`, and `into`.\n\nCan be for example used to wrap numpy arrays, json outputs from requests, or any custom class instance, as a way to integrate them into a chain of operations, rather than breaking the chain to reference intermediate variables.\n\n### Core Piping Methods\n\nAll wrappers inherit from `CommonBase`:\n\n* `into[**P, R](func: Callable[Concatenate[T, P]], *args: P.args, **kwargs: P.kwargs) -> R`\n Passes the **unwrapped** data to `func` and returns the raw result (terminal).\n* `apply[**P, R](func: Callable[Concatenate[T, P]], *args: P.args, **kwargs: P.kwargs) -> \"CurrentWrapper\"[R]`\n Passes the **unwrapped** data to`func` and **re-wraps** the result for continued chaining.\n* `pipe[**P, R](func: Callable[Concatenate[Self, P]], *args: P.args, **kwargs: P.kwargs) -> R`\n Passes the **wrapped instance** (`self`) to `func` and returns the raw result (can be terminal).\n* `println()`\n Prints the unwrapped data and returns `self`.\n\n### Rich Lazy Iteration (`Iter`)\n\nLeverage dozens of methods inspired by Rust's `Iterator`, `itertools`, `cytoolz`, and `more-itertools`.\n\n```python\nimport pyochain as pc\n\nresult = (\n pc.Iter.from_count(1) # Infinite iterator: 1, 2, 3, ...\n .filter(lambda x: x % 2 != 0) # Keep odd numbers: 1, 3, 5, ...\n .map(lambda x: x * x) # Square them: 1, 9, 25, ...\n .take(5) # Take the first 5: 1, 9, 25, 49, 81\n .into(list) # Consume into a list\n)\n# result: [1, 9, 25, 49, 81]\n```\n\n### Typing enforcement\n\nEach method and class make extensive use of generics, type hints, and overloads (when necessary) to ensure type safety and improve developer experience.\n\nSince there's much less need for intermediate variables, the developper don't have to annotate them as much, whilst still keeping a type-safe codebase.\n\n### Convenience mappers: itr and struct\n\nOperate on iterables of iterables or iterables of dicts without leaving the chain.\n\n```python\nimport pyochain as pc\n\nnested = pc.Iter.from_([[1, 2, 3], [4, 5]])\ntotals = nested.itr(lambda it: it.sum()).into(list)\n# [6, 9]\n\nrecords = pc.Iter.from_(\n [\n {\"name\": \"Alice\", \"age\": 30},\n {\"name\": \"Bob\", \"age\": 25},\n ]\n)\nnames = records.struct(lambda d: d.pluck(\"name\").unwrap()).into(list)\n# ['Alice', 'Bob']\n```\n\n## Key Dependencies and credits\n\nMost of the computations are done with implementations from the `cytoolz`, `more-itertools`, and `rolling` libraries.\n\nAn extensive use of the `itertools` stdlib module is also to be noted.\n\npyochain acts as a unifying API layer over these powerful tools.\n\n<https://github.com/pytoolz/cytoolz>\n\n<https://github.com/more-itertools/more-itertools>\n\n<https://github.com/ajcr/rolling>\n\nThe stubs used for the developpement, made by the maintainer of pyochain, can be found here:\n\n<https://github.com/py-stubs/cytoolz-stubs>\n\n---\n\n## Real-life simple example\n\nIn one of my project, I have to introspect some modules from plotly to get some lists of colors.\n\nI want to check wether the colors are in hex format or not, and I want to get a dictionary of palettes.\nWe can see here that pyochain allow to keep the same style than polars, with method chaining, but for plain python objects.\n\nDue to the freedom of python, multiple paradigms are implemented across libraries.\n\nIf you like the fluent, functional, chainable style, pyochain can help you to keep it across your codebase, rather than mixing object().method().method() and then another where it's [[... for ... in ...] ... ].\n\n```python\n\nfrom types import ModuleType\n\nimport polars as pl\nimport pyochain as pc\nfrom plotly.express.colors import cyclical, qualitative, sequential\n\n\n\nMODULES: set[ModuleType] = {\n sequential,\n cyclical,\n qualitative,\n}\n\ndef get_palettes() -> pc.Dict[str, list[str]]:\n clr = \"color\"\n scl = \"scale\"\n df: pl.DataFrame = (\n pc.Iter.from_(MODULES)\n .map(\n lambda mod: pc.Dict.from_object(mod)\n .filter_values(lambda v: isinstance(v, list))\n .unwrap()\n )\n .into(pl.LazyFrame)\n .unpivot(value_name=clr, variable_name=scl)\n .drop_nulls()\n .filter(\n pl.col(clr)\n .list.eval(pl.element().first().str.starts_with(\"#\").alias(\"is_hex\"))\n .list.first()\n )\n .sort(scl)\n .collect()\n )\n keys: list[str] = df.get_column(scl).to_list()\n values: list[list[str]] = df.get_column(clr).to_list()\n return pc.Iter.from_(keys).with_values(values)\n\n\n# Ouput excerpt:\n{'mygbm_r': ['#ef55f1',\n '#c543fa',\n '#9139fa',\n '#6324f5',\n '#2e21ea',\n '#284ec8',\n '#3d719a',\n '#439064',\n '#31ac28',\n '#61c10b',\n '#96d310',\n '#c6e516',\n '#f0ed35',\n '#fcd471',\n '#fbafa1',\n '#fb84ce',\n '#ef55f1']}\n```\n\nHowever you can still easily go back with for loops when the readability is better this way.\n\nIn another place, I use this function to generate a Literal from the keys of the palettes.\n\n```python\n\nfrom enum import StrEnum\n\nclass Text(StrEnum):\n CONTENT = \"Palettes = Literal[\\n\"\n END_CONTENT = \"]\\n\"\n ...# rest of the class\n\ndef generate_palettes_literal() -> None:\n literal_content: str = Text.CONTENT\n for name in get_palettes().iter_keys().sort().unwrap():\n literal_content += f' \"{name}\",\\n'\n literal_content += Text.END_CONTENT\n ...# rest of the function\n```\n\nSince I have to reference the literal_content variable in the for loop, This is more reasonnable to use a for loop here rather than a map + reduce approach.\n",
"bugtrack_url": null,
"license": null,
"summary": "Method chaining for iterables and dictionaries in Python.",
"version": "0.5.42",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "ef811e94011559861b67bfce2022c96dfbd6b59cac840090338cec8a09e21407",
"md5": "bdff0705fd734b10b614d1a92c52f366",
"sha256": "788c44420bdc64b9a9a07faedecf091046a8f6e73cf03545b1deda1880db96d3"
},
"downloads": -1,
"filename": "pyochain-0.5.42-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bdff0705fd734b10b614d1a92c52f366",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.12",
"size": 63170,
"upload_time": "2025-11-11T17:35:42",
"upload_time_iso_8601": "2025-11-11T17:35:42.289357Z",
"url": "https://files.pythonhosted.org/packages/ef/81/1e94011559861b67bfce2022c96dfbd6b59cac840090338cec8a09e21407/pyochain-0.5.42-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "2dfb9fbd1b48135d334ce05d5b6a55d4d580bbfe3c84208e31fed68e326b19aa",
"md5": "b36296c9a334b045ff560b94eabc2778",
"sha256": "7be1f1800d5b3abb9bdd7a5d55ad5314240f4fe3f17e11f4dec8529b38512a74"
},
"downloads": -1,
"filename": "pyochain-0.5.42.tar.gz",
"has_sig": false,
"md5_digest": "b36296c9a334b045ff560b94eabc2778",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.12",
"size": 46211,
"upload_time": "2025-11-11T17:35:43",
"upload_time_iso_8601": "2025-11-11T17:35:43.376459Z",
"url": "https://files.pythonhosted.org/packages/2d/fb/9fbd1b48135d334ce05d5b6a55d4d580bbfe3c84208e31fed68e326b19aa/pyochain-0.5.42.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-11-11 17:35:43",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pyochain"
}