Skip to main content

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

  • --tsv print TAB-separated high,low range values.
  • --python print closed,open Python ranges.
  • --ignore-word-boundaries (or --iwb) match numbers with no respect for word boundaries (e.g., match the 33 in abc33def).
  • --ignore-negatives (or --in) do not match leading hyphens that would otherwise indicate a negative number. So catch-22 will result in the number 22 being 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 --human
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,low arguments and exit non-zero if the expected range(s) are not found. This could also have a --strict option to check that there are no additional unexpected ranges found and a --quiet option to suppress output (though /dev/null can just be used).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

summarize_ranges-0.1.0.tar.gz (12.7 kB view details)

Uploaded Source

File details

Details for the file summarize_ranges-0.1.0.tar.gz.

File metadata

  • Download URL: summarize_ranges-0.1.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for summarize_ranges-0.1.0.tar.gz
Algorithm Hash digest
SHA256 43ed11ffbcb0ac892b40e73b8fa6071afadbf7565d4c197e088624ce28a199c0
MD5 80e6feeb0db7cdc9479b6b22abb8ccbd
BLAKE2b-256 a7877a8a8b55719d75fd9711d804b8302dd792422f8c40a0ad1cf3c13f84e463

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page