namedranges
This lib provides a simple way to work with intervals/ranges in Python using a tuple representation for each interval and a string annotation.
Installation
pip install namedranges
Usage
Core classes
The library provides three core classes:
Segment— a single contiguous range(start, end), parseable from tuples, strings, or other Segments.NamedRange— a named collection ofSegmentobjects (e.g. a range "A" with segments(1, 10)and(12, 13)).NamedRanges— the main collection class holding multipleNamedRangeobjects.
Constructing NamedRanges
The most common way to use this library is to pass a dict mapping names to lists of segments:
from namedranges import NamedRanges
nr = NamedRanges.from_segments({
"A": [(1, 10), (12, 13)],
"B": ["30-35", "45-50"],
})
print(nr["A"].segments) # [Segment(1, 10), Segment(12, 13)]
print(nr.to_dict()) # {"A": [(1, 10), (12, 13)], "B": [(30, 35), (45, 50)]}
For simple one-segment-per-name cases, use from_flat_dict:
nr = NamedRanges.from_flat_dict({
"1": (1, 5),
"2": (6, 22),
"3": (23, 26),
"4": (27, 38),
})
Or use from_dict which auto-detects the format:
# Detected as flat (values are tuples):
nr = NamedRanges.from_dict({"1": (1, 5), "2": (6, 22)})
# Detected as segmented (values are lists):
nr = NamedRanges.from_dict({"A": [(1, 10), (12, 13)]})
Preconfigured classes with NamedRangeFactory
Use NamedRangeFactory to create a NamedRanges subclass with default settings for indexing style, interval openness, etc.:
from namedranges import NamedRangeFactory
OneIndexed = NamedRangeFactory(indexing=1, right_side_closed=True)
nr = OneIndexed.from_segments({"X": ["1-5", "10-20"]})
print(nr.args.indexing) # 1
Operations
from namedranges import NamedRanges, namedrange_args
nr = NamedRanges.from_flat_dict({
"1": (1, 5),
"2": (6, 22),
"3": (23, 26),
"4": (27, 38),
})
# Insert gaps (splits ranges that overlap with gap positions):
nr.add_gaps([(10, 10)])
print(nr.to_flat_dict())
# {'1': (1, 5), '2': (6, 9), '2-1': (11, 22), '3': (23, 26), '4': (27, 38)}
# Compute the complement (gaps between ranges):
nr = NamedRanges.from_flat_dict({"1": (1, 5), "2": (6, 22)}, namedrange_args(indexing=1))
complement = nr.complement()
# Reindex ranges to start from 0 or 1:
nr = NamedRanges.from_flat_dict({"1": (10, 15), "2": (21, 30)},
namedrange_args(indexing=1, right_side_closed=True))
reindexed = nr.reindex(keep_gaps=True)
# Sort ranges:
sorted_ranges = nr.sorted()
Working with Segment and NamedRange
from namedranges import Segment, NamedRange
# Parse from various formats:
seg = Segment.parse("1-10") # Segment(1, 10)
seg = Segment.parse((1, 10)) # Segment(1, 10)
# Useful methods:
seg.to_tuple() # (1, 10)
seg.to_list() # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
seg.length() # 10
# NamedRange groups segments under a name:
nr = NamedRange.parse("A", [(1, 10), "12-13", Segment(20, 25)])
nr.to_tuples() # [(1, 10), (12, 13), (20, 25)]
Utility functions
from namedranges import list_to_ranges, ranges_to_list
# Convert a list of integers to range expressions:
list_to_ranges([2, 3, 8, 15, 16, 17, 18, 20, 23, 24, 25])
# ['2-3', '8-8', '15-18', '20-20', '23-25']
# Convert range expressions back to lists:
ranges_to_list(['2-3', '8-8', '15-18'], flatten=True)
# [2, 3, 8, 15, 16, 17, 18]
Metadata
Release files for namedranges 1.0.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 |
|---|---|---|---|---|
| namedranges-1.0.0.tar.gz | 7.3 kB | Without provenance | Details for namedranges-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| namedranges-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for namedranges-1.0.0-py3-none-any.whl |
Total release size: 15.5 kB
Release files / namedranges-1.0.0.tar.gz
| Download URL | namedranges-1.0.0.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
442b47d1d3574eab9d6b9f1300fda7958e2aab73b3dc3b7ac87b1b57c4521c13
|
|
BLAKE2b-256 checksum How to use checksums |
82d7597874511d63702744388d11b811bb39c7898073a4322d41d02da3576054
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Release files / namedranges-1.0.0-py3-none-any.whl
| Download URL | namedranges-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bb639e66fbe78da6fc7b8afe835c36e1e927ffa93ade7d27a06e669a649d8f0d
|
|
BLAKE2b-256 checksum How to use checksums |
22c763a12420a743fde2e8e8cea34bb563d53e10517df59c14e6d502bfef48f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Security for namedranges 1.0.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.