A small example package
Project description
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).
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rangy-0.0.1.tar.gz.
File metadata
- Download URL: rangy-0.0.1.tar.gz
- Upload date:
- Size: 21.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88d85405b4ff6e2b40df7d12df0bffcbb9ca69ed8e81b20471de57f0401d3015
|
|
| MD5 |
38159a5087c2a7dd1fc41a708fec0867
|
|
| BLAKE2b-256 |
acb6a27d332d1f9447f2ea2a08571bd98dfcfbb5b19d27ee2f0c796bdbf50d98
|
File details
Details for the file rangy-0.0.1-py3-none-any.whl.
File metadata
- Download URL: rangy-0.0.1-py3-none-any.whl
- Upload date:
- Size: 29.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95d19dcfca1ea819aa2fb771d3042a24a897e3d82c1c4f0c638db23f3a021100
|
|
| MD5 |
324b336653893f09787399330ddd5ecc
|
|
| BLAKE2b-256 |
d7549c7b609164176d65726c32a68af565b73c863e438612a8bd8d07f16c65bd
|