]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/setuptools/_distutils/_collections.py
7 # from jaraco.collections 3.5.1
8 class DictStack(list, collections
.abc
.Mapping
):
10 A stack of dictionaries that behaves as a view on those dictionaries,
11 giving preference to the last.
13 >>> stack = DictStack([dict(a=1, c=2), dict(b=2, a=2)])
22 >>> stack.push(dict(a=3))
25 >>> set(stack.keys()) == set(['a', 'b', 'c'])
27 >>> set(stack.items()) == set([('a', 3), ('b', 2), ('c', 2)])
29 >>> dict(**stack) == dict(stack) == dict(a=3, c=2, b=2)
37 >>> stack.get('b', None)
43 dicts
= list.__iter
__(self
)
44 return iter(set(itertools
.chain
.from_iterable(c
.keys() for c
in dicts
)))
46 def __getitem__(self
, key
):
47 for scope
in reversed(tuple(list.__iter
__(self
))):
54 def __contains__(self
, other
):
55 return collections
.abc
.Mapping
.__contains
__(self
, other
)
58 return len(list(iter(self
)))
61 # from jaraco.collections 3.7
64 A dictionary-like object that uses the keys as bounds for a range.
65 Inclusion of the value for that range is determined by the
66 key_match_comparator, which defaults to less-than-or-equal.
67 A value is returned for a key if it is the first key that matches in
68 the sorted list of keys.
70 One may supply keyword parameters to be passed to the sort function used
71 to sort keys (i.e. key, reverse) as sort_params.
73 Let's create a map that maps 1-3 -> 'a', 4-6 -> 'b'
75 >>> r = RangeMap({3: 'a', 6: 'b'}) # boy, that was easy
76 >>> r[1], r[2], r[3], r[4], r[5], r[6]
77 ('a', 'a', 'a', 'b', 'b', 'b')
79 Even float values should work so long as the comparison operator
85 But you'll notice that the way rangemap is defined, it must be open-ended
93 One can close the open-end of the RangeMap by using undefined_value
95 >>> r = RangeMap({0: RangeMap.undefined_value, 3: 'a', 6: 'b'})
97 Traceback (most recent call last):
101 One can get the first or last elements in the range by using RangeMap.Item
103 >>> last_item = RangeMap.Item(-1)
107 .last_item is a shortcut for Item(-1)
109 >>> r[RangeMap.last_item]
112 Sometimes it's useful to find the bounds for a RangeMap
117 RangeMap supports .get(key, default)
119 >>> r.get(0, 'not found')
122 >>> r.get(7, 'not found')
125 One often wishes to define the ranges by their left-most values,
126 which requires use of sort params and a key_match_comparator.
128 >>> r = RangeMap({1: 'a', 4: 'b'},
129 ... sort_params=dict(reverse=True),
130 ... key_match_comparator=operator.ge)
131 >>> r[1], r[2], r[3], r[4], r[5], r[6]
132 ('a', 'a', 'a', 'b', 'b', 'b')
134 That wasn't nearly as easy as before, so an alternate constructor
137 >>> r = RangeMap.left({1: 'a', 4: 'b', 7: RangeMap.undefined_value})
138 >>> r[1], r[2], r[3], r[4], r[5], r[6]
139 ('a', 'a', 'a', 'b', 'b', 'b')
143 def __init__(self
, source
, sort_params
={}, key_match_comparator
=operator
.le
):
144 dict.__init
__(self
, source
)
145 self
.sort_params
= sort_params
146 self
.match
= key_match_comparator
149 def left(cls
, source
):
151 source
, sort_params
=dict(reverse
=True), key_match_comparator
=operator
.ge
154 def __getitem__(self
, item
):
155 sorted_keys
= sorted(self
.keys(), **self
.sort_params
)
156 if isinstance(item
, RangeMap
.Item
):
157 result
= self
.__getitem
__(sorted_keys
[item
])
159 key
= self
._find
_first
_match
_(sorted_keys
, item
)
160 result
= dict.__getitem
__(self
, key
)
161 if result
is RangeMap
.undefined_value
:
165 def get(self
, key
, default
=None):
167 Return the value for key if key is in the dictionary, else default.
168 If default is not given, it defaults to None, so that this method
169 never raises a KeyError.
176 def _find_first_match_(self
, keys
, item
):
177 is_match
= functools
.partial(self
.match
, item
)
178 matches
= list(filter(is_match
, keys
))
184 sorted_keys
= sorted(self
.keys(), **self
.sort_params
)
185 return (sorted_keys
[RangeMap
.first_item
], sorted_keys
[RangeMap
.last_item
])
187 # some special values for the RangeMap
188 undefined_value
= type('RangeValueUndefined', (), {})()