Skip to main content

seltools

Tests

Reusable selections and wrap-around access for anything with len() and [].

itertools and more_itertools cannot wrap around: going past the end needs the length of the data and random access to its start, and an iterator has neither. seltools asks for exactly those two things, so a list, tuple, str, range, NumPy array or any class defining __len__ and __getitem__ gives the same elements.

>>> from seltools import circular_getitem, circular_pairwise, circular_windowed
>>> circular_getitem("ABC", 4)
'B'
>>> list(circular_pairwise("ABCD"))
[('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'A')]
>>> list(circular_windowed("ABCD", 3))
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'A'), ('D', 'A', 'B')]

Selection

Selection(key) keeps an index as a value, the way a region of interest keeps its bounds: build it once, apply it to any container, store or compare it.

>>> import numpy as np
>>> from seltools import Selection
>>> roi = Selection((slice(1, 3), slice(0, 2)))
>>> roi(np.arange(12).reshape(3, 4))
array([[4, 5],
       [8, 9]])
>>> ends = Selection([0, -1])
>>> ends("abc"), ends(np.array([10, 20, 30]))
(['a', 'c'], array([10, 30]))
>>> Selection([True, False, True])("abc")
['a', 'c']

Any key the container accepts is passed through as data[key], so ndarray slices stay views. A list or array of ints or bools (fancy indexing) also works on list, tuple, str, range and anything else that takes a single index; the picks then come back as a list.

Install

Not published to PyPI yet: seltools and seltools-rs both return 404 on PyPI (checked 2026-10-23). publish.yml uploads both when a GitHub release is published for a v* tag. Until then, install the pure-Python package from a clone:

pip install .

Once published, pip install seltools is the pure-Python package with no dependencies, and pip install "seltools[rust]" adds the compiled iterators from seltools-rs.

Requires Python 3.14.

From a clone of this repository, install it into a throwaway environment and check that it imports and wraps around. The command exits non-zero if it does not.

uv run --with . python -c "from seltools import circular_getitem; assert circular_getitem('ABC', 4) == 'B'"

Tools

Tool Results Non-wrapping version
circular_getitem(data, i) data[i % len(data)] operator.getitem
circular_pairwise(data) (d[0], d[1]), ..., (d[-1], d[0]) itertools.pairwise
circular_windowed(data, n) one n-tuple starting at each index more_itertools.windowed

Indexable[T] is the typing.Protocol they accept: __len__ and __getitem__ with an int.

All tools follow the same rules:

  • Each name is circular_ + the name of the non-wrapping version, with the same arguments in the same order.
  • Elements come back exactly as data[i] returns them. Nothing is converted: an ndarray row is still a view.
  • Multi-element results are lazy iterators that read each index once per lap, and raise the same errors as data[i] would.

Other non-wrapping tools are left to the libraries that already have them: more_itertools.argmax / argmin, more_itertools.sliced, more_itertools.circular_shifts.

Rust iterators

seltools.core is the reference implementation, built from itertools so its loops run in C. The seltools-rs package provides the same circular_pairwise and circular_windowed as a PyO3 extension, _seltools; seltools uses it when it is installed, the way heapq uses _heapq. Both are tested against each other for results, errors, laziness and the number of data[i] reads. The extension supports free-threaded builds.

Measured on Windows with CPython 3.14, for a full iteration:

Iteration n Pure Python Rust
pairwise, list 4 1.06 µs 0.46 µs
pairwise, ndarray rows 4 1.99 µs 0.97 µs
windowed 3, list 4 2.53 µs 0.54 µs
pairwise, list 1000 67 µs 60 µs
pairwise, ndarray rows 1000 212 µs 152 µs
windowed 3, list 1000 100 µs 62 µs

ndarray rows stay slow in both because ndarray.__getitem__ itself costs about 0.15 µs.

Documentation

The full reference, with "roughly equivalent" code for each tool and tested recipes (polygon area, perimeter, convexity, ring neighbours, periodic moving average), is in docs/, written for Sphinx with the theme of docs.python.org.

Development

Uses uv. The Rust extension needs a Rust toolchain.

uv run --exact pytest                    # pure Python
uv run --exact --extra rust pytest       # pure Python and Rust
uv run --exact --group docs --group dev sphinx-build -W -b doctest docs docs/_build/doctest
uv run --exact --group docs --group dev sphinx-build -W -b html docs docs/_build/html

seltools and seltools-rs are released together with the same version; the rust extra pins it exactly.

uv build                                        # seltools sdist and wheel
cd rust && uvx maturin build --release --sdist  # seltools-rs, one wheel per interpreter
src/seltools/
  core.py         Indexable, circular_getitem, circular_pairwise, circular_windowed
  __init__.py     re-exports, swapping in _seltools when installed
rust/             seltools-rs: src/lib.rs, _seltools.pyi
docs/             Sphinx sources
test_seltools.py

License

MIT. Citation metadata is in CITATION.cff.

Metadata

Release files for seltools 0.2.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 seltools 0.2.0
File Size Uploaded Provenance Actions
seltools-0.2.0.tar.gz 6.4 kB With provenance Details for seltools-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 13.5 kB

Release files / seltools-0.2.0.tar.gz

Download URL seltools-0.2.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a6efebd87eeaeec96afe733afebb237e92f3076a349c6b8b6757ad91ea606a53
BLAKE2b-256 checksum
How to use checksums
9db84cd35d707281650422ab4532553178c895ecd0738c2e0ed2f5f5a8715ac2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / seltools-0.2.0-py3-none-any.whl

Download URL seltools-0.2.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fad4aa1069dd9c485f07f439d69b6a18c1e4aa40fb268e26f647df63f8e82a88
BLAKE2b-256 checksum
How to use checksums
388648ac59be819ed7e1a075eab4fcb41eb72c5319b257502e3a2cc511a44532
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Security for seltools 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for seltools-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

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