a „oe´ã@sJddlZddlZddlZddlZGdd„deejjƒZGdd„deƒZ dS)éNc@s6eZdZdZdd„Zdd„ZejZdd„Z dd „Z d S) Ú DictStacka² A stack of dictionaries that behaves as a view on those dictionaries, giving preference to the last. >>> stack = DictStack([dict(a=1, c=2), dict(b=2, a=2)]) >>> stack['a'] 2 >>> stack['b'] 2 >>> stack['c'] 2 >>> len(stack) 3 >>> stack.push(dict(a=3)) >>> stack['a'] 3 >>> set(stack.keys()) == set(['a', 'b', 'c']) True >>> set(stack.items()) == set([('a', 3), ('b', 2), ('c', 2)]) True >>> dict(**stack) == dict(stack) == dict(a=3, c=2, b=2) True >>> d = stack.pop() >>> stack['a'] 2 >>> d = stack.pop() >>> stack['a'] 1 >>> stack.get('b', None) >>> 'c' in stack True cCs(t |¡}tttj dd„|Dƒ¡ƒƒS)Ncss|]}| ¡VqdS©N)Úkeys)Ú.0Úc©rúCC:\Program Files\Certbot\pkgs\setuptools\_distutils\_collections.pyÚ ,óz%DictStack.__iter__..)ÚlistÚ__iter__ÚiterÚsetÚ itertoolsÚchainÚ from_iterable)ÚselfZdictsrrrr *s zDictStack.__iter__cCs8ttt |¡ƒƒD]}||vr||Sqt|ƒ‚dSr)ÚreversedÚtupler r ÚKeyError)rÚkeyZscoperrrÚ __getitem__.szDictStack.__getitem__cCstjj ||¡Sr)Ú collectionsÚabcÚMappingÚ __contains__)rÚotherrrrr6szDictStack.__contains__cCsttt|ƒƒƒSr)Úlenr r )rrrrÚ__len__9szDictStack.__len__N) Ú__name__Ú __module__Ú __qualname__Ú__doc__r rr ÚappendÚpushrrrrrrrs !rc@s|eZdZdZiejfdd„Zedd„ƒZdd„Z dd d „Z d d „Z d d„Z e ddiƒƒZGdd„deƒZedƒZedƒZdS)ÚRangeMapaa A dictionary-like object that uses the keys as bounds for a range. Inclusion of the value for that range is determined by the key_match_comparator, which defaults to less-than-or-equal. A value is returned for a key if it is the first key that matches in the sorted list of keys. One may supply keyword parameters to be passed to the sort function used to sort keys (i.e. key, reverse) as sort_params. 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') Even float values should work so long as the comparison operator supports it. >>> r[4.5] '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 undefined_value >>> r = RangeMap({0: RangeMap.undefined_value, 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 RangeMap.Item >>> last_item = RangeMap.Item(-1) >>> r[last_item] 'b' .last_item is a shortcut for Item(-1) >>> r[RangeMap.last_item] 'b' Sometimes it's useful to find the bounds for a RangeMap >>> r.bounds() (0, 6) RangeMap supports .get(key, default) >>> r.get(0, 'not found') 'not found' >>> r.get(7, 'not found') 'not found' One often wishes to define the ranges by their left-most values, which requires use of sort params and a key_match_comparator. >>> r = RangeMap({1: 'a', 4: 'b'}, ... sort_params=dict(reverse=True), ... key_match_comparator=operator.ge) >>> r[1], r[2], r[3], r[4], r[5], r[6] ('a', 'a', 'a', 'b', 'b', 'b') That wasn't nearly as easy as before, so an alternate constructor is provided: >>> r = RangeMap.left({1: 'a', 4: 'b', 7: RangeMap.undefined_value}) >>> r[1], r[2], r[3], r[4], r[5], r[6] ('a', 'a', 'a', 'b', 'b', 'b') cCst ||¡||_||_dSr)ÚdictÚ__init__Ú sort_paramsÚmatch)rÚsourcer(Úkey_match_comparatorrrrr'�s zRangeMap.__init__cCs||tdd�tjd�S)NT)Úreverse)r(r+)r&ÚoperatorÚge)Úclsr*rrrÚleft”sÿz RangeMap.leftcCs`t| ¡fi|j¤Ž}t|tjƒr2| ||¡}n*| ||¡}t ||¡}|tj ur\t |ƒ‚|Sr) Úsortedrr(Ú isinstancer%ÚItemrÚ_find_first_match_r&Úundefined_valuer)rÚitemÚ sorted_keysÚresultrrrrršs    zRangeMap.__getitem__NcCs&z ||WSty |YS0dS)zÀ Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError. N)r)rrÚdefaultrrrÚget¥s  z RangeMap.getcCs4t |j|¡}tt||ƒƒ}|r(|dSt|ƒ‚dS)Nr)Ú functoolsÚpartialr)r Úfilterr)rrr6Zis_matchÚmatchesrrrr4°s zRangeMap._find_first_match_cCs*t| ¡fi|j¤Ž}|tj|tjfSr)r1rr(r%Ú first_itemÚ last_item)rr7rrrÚbounds·szRangeMap.boundsZRangeValueUndefinedrc@seZdZdZdS)z RangeMap.Itemz RangeMap ItemN)rr r!r"rrrrr3¾sr3réÿÿÿÿ)N)rr r!r"r-Úler'Ú classmethodr0rr:r4rAÚtyper5Úintr3r?r@rrrrr%>sP  r%) rr;rr-r rrrr&r%rrrrÚs 6