intranges
Typed, zero-dependency conversion between range strings like 1-3,5,7-9 and integers,
with a lazy set type that never expands huge ranges, named dialects for real-world
syntaxes, typed errors that point at the bad token, and a command-line tool.
Install
pip install intranges
Usage
from intranges import compact, parse
parse("1-5,8,10-12")
# [1, 2, 3, 4, 5, 8, 10, 11, 12]
compact([6, 5, 1, 1, 3, 2])
# "1-3,5-6"
parse("-3--1, 0, 2")
# [-3, -2, -1, 0, 2]
parse("1..3;6", range_sep="..", list_sep=";")
# [1, 2, 3, 6]
parse keeps input order and repeats. It refuses to build more than 10,000,000 values
by default (max_size=), and it checks that before allocating anything.
A set that never expands
from intranges import IntRanges, parse_ranges, count, normalize
r = parse_ranges("0-10000000000,5") # two spans, not ten billion ints
r.count # 10000000001
7_000_000_000 in r # True, binary search
r.spans # ((0, 10000000000),)
free = IntRanges.parse("0-3,8-11").complement(0, 15)
str(free) # "4-7,12-15"
a, b = IntRanges.parse("1-10"), IntRanges.parse("5-15")
str(a & b), str(a - b), str(a ^ b) # ("5-10", "1-4", "1-4,11-15")
count("1-1000000,500000-2000000") # 2000000
normalize("8, 1-3, 2, 4") # "1-4,8"
IntRanges is an immutable collections.abc.Set[int]: in, iteration (ascending and
reversed), len, |, &, -, ^, subset tests, first, last, complement,
hashing and pickling all work on spans.
Dialects
| Dialect | Example | Source |
|---|---|---|
DEFAULT |
-3--1,5,9- (with upper=) |
this library |
PAGES |
1-4,7,9-12 |
CUPS -o page-ranges=1-4,7,9-12, Command-Line Printing and Options |
CPULIST |
0-3,8-11, 100-2000:2/25, 16-N, all |
Linux kernel, kernel-parameters: CPU lists |
CPULIST |
0-10:3 (stride) |
util-linux taskset(1): "0-10:3 is interpreted as 0,3,6,9" |
SLURM |
0-15:4, [1-3,5], 001-010 |
sbatch --array, hostlist numbers as in tux[1-3] from scontrol |
HTTP |
bytes=0-499,9500-, -500 |
RFC 9110 section 14.1.2, Byte Ranges |
from intranges import CPULIST, HTTP, SLURM, parse, parse_ranges, compact
parse("100-200:2/25", dialect=CPULIST) # [100, 101, 125, 126, 150, 151, 175, 176, 200]
parse("16-N", dialect=CPULIST, upper=19) # [16, 17, 18, 19]
parse("0-15:4", dialect=SLURM) # [0, 4, 8, 12]
parse_ranges("bytes=-500", dialect=HTTP, upper=9999).spans # ((9500, 9999),)
compact(range(1, 11), dialect=SLURM, width=3) # "001-010"
Open ends such as "5-" are accepted only when you pass upper=. Unsupported parts of
a source syntax are rejected rather than guessed, for example Slurm's % throttle
suffix. The HTTP dialect clamps ends past upper as RFC 9110 describes; upper is the
representation length minus one.
Errors
Every error is a RangeError, a ValueError subclass, carrying text, token and
offset:
from intranges import RangeError, parse
try:
parse("1-3, 5, x-9")
except RangeError as err:
print(err)
print(err.pointer())
prints
invalid range token 'x-9' for the default dialect at offset 8
1-3, 5, x-9
^^^
Subclasses: RangeSyntaxError, DescendingRangeError, RangeOverlapError,
RangeOrderError, RangeBoundsError, RangeSizeError. Overlapping and unsorted tokens
are merged by default; pass on_overlap="reject" or on_unsorted="reject" to
parse_ranges to refuse them.
Command line
intranges expand "1-3,7" # 1 2 3 7, one per line
intranges expand "0-10:3" --dialect cpulist -d , # 0,3,6,9
seq 1 5 | intranges compress # 1-5
intranges normalize "8, 1-3, 2, 4" # 1-4,8
intranges count "0-10000000000" # 10000000001
intranges contains "0-3,8-11" 9 12 # 9 yes, 12 no; exit code 2
intranges complement "0-3,8-11" --lower 0 --upper 15 # 4-7,12-15
Errors go to stderr with exit code 1. Put -- before a range string that starts with
-. See intranges --help and the guide for everything else.
Why
Expressing a set of integers as a compact string like 1-5,8,10-12 is common in page
selectors, CPU affinity, job arrays and config files, but converting it back and forth
usually ends up reimplemented in each project. intspan covers the idea with a
set-like class, yet it is untyped (it ships no py.typed) and expands its ranges in
memory. intranges is strictly typed, keeps ranges as spans, and follows documented
syntaxes instead of inventing new ones.
License
MIT. See LICENSE.
Links
- Website: https://lianes8studio.com
- GitHub: https://github.com/JaviLianes8
- LinkedIn: https://www.linkedin.com/in/jlianes/
Metadata
Release files for intranges 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| intranges-0.2.0.tar.gz | 30.6 kB | Without provenance | Details for intranges-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| intranges-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for intranges-0.2.0-py3-none-any.whl |
Total release size: 51.1 kB
Release files / intranges-0.2.0.tar.gz
| Download URL | intranges-0.2.0.tar.gz |
|---|---|
| Size | 30.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a1b76e513d51bab00b36749666fd5d270c94b61268b1c7215cca4c757469d0a5
|
|
BLAKE2b-256 checksum How to use checksums |
bd8a2a91193d92bc5645fead3c2ecd357608d1f9260ce411a2d93e3802490051
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / intranges-0.2.0-py3-none-any.whl
| Download URL | intranges-0.2.0-py3-none-any.whl |
|---|---|
| Size | 20.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9d91b3802f752ef4cd29658ccc5a15fc316da36fc1d504da809b811ef55bf6c5
|
|
BLAKE2b-256 checksum How to use checksums |
65d5c59f843ea1f518e39963ddbf119f156897d474298efd1b7685abfb014a69
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for intranges 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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.