Python class decorator that adds selection chaining
Project description
ChainGang
Tuple-style indexing for nested lists — obj[i, j, k] means obj[i][j][k] for get, set, and delete.
Overview
ChainGang exposes small class decorators that wrap __getitem__, __setitem__, and __delitem__ so that a tuple key walks nested containers in one step. Non-tuple keys (single indexes, slices) are passed through to the base type unchanged.
| Decorator | Effect |
|---|---|
selection_chaining |
Chained get, set, and delete |
getitem_chaining |
Chained read only |
setitem_chaining |
Chained write only |
delitem_chaining |
Chained delete only |
Python 3.10+. No runtime dependencies beyond the standard library.
Install
pip install chaingang
Install from source:
git clone https://github.com/eddiethedean/chaingang.git
cd chaingang
pip install .
Optional dev tools: pip install chaingang[testing]
Usage
Apply @selection_chaining to a class that already supports nested indexing (typically a list subclass). Then use comma-separated indices anywhere you would otherwise chain brackets.
from chaingang import selection_chaining
@selection_chaining
class ChainList(list):
"""Nested list with tuple indexing."""
pass
cl = ChainList([[1, 2, 3], [4, 5, 6]])
# Read: tuple index equals chained brackets
assert cl[1, 2] == 6
assert cl[1][2] == 6
# Write
cl[1, 2] = 100
assert cl == [[1, 2, 3], [4, 5, 100]]
# Delete (here, remove the middle element of the inner row)
del cl[1, 1]
assert cl == [[1, 2, 3], [4, 100]]
You can compose the lower-level decorators if you only need one protocol, for example:
from chaingang.selection_chaining import getitem_chaining
ReadOnlyChain = getitem_chaining(list)
Import the combined decorator from the package root or from chaingang.selection_chaining.
Tuple keys and compatibility
In Python, obj[a, b] always passes key as the tuple (a, b). These decorators always treat a tuple key as a chain of lookups or updates. That matches nested lists (and similar sequence-like types).
Avoid using the decorators on types where a tuple is already a single valid key (for example dict with tuple keys, or NumPy-style indexing). Tuple keys are reserved for chaining and will not match the base mapping or array semantics.
An empty tuple () is rejected with TypeError (it would otherwise be meaningless for set/delete).
Type checkers
The package ships py.typed (PEP 561). Point mypy, Pyright, or other tools at your environment after pip install chaingang.
Development
pip install -r requirements_dev.txt
pytest
# or full lint + tests as in CI:
tox
Changelog and links
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chaingang-0.1.0.tar.gz.
File metadata
- Download URL: chaingang-0.1.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e2661f423cff33b441e638439abfef4d9fc3042e5e1154b610f4d687347b48a
|
|
| MD5 |
644764d1c783e8a2573f65810a9b526f
|
|
| BLAKE2b-256 |
24053c0c374eb5167d339f8be8610fcd59cc6798492aff5efa0bbddc207354b1
|
File details
Details for the file chaingang-0.1.0-py3-none-any.whl.
File metadata
- Download URL: chaingang-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9707b60682d8ef2e74749c7059dd743662128a0cd30bc4e8b17b3e44044f1532
|
|
| MD5 |
2c185570a0f70dd638a7d4d6060469fb
|
|
| BLAKE2b-256 |
9e8ef5953995cf3e7a4943cb6e41a1f9a8bf431c0e3e6c139757587014d57f9c
|