Skip to main content

rangy helps you with the efficience of shell scripts in Python.

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 in operator.
  • Easy Validation: Validate if a given count satisfies a Rangy's specification with the .validate() method.
  • Clear Value Access: Use .value for exact counts and .values for ranges.
  • Intelligent Distribution (via distribute function): 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

MIT License

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

rangy-0.0.6.post2.tar.gz (40.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rangy-0.0.6.post2-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file rangy-0.0.6.post2.tar.gz.

File metadata

  • Download URL: rangy-0.0.6.post2.tar.gz
  • Upload date:
  • Size: 40.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.20

File hashes

Hashes for rangy-0.0.6.post2.tar.gz
Algorithm Hash digest
SHA256 e08529c6811ee2cd0394eb824ed18ccd4103df20c8c99211433ae0c67bc4ed4f
MD5 06ebe693319b7530fb3121d6b9473b9f
BLAKE2b-256 d515b0f908c6b3e040f068b40fbab3cafdf87715d4d06347cbfd9a3fee3ff661

See more details on using hashes here.

File details

Details for the file rangy-0.0.6.post2-py3-none-any.whl.

File metadata

  • Download URL: rangy-0.0.6.post2-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.20

File hashes

Hashes for rangy-0.0.6.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 addcf30fcc3016ce655bb219897251a4a5b743910037b1cf3cc38fadf9b46618
MD5 7de82a1cc948a2ba828e939dfa90138d
BLAKE2b-256 1312f97b86eeffa5bc53a92cce28da135e0b77a22567a50b0aafda686439b7f0

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