Skip to main content

ChainGang

Tuple-style indexing for nested lists — obj[i, j, k] means obj[i][j][k] for get, set, and delete.

PyPI version Python versions Tests License: MIT

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

License

MIT — see LICENSE.

Metadata

Release files for chaingang 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chaingang 0.1.0
File Size Uploaded
chaingang-0.1.0.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chaingang 0.1.0
File Interpreter ABI Platform
chaingang-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / chaingang-0.1.0.tar.gz

Download URL chaingang-0.1.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0e2661f423cff33b441e638439abfef4d9fc3042e5e1154b610f4d687347b48a
BLAKE2b-256 checksum
How to use checksums
24053c0c374eb5167d339f8be8610fcd59cc6798492aff5efa0bbddc207354b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / chaingang-0.1.0-py3-none-any.whl

Download URL chaingang-0.1.0-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9707b60682d8ef2e74749c7059dd743662128a0cd30bc4e8b17b3e44044f1532
BLAKE2b-256 checksum
How to use checksums
9e8ef5953995cf3e7a4943cb6e41a1f9a8bf431c0e3e6c139757587014d57f9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page