Summarize contiguous ranges of integers found on standard input.
Project description
Summarize contiguous numeric (integer) ranges found on standard input
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).
Installation
$ pip install summarize-ranges
Works with Python 3.8 through 3.14.
Options
--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.
Example usage
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 35
Python API
There 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
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.
Possible enhancements
- 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).
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
File details
Details for the file summarize_ranges-0.1.1.tar.gz.
File metadata
- Download URL: summarize_ranges-0.1.1.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ee9d5cfbc1dee2688e76bafb24b202d6c30201c2cba410370fda7506fc4edfc
|
|
| MD5 |
c3d7bd233c388f6151ed3bcfbaf91154
|
|
| BLAKE2b-256 |
79ce28aae7249b5801ac278ae1c5083dd141822ba041d060ad5c62d18ae588b3
|