rangy


Namerangy JSON
Version 1.0.1 PyPI version JSON
download
home_pageNone
SummaryWork with integer ranges with ease
upload_time2024-12-27 22:36:44
maintainerNone
docs_urlNone
authorArthur Debert
requires_python>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](https://rangy.readthedocs.io/en/latest/index.html).

## 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:

```bash
pip install rangy
```

## Usage

### Defining Rangy Objects

```python
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

```python
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

```python
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](https://github.com/pytest-dev/pytest), makers of happy lives.

### License

[MIT License][def]

[def]: ./LICENSE


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "rangy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Arthur Debert",
    "author_email": "arthur@debert.xyz",
    "download_url": "https://files.pythonhosted.org/packages/a4/f3/bdb0c6ab4df60af95f46c77bccddfdfa9345f01a77bc5043b41bd54db52f/rangy-1.0.1.tar.gz",
    "platform": null,
    "description": "# rangy\n\nRangy 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`).\n\nFull docs at [Rangy Documentation](https://rangy.readthedocs.io/en/latest/index.html).\n\n## Features\n\n* **Expressive Range Definitions:** Define counts as exact values (`4`), ranges (`\"2-4\"`, `\"2-*\"`, `\"+\"`), or unbounded (`\"*\"`).\n* **Intuitive Comparisons:** Compare Rangy objects with integers using standard comparison operators (e.g., `<`, `<=`, `>`, `>=`, `==`, `!=`).\n* **Membership Testing:** Check if an integer falls within a Rangy's defined range using the `in` operator.\n* **Easy Validation:** Validate if a given count satisfies a Rangy's specification with the `.validate()` method.\n* **Clear Value Access:** Use `.value` for exact counts and `.values` for ranges.\n* **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.\n\n## Installation\n\nYou can install rangy using pip:\n\n```bash\npip install rangy\n```\n\n## Usage\n\n### Defining Rangy Objects\n\n```python\nfrom rangy import Rangy\n\n# Exact count\nexact_count = Rangy(4)  # or Rangy(\"4\")\n\n# Range count\nrange_count = Rangy(\"2-4\")  # or Rangy((2, 4)) or Rangy((\"2\", \"4\"))\n\n# Unbounded count (any non-negative integer)\nany_count = Rangy(\"*\")\n\n# Unbounded count (at least one)\nat_least_one = Rangy(\"+\")\n\n# Open-ended range\nopen_range = Rangy(\"2-*\") # 2 or more\n```\n\n### Comparison and Validation\n\n```python\ncount = Rangy(\"1-3\")\n\nprint(2 in count)  # True\nprint(4 in count)  # False\n\nprint(count.validate(2))  # True\nprint(count.validate(0))  # False\n\nprint(count < 4)  # True (compares against the maximum value of the range)\n\nprint(count == 2) # False - the equality against an integer checks if rangy covers only that integer.\nprint(count == Rangy(\"1-3\")) # True\n\n```\n\n### Distributing Items with distribute\n\n```python\nfrom rangy import Rangy, distribute\n\nitems = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\ncounts = [Rangy(1), Rangy(\"2-4\"), Rangy(\"*\")]\n\nresult = distribute(items, counts)\nprint(result)  # Output: [[1], [2, 3, 4], [5, 6, 7, 8, 9, 10]]\n\n\nitems_with_separator = [1, 2, \"--\", 3, 4, 5, 6, \"--\", 7, 8, 9, 10]\ncounts_with_separator = [Rangy(\"1-2\"), Rangy(\"4-6\"), Rangy(\"2-5\")]\n\nresult_with_separator = distribute(items_with_separator, counts_with_separator)\nprint(result_with_separator)  # Output: [[1, 2], [3, 4, 5, 6], [7, 8, 9, 10]]\n\n\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to open issues or submit pull requests.\n\nTests are done with [pytest](https://github.com/pytest-dev/pytest), makers of happy lives.\n\n### License\n\n[MIT License][def]\n\n[def]: ./LICENSE\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Work with integer ranges with ease",
    "version": "1.0.1",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c4f3b40cd712526b5f7a1cc5fedaefee87db60dcc0fea3feec8d6d92e1255ec5",
                "md5": "8fdec3c0fdeb3fcb1d925d85d7a78452",
                "sha256": "d5f69da037d69331d78126a6941cce9c7ebb6b3550be03854d5226a3da146454"
            },
            "downloads": -1,
            "filename": "rangy-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8fdec3c0fdeb3fcb1d925d85d7a78452",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 15915,
            "upload_time": "2024-12-27T22:36:42",
            "upload_time_iso_8601": "2024-12-27T22:36:42.188749Z",
            "url": "https://files.pythonhosted.org/packages/c4/f3/b40cd712526b5f7a1cc5fedaefee87db60dcc0fea3feec8d6d92e1255ec5/rangy-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a4f3bdb0c6ab4df60af95f46c77bccddfdfa9345f01a77bc5043b41bd54db52f",
                "md5": "b7efa3e89ddbbea08683ad429e318d7f",
                "sha256": "4e9780be0eb33d37916a35927228e2d21476320b2b6ae596a208d7c796499afe"
            },
            "downloads": -1,
            "filename": "rangy-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "b7efa3e89ddbbea08683ad429e318d7f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 12603,
            "upload_time": "2024-12-27T22:36:44",
            "upload_time_iso_8601": "2024-12-27T22:36:44.416606Z",
            "url": "https://files.pythonhosted.org/packages/a4/f3/bdb0c6ab4df60af95f46c77bccddfdfa9345f01a77bc5043b41bd54db52f/rangy-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-27 22:36:44",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "rangy"
}
        
Elapsed time: 0.37061s