Skip to content

Commit e414cdf

Browse files
committed
Re-paste RangeMap with newlines restored (unsure what made them disappear).
1 parent b37f867 commit e414cdf

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

distutils/_collections.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,55 +66,78 @@ class RangeMap(dict):
6666
key_match_comparator, which defaults to less-than-or-equal.
6767
A value is returned for a key if it is the first key that matches in
6868
the sorted list of keys.
69+
6970
One may supply keyword parameters to be passed to the sort function used
7071
to sort keys (i.e. key, reverse) as sort_params.
72+
7173
Let's create a map that maps 1-3 -> 'a', 4-6 -> 'b'
74+
7275
>>> r = RangeMap({3: 'a', 6: 'b'}) # boy, that was easy
7376
>>> r[1], r[2], r[3], r[4], r[5], r[6]
7477
('a', 'a', 'a', 'b', 'b', 'b')
78+
7579
Even float values should work so long as the comparison operator
7680
supports it.
81+
7782
>>> r[4.5]
7883
'b'
84+
7985
But you'll notice that the way rangemap is defined, it must be open-ended
8086
on one side.
87+
8188
>>> r[0]
8289
'a'
8390
>>> r[-1]
8491
'a'
92+
8593
One can close the open-end of the RangeMap by using undefined_value
94+
8695
>>> r = RangeMap({0: RangeMap.undefined_value, 3: 'a', 6: 'b'})
8796
>>> r[0]
8897
Traceback (most recent call last):
8998
...
9099
KeyError: 0
100+
91101
One can get the first or last elements in the range by using RangeMap.Item
102+
92103
>>> last_item = RangeMap.Item(-1)
93104
>>> r[last_item]
94105
'b'
106+
95107
.last_item is a shortcut for Item(-1)
108+
96109
>>> r[RangeMap.last_item]
97110
'b'
111+
98112
Sometimes it's useful to find the bounds for a RangeMap
113+
99114
>>> r.bounds()
100115
(0, 6)
116+
101117
RangeMap supports .get(key, default)
118+
102119
>>> r.get(0, 'not found')
103120
'not found'
121+
104122
>>> r.get(7, 'not found')
105123
'not found'
124+
106125
One often wishes to define the ranges by their left-most values,
107126
which requires use of sort params and a key_match_comparator.
127+
108128
>>> r = RangeMap({1: 'a', 4: 'b'},
109129
... sort_params=dict(reverse=True),
110130
... key_match_comparator=operator.ge)
111131
>>> r[1], r[2], r[3], r[4], r[5], r[6]
112132
('a', 'a', 'a', 'b', 'b', 'b')
133+
113134
That wasn't nearly as easy as before, so an alternate constructor
114135
is provided:
136+
115137
>>> r = RangeMap.left({1: 'a', 4: 'b', 7: RangeMap.undefined_value})
116138
>>> r[1], r[2], r[3], r[4], r[5], r[6]
117139
('a', 'a', 'a', 'b', 'b', 'b')
140+
118141
"""
119142

120143
def __init__(self, source, sort_params={}, key_match_comparator=operator.le):

0 commit comments

Comments
 (0)