rangy
Rangy is a small but feisty python lib designed to make working with numerical ranges a breeze. It handles both open and closed ranges, provides algorithms for distributing items across ranges, and allows you to treat ranges like numbers in comparisons (e.g., if x < myrange).
Full docs at Rangy Documentation.
Features
- Expressive Range Definitions: Define counts as exact values (
4), ranges ("2-4","2-*","+"), or unbounded ("*"). - Intuitive Comparisons: Compare Rangy objects with integers using standard comparison operators (e.g.,
<,<=,>,>=,==,!=). - Membership Testing: Check if an integer falls within a Rangy's defined range using the
inoperator. - Easy Validation: Validate if a given count satisfies a Rangy's specification with the
.validate()method. - Clear Value Access: Use
.valuefor exact counts and.valuesfor ranges. - Intelligent Distribution (via
distributefunction): Distribute a list of items into sublists according to a set of Rangy specifications, handling both pre-segmented and dynamically divided lists.
Installation
You can install rangy using pip:
pip install rangy
Usage
Defining Rangy Objects
from rangy import Rangy
# Exact count
exact_count = Rangy(4) # or Rangy("4")
# Range count
range_count = Rangy("2-4") # or Rangy((2, 4)) or Rangy(("2", "4"))
# Unbounded count (any non-negative integer)
any_count = Rangy("*")
# Unbounded count (at least one)
at_least_one = Rangy("+")
# Open-ended range
open_range = Rangy("2-*") # 2 or more
Comparison and Validation
count = Rangy("1-3")
print(2 in count) # True
print(4 in count) # False
print(count.validate(2)) # True
print(count.validate(0)) # False
print(count < 4) # True (compares against the maximum value of the range)
print(count == 2) # False - the equality against an integer checks if rangy covers only that integer.
print(count == Rangy("1-3")) # True
Distributing Items with distribute
from rangy import Rangy, distribute
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
counts = [Rangy(1), Rangy("2-4"), Rangy("*")]
result = distribute(items, counts)
print(result) # Output: [[1], [2, 3, 4], [5, 6, 7, 8, 9, 10]]
items_with_separator = [1, 2, "--", 3, 4, 5, 6, "--", 7, 8, 9, 10]
counts_with_separator = [Rangy("1-2"), Rangy("4-6"), Rangy("2-5")]
result_with_separator = distribute(items_with_separator, counts_with_separator)
print(result_with_separator) # Output: [[1, 2], [3, 4, 5, 6], [7, 8, 9, 10]]
Contributing
Contributions are welcome! Please feel free to open issues or submit pull requests.
Tests are done with pytest, makers of happy lives.
License
Metadata
Release files for rangy 1.0.1
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 |
|---|---|---|---|---|
| rangy-1.0.1.tar.gz | 12.6 kB | Without provenance | Details for rangy-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| rangy-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for rangy-1.0.1-py3-none-any.whl |
Total release size: 28.5 kB
Release files / rangy-1.0.1.tar.gz
| Download URL | rangy-1.0.1.tar.gz |
|---|---|
| Size | 12.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4e9780be0eb33d37916a35927228e2d21476320b2b6ae596a208d7c796499afe
|
|
BLAKE2b-256 checksum How to use checksums |
a4f3bdb0c6ab4df60af95f46c77bccddfdfa9345f01a77bc5043b41bd54db52f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.20
|
Release files / rangy-1.0.1-py3-none-any.whl
| Download URL | rangy-1.0.1-py3-none-any.whl |
|---|---|
| Size | 15.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d5f69da037d69331d78126a6941cce9c7ebb6b3550be03854d5226a3da146454
|
|
BLAKE2b-256 checksum How to use checksums |
c4f3b40cd712526b5f7a1cc5fedaefee87db60dcc0fea3feec8d6d92e1255ec5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.20
|
Security for rangy 1.0.1
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.