win32timezone.RangeMap Object

A dictionary-like object that uses the keys as bounds for a range. Inclusion of the value for that range is determined by the keyMatchComparator, which defaults to greater-than-or-equal. A value is returned for a key if it is the first key that matches in the sorted list of keys. By default, keys are sorted in ascending order, but can be sorted in any other order using the keySortComparator.

Comments

Let's create a map that maps 1-3 -> 'a', 4-6 -> 'b'

>>> r = RangeMap({3: 'a', 6: 'b'})  # boy, that was easy

>>> r[1], r[2], r[3], r[4], r[5], r[6]

('a', 'a', 'a', 'b', 'b', 'b')

But you'll notice that the way rangemap is defined, it must be open-ended on one side.

>>> r[0]

'a'

>>> r[-1]

'a'

One can close the open-end of the RangeMap by using RangeValueUndefined

>>> r = RangeMap({0: RangeValueUndefined(), 3: 'a', 6: 'b'})

>>> r[0]

Traceback (most recent call last):

...

KeyError: 0

One can get the first or last elements in the range by using RangeItem

>>> last_item = RangeItem(-1)

>>> r[last_item]

'b'

>>> r[RangeItemLast()] 'b'

>>> r.bounds() (0, 6)