jaraco-collections

5.2.1 · active · verified Sun Apr 12

jaraco.collections provides a suite of collection objects and utilities that complement Python's standard library `collections` module. It offers specialized data structures like `RangeMap`, `FrozenDict`, and `FoldedCaseKeyedDict`. The library is actively maintained, with its current version being 5.2.1, and releases occurring regularly.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the use of `RangeMap` to map numerical ranges to specific values, and `FrozenDict` for an immutable dictionary. `RangeMap` is particularly useful for tiered pricing, scoring, or categorization based on continuous ranges.

from jaraco.collections import RangeMap

# Create a RangeMap to associate values with key ranges
price_tiers = RangeMap({(0, 50): 'economy', (51, 100): 'standard', (101, float('inf')): 'premium'})

# Retrieve values based on a key within a range
print(f"Price 30 is in the {price_tiers[30]} tier")
print(f"Price 75 is in the {price_tiers[75]} tier")
print(f"Price 120 is in the {price_tiers[120]} tier")

# Example of FrozenDict (immutable dictionary)
from jaraco.collections import FrozenDict
f_dict = FrozenDict({'a': 1, 'b': 2})
print(f"Frozen dict: {f_dict}")
# f_dict['c'] = 3 # This would raise a TypeError

view raw JSON →