Skip to main content

Parses commonly used range notations to python objects

Project description

py-range-parse Contributors MIT License Code Size https://badge.fury.io/py/py-range-parse Build Status

py-range-parse is a library to parse commonly used range notations to python objects that act like sets.

py-range-parse is used by

and hopefully many others :)

How to use

pip install py-range-parse
from py_range_parse import parse_range

range = parse_range("[0..5]")

or create on manually:

from py_range_parse import Range
range = Range(0,5)

Input formats

When parsing a Range from a str any whitespace is ignored.

int ranges

If both the start and end value are of type int, the resulting Range will only consider integers as part of it. If you want to include float values as well, at least one of the values has to be a float.

  • [-2 .. 5]
  • [10 .. 1]

If the end value is bigger than the start value the resulting range will automatically be inverted. Therefore range.start <= range.end is always True in a Range.

float ranges

A float Range includes every possible float value between the start and end value.

  • [-2.2 .. 5.123]
  • [-2.0 .. 5]

Infinity

Infinity can also be specified using both inf as well as the unicode symbol . Since it is internally represented using math.inf it will behave like a float.

  • ]-inf .. inf[
  • ]-∞ .. ∞[

Exclude borders

The start and end values can be excluded from the Range independent of one another using the open bracket notation.

  • ]0 .. 5.5]
  • ]0 .. inf[

Operations

Contains

You can easily check if a value is within a given Range like this:

> from py_range_parse import parse_range
> range = parse_range("[0 .. 5]")
> print(4 in range)
True

Comparison

You can compare equality of two Range instances using the == operator. For two ranges to be equal they have to have the same

  • start value
  • end value
  • start inclusion
  • end inclusion
  • type (int or float)
> from py_range_parse import parse_range
> range1 = parse_range("[0 .. 5]")
> range2 = parse_range("[0 .. 5]")
> range3 = parse_range("[0 .. 5.0]")
> print(range1 == range2)
True
> print(range1 == range3)
False

Contributing

GitHub is for social coding: if you want to write code, I encourage contributions through pull requests from forks of this repository. Create GitHub tickets for bugs and new features and comment on the ones that you are interested in.

License

py-range-parse
Copyright (c) 2019 Markus Ressel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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

py_range_parse-1.0.5.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

py_range_parse-1.0.5-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file py_range_parse-1.0.5.tar.gz.

File metadata

  • Download URL: py_range_parse-1.0.5.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.0 CPython/3.6.7

File hashes

Hashes for py_range_parse-1.0.5.tar.gz
Algorithm Hash digest
SHA256 15cf56ab4483814162f57f3b2bfd3ae68f6c4ea4cd7e710c881a5ce97f516c2c
MD5 9d33a463f433c6c40641ff58d2f683b9
BLAKE2b-256 2860f3ac14cf6c011b2fb331fd80163a6de900844737fb0a37876952a9635867

See more details on using hashes here.

File details

Details for the file py_range_parse-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: py_range_parse-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.0 CPython/3.6.7

File hashes

Hashes for py_range_parse-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a9d6b66f8e10dd26f5ff9726daef5b70bb32368a00a4563bd65d062c73d7c979
MD5 15c76b550738323b32836e8492bd8921
BLAKE2b-256 cd1929064acd044d0694cff7cb0b73450f43595f642b41389f2248aa26d48b10

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page