Skip to main content

Range and RangeDict classes.

Project description

RangeDictionary

RangeDictionary package provides Range and RangeDict classes.

from range_dictionary import Range, RangeDict

Range

Range objects represent an interval. They can be open, semi-open or closed.

r1 = Range(1, 2) # open
r2 = Range[1, 2] # closed
r3 = Range(1, 2, closed_left=True) # semi-open
r4 = Range(1, 2, closed_right=True) # semi-open

2 in r1 # False
2 in r2 # True

RangeDict

RangeDict objects are dictionaries whose keys are Range objects. They can be initialized using any iterable of 2 int/float values (the corresponding Ranges are open by default).

rd = RangeDict({
    (1, 2) : "first",
    Range[2, 5] : "second",
    Range(5, 7, closed_right=True) : "third",
    Range(7, float("inf")) : "fourth"
})

Accessing an int/float from a RangeDict returns its corresponding value.

3.5 in rd # True
rd[3.5] # "second"

RangeDicts behave similarly to standard dictionaries.

rd[Range(7, 9)] = "fifth"
rd.insert(Range[9, 11], "sixth")

del rd[Range[2, 5]]
rd.remove(Range(1, 2))

rd.get(8) # "fifth"
rd.get(100) # None

rd2 = RangeDict()
rd == rd2 # False
rd.clear()
rd == rd2 # True

# return generators, order not guaranteed
rd.keys()
rd.values()
rd.items()

# return sorted lists
rd.keys_sorted()
rd.values_sorted()
rd.items_sorted()

# merge RangeDicts
rd3 = rd | rd2
rd.update(rd2)

Acknowledgements

Inspired by rangedict by WKPlus.

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

RangeDictionary-1.0.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

RangeDictionary-1.0.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file RangeDictionary-1.0.2.tar.gz.

File metadata

  • Download URL: RangeDictionary-1.0.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for RangeDictionary-1.0.2.tar.gz
Algorithm Hash digest
SHA256 b4642679bf8a3b161026ef08a1e498d7fd76cb69e9f933e1699d839963af0f54
MD5 6800209cf943c6817187bd3f91894499
BLAKE2b-256 8cb68348d70c273ba1dbb4844ede3991525f240b617bd50c6cf77fd76d5488bb

See more details on using hashes here.

File details

Details for the file RangeDictionary-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for RangeDictionary-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 802ac607bbd4e6009de54b40bc34a744d1aa6e7ccbe81e60de16a7a2f19e007b
MD5 40d4587a9d00638b7b01027207652b16
BLAKE2b-256 f110570be5651d7d371f64c776a63b1add0d8d336c0a265f6c3c8bbe3ac2b066

See more details on using hashes here.

Supported by

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