Here's a simple tool you can use to check whether you have one or more expected sets of contiguous integers in a file (or some other input).
$ pip install summarize-rangesWorks with Python 3.8 through 3.14.
--tsvprint TAB-separated high,low range values.--pythonprint closed,open Python ranges.--ignore-word-boundaries(or--iwb) match numbers with no respect for word boundaries (e.g., match the33inabc33def).--ignore-negatives(or--in) do not match leading hyphens that would otherwise indicate a negative number. Socatch-22will result in the number22being collected rather than-22.
You can of course redirect a file or more complicated pipeline into
summarize-ranges. The examples below all just use echo for simplicity.
# One range is found:
$ echo 3 | summarize-ranges
Range 1: 3
$ echo 3 4 5 | summarize-ranges
Range 1: 3 to 5
# TAB-separated output:
$ echo 3 4 5 | summarize-ranges --tsv
3 5
# TAB-separated output for Python:
$ echo 3 4 5 | summarize-ranges --tsv --python
3 6
# Two ranges:
$ echo 3 5 | summarize-ranges
Range 1: 3
Range 2: 5
$ echo some text 3 more 4 text and 6, 7, 8. | summarize-ranges
Range 1: 3 to 4
Range 2: 6 to 8
# Negative numbers are recognized:
$ echo some text -3 and -4. | summarize-ranges
Range 1: -4 to -3
# But leading hyphens in numbers can be ignored:
$ echo some text -3 and -4. | summarize-ranges --ignore-negatives
Range 1: 3 to 4
# Python style closed-open ranges
$ echo 3 | summarize-ranges --python
Range 1: 3 to 4
$ echo 3 4 5 10 11 | summarize-ranges --python
Range 1: 3 to 6
Range 2: 10 to 12
# Ignore word boundaries:
$ echo some33 text34here and 35. | summarize-ranges --ignore-word-boundaries
Range 1: 33 to 35There is a simple Python class, RangeSummarizer you can use to build your
own tools. See src/summarize_ranges/ranges.py and the tests in
test/test_summarizer.py.
Testing is done with pytest and nox.
env PYTHONPATH=src pytest and nox can be used directly. Or, to use the
Makefile, install uv, then make test
or make nox.
- Allow for
--expect high,lowarguments and exit non-zero if the expected range(s) are not found. This could also have a--strictoption to check that there are no additional unexpected ranges found and a--quietoption to suppress output (though/dev/nullcan just be used).