a _bs@sddlmZddlZddlmZddlZddlmZmZddl m Z ej rXddl m Z n ddl m Z zddl mZWneyddlmZYn0eZejdd krd nd Zd d ZGddde ZddZdS))absolute_importN)chain)map zip_longest)itemlist)MutableMapping) OrderedDictitems iteritemscCst||ott||SN)hasattrcallablegetattr)objattrrH/tmp/pip-target-4jja1joz/lib/python/orderedmultidict/orderedmultidict.py callable_attr%src@s eZdZdZddZddZddZdd Zd d Ze dud dZ ddZ ddZ ddZ ddZddZddZdvddZgfddZdwdd Zd gfd!d"Zdxd#d$Zgfd%d&Zdyd'd(Zd)d*Zd+d,Zefd-d.Zefd/d0Zeed1fd2d3Zdzd5d6Zd{d7d8Zefd9d:Z d;d<Z!efd=d>Z"d?d@Z#dAdBZ$efdCdDZ%dEdFZ&efdGdHZ'efdIdJZ(dKdLZ)efdMdNZ*efdOdPZ+dQdRZ,efdSdTZ-dUdVZ.dWdXZ/dYdZZ0d[d\Z1d]d^Z2d_d`Z3dadbZ4dcddZ5dedfZ6dgdhZ7didjZ8dkdlZ9dmdnZ:dodpZ;dqdrZ parameter has been added to these methods: items(), values(), iteritems(), itervalues() New methods have also been added to omdict. Methods with 'list' in their name interact with lists of values, and methods with 'all' in their name interact with all items in the dictionary, including multiple items with the same key. The new omdict methods are: load(), size(), reverse(), getlist(), add(), addlist(), set(), setlist(), setdefaultlist(), poplist(), popvalue(), popvalues(), popitem(), poplistitem(), allitems(), allkeys(), allvalues(), lists(), listitems(), iterallitems(), iterallkeys(), iterallvalues(), iterlists(), iterlistitems() Explanations and examples of the new methods above can be found in the function comments below and online at https://github.com/gruns/orderedmultidict Additional omdict information and documentation can also be found at the above url. cOs$t|_t|_|j|i|dSr )r_itemsodict_maploadselfargskwargsrrr__init__szomdict.__init__cOs||j|i||S)a Clear all existing key:value items and import all key:value items from . If multiple values exist for the same key in , they are all be imported. Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.load([(4,4), (4,44), (5,5)]) omd.allitems() == [(4,4), (4,44), (5,5)] Returns: . )clear updateallrrrrrs z omdict.loadcCs||Sr ) __class__allitemsrrrrcopysz omdict.copycCs|j|jdSr )rr rr$rrrr s z omdict.clearcCs t|jS)z Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.size() == 5 Returns: Total number of items, including multiple items with the same key. )lenrr$rrrsizes z omdict.sizeNcs|fdd|DS)Ncsg|] }|fqSrr).0keyvaluerr z#omdict.fromkeys..r)clsiterabler+rr*rfromkeysszomdict.fromkeyscCs||vSr rrr)rrrhas_keyszomdict.has_keycOs|jdg|Ri|dS)NT_update_updateallrrrrupdatesz omdict.updatecOs|jdg|Ri||S)a Update this dictionary with the items from , replacing existing key:value items with shared keys before adding new key:value items. Example: omd = omdict([(1,1), (2,2)]) omd.updateall([(2,'two'), (1,'one'), (2,222), (1,111)]) omd.allitems() == [(1, 'one'), (2, 'two'), (2, 222), (1, 111)] Returns: . Fr3rrrrr!s zomdict.updateallc Osrtg}}t||gD]}||||||qt|D]\}}|||q>|D]\}} ||| qXdSr )dictr_bin_update_items_items_iteratorsixr setlistadd) rreplace_at_most_onerr replacements leftoversmappingr)valuesr+rrrr4s   zomdict._update_updateallcCs|D]x\}}||vr(||vr(|g||<q||vr^|s^t||t||kr^|||q|rn|g||<q|||fqdS)ze are modified directly, ala pass by reference. N)r&r@append)rr r<r=r>r)r+rrrr7s    zomdict._bin_update_itemscCsf|}t|}t|dr |}nBt|dr8t|}n*t|drL|}nt|drbt|}|S)N iterallitemsr#r r )iterrrBr#r r )r containercontiteratorrrrr8s       zomdict._items_iteratorcCs||vr|j|djS|SNr)rr+rr)defaultrrrgetsz omdict.getcCs ||vrdd|j|DS|S)z Returns: The list of values for if is in the dictionary, else . If is not provided, an empty list is returned. cSsg|] }|jqSrr*r(noderrrr,r-z"omdict.getlist..rrHrrrgetlistszomdict.getlistcCs ||vr||S||||Sr r;rHrrr setdefault s zomdict.setdefaultcCs"||vr||S||||S)a Similar to setdefault() except is a list of values to set for . If already exists, its existing list of values is returned. If isn't a key and is an empty list, [], no values are added for and will not be added as a key. Returns: List of 's values if exists in the dictionary, otherwise . )rNaddlist)rr)Z defaultlistrrrsetdefaultlist&s   zomdict.setdefaultlistcCs0|j|g|j||}|j|||S)a Add to the list of values for . If is not in the dictionary, then is added as the sole value for . Example: omd = omdict() omd.add(1, 1) # omd.allitems() == [(1,1)] omd.add(1, 11) # omd.allitems() == [(1,1), (1,11)] omd.add(2, 2) # omd.allitems() == [(1,1), (1,11), (2,2)] Returns: . )rrPrrA)rr)r+rLrrrr;7s z omdict.addcCs|D]}|||q|S)a Add the values in to the list of values for . If is not in the dictionary, the values in become the values for . Example: omd = omdict([(1,1)]) omd.addlist(1, [11, 111]) omd.allitems() == [(1, 1), (1, 11), (1, 111)] omd.addlist(2, [2]) omd.allitems() == [(1, 1), (1, 11), (1, 111), (2, 2)] Returns: . rO)rr)Z valuelistr+rrrrQIszomdict.addlistcCs |||<|S)zr Sets 's value to . Identical in function to __setitem__(). Returns: . rrr)r+rrrset\sz omdict.setcCs|s||vr||n|tt|j|g|td}|D]Z\}}|turX|turX||_q8|turn|||q8|tur8|j|||j |q8|S)a Sets 's list of values to . Existing items with key are first replaced with new values from . Any remaining old items that haven't been replaced with new values are deleted, and any new values from that don't have corresponding items with to replace are appended to the end of the list of all items. If values is an empty list, [], is deleted, equivalent in action to del self[]. Example: omd = omdict([(1,1), (2,2)]) omd.setlist(1, [11, 111]) omd.allitems() == [(1,11), (2,2), (1,111)] omd = omdict([(1,1), (1,11), (2,2), (1,111)]) omd.setlist(1, [None]) omd.allitems() == [(1,None), (2,2)] omd = omdict([(1,1), (1,11), (2,2), (1,111)]) omd.setlist(1, []) omd.allitems() == [(2,2)] Returns: .  fillvalue) poprlistrrJ_absentr+r;remover removenode)rr)r@itrLr+rrrr:es   zomdict.setlistcs$||fdd||D|S)aH Removes all from the values of . If has no remaining values after removevalues(), the key is popped. Example: omd = omdict([(1, 1), (1, 11), (1, 1), (1, 111)]) omd.removevalues(1, [1, 111]) omd.allitems() == [(1, 11)] Returns: . csg|]}|vr|qSrr)r(vr@rrr,r-z'omdict.removevalues..)r:rN)rr)r@rr^r removevaluess zomdict.removevaluescCs8||vr||dS||jvr,|tur,|St|dSrG)poplistrrYKeyErrorrHrrrrWs z omdict.popcCsf||vrD||}|j|=|jD]\}}}||kr |j|q |S||jvrZ|turZ|St|dS)ad If is in the dictionary, pop it and return its list of values. If is not in the dictionary, return . KeyError is raised if is not provided and is not in the dictionary. Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.poplist(1) == [1, 11, 111] omd.allitems() == [(2,2), (3,3)] omd.poplist(2) == [2] omd.allitems() == [(3,3)] Raises: KeyError if isn't in the dictionary and isn't provided. Returns: List of 's values. N)rNrrr[rYra)rr)rIr@rLZnodekeyZ nodevaluerrrr`s zomdict.poplistTcsfdd}|vr|tur|r<|ddd|}n||}|dkr^t|q|rxt|d|n|}|||jSq|||rdndjSn|jvr|tur|St|dS)a If is provided, pops the first or last (key,value) item in the dictionary if is in the dictionary. If is not provided, pops the first or last value for if is in the dictionary. If no longer has any values after a popvalue() call, is removed from the dictionary. If isn't in the dictionary and was provided, return default. KeyError is raised if is not provided and is not in the dictionary. ValueError is raised if is provided but isn't a value for . Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3), (2,22)]) omd.popvalue(1) == 111 omd.allitems() == [(1,11), (1,111), (2,2), (3,3), (2,22)] omd.popvalue(1, last=False) == 1 omd.allitems() == [(1,11), (2,2), (3,3), (2,22)] omd.popvalue(2, 2) == 2 omd.allitems() == [(1,11), (3,3), (2,22)] omd.popvalue(1, 11) == 11 omd.allitems() == [(3,3), (2,22)] omd.popvalue('not a key', default='sup') == 'sup' Params: last: Boolean whether to return 's first value ( is False) or last value ( is True). Raises: KeyError if isn't in the dictionary and isn't provided. ValueError if isn't a value for . Returns: The first or last of 's values. cs2j||}j|s"j|=j||Sr )rrWrr[)r)indexrLr$rrpop_node_with_indexs   z,omdict.popvalue..pop_node_with_indexNrr)rYr@rb ValueErrorr&r+rra)rr)r+rIlastrcposrbrr$rpopvalues#  zomdict.popvalueFcCsp|jstd|jj|rD|j|r&dnd}|j}||j||dfSt|j|rXdnd}|| |fSdS)a Pop and return a key:value item. If is False, items()[0] is popped if is False or items()[-1] is popped if is True. All remaining items with the same key are removed. If is True, allitems()[0] is popped if is False or allitems()[-1] is popped if is True. Any remaining items with the same key remain. Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.popitem() == (3,3) omd.popitem(fromall=False, last=False) == (1,1) omd.popitem(fromall=False, last=False) == (2,2) omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.popitem(fromall=True, last=False) == (1,1) omd.popitem(fromall=True, last=False) == (1,11) omd.popitem(fromall=True, last=True) == (3,3) omd.popitem(fromall=True, last=False) == (1,111) Params: fromall: Whether to pop an item from items() ( is True) or allitems() ( is False). last: Boolean whether to pop the first item or last item of items() or allitems(). Raises: KeyError if the dictionary is empty. Returns: The first or last item from item() or allitem(). zpopitem(): %s is emptyrdr)rfN) rrar"__name__r)rhrXrkeysrW)rZfromallrfrLr)rrrpopitems zomdict.popitemcCs<|jsd|jj}t|||r(dnd}|||fS)a= Pop and return a key:valuelist item comprised of a key and that key's list of values. If is False, a key:valuelist item comprised of keys()[0] and its list of values is popped and returned. If is True, a key:valuelist item comprised of keys()[-1] and its list of values is popped and returned. Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.poplistitem(last=True) == (3,[3]) omd.poplistitem(last=False) == (1,[1,11,111]) Params: last: Boolean whether to pop the first or last key and its associated list of values. Raises: KeyError if the dictionary is empty. Returns: A two-tuple comprised of the first or last key and its associated list of values. zpoplistitem(): %s is emptyrdr)rr"rirarjr`)rrfsr)rrr poplistitem&s  zomdict.poplistitemcCst||S)z Raises: KeyError if is provided and not in the dictionary. Returns: List created from iteritems(). Only items with key are returned if is provided and is a dictionary key. )rXr r1rrrr Asz omdict.itemscCs t|Sr )rXiterkeysr$rrrrjIsz omdict.keyscCs(|tur||jvr||St|S)z Raises: KeyError if is provided and not in the dictionary. Returns: List created from itervalues().If is provided and is a dictionary key, only values of items with key are returned. )rYrrNrX itervaluesr1rrrr@Ls z omdict.valuescCs t|S)z9 Returns: List created from iterlists(). )rX iterlistsr$rrrlistsWsz omdict.listscCs t|S)z= Returns: List created from iterlistitems(). )rX iterlistitemsr$rrr listitems]szomdict.listitemscCsR|tur4||vr,dd|j|D}t|St|t|j}tdd|DS)a Parity with dict.iteritems() except the optional parameter has been added. If is provided, only items with the provided key are iterated over. KeyError is raised if is provided and not in the dictionary. Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.iteritems(1) -> (1,1) -> (1,11) -> (1,111) omd.iteritems() -> (1,1) -> (2,2) -> (3,3) Raises: KeyError if is provided and not in the dictionary. Returns: An iterator over the items() of the dictionary, or only items with the key if is provided. cSsg|]}|j|jfqSr)r)r+rKrrrr,ur-z$omdict.iteritems..css |]\}}||djfVqdS)rNr*)r(r)nodesrrr yr-z#omdict.iteritems..)rYrrCrar9r )rr)r rrrr cs zomdict.iteritemscCs t|jSr )r9rnrr$rrrrn{szomdict.iterkeyscCsJ|tur0||vr(tdd|j|DSt|tddt|jDS)a Parity with dict.itervalues() except the optional parameter has been added. If is provided, only values from items with the provided key are iterated over. KeyError is raised if is provided and not in the dictionary. Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.itervalues(1) -> 1 -> 11 -> 111 omd.itervalues() -> 1 -> 11 -> 111 -> 2 -> 3 Raises: KeyError if is provided and isn't in the dictionary. Returns: An iterator over the values() of the dictionary, or only the values of key if is provided. cSsg|] }|jqSrr*rKrrrr,r-z%omdict.itervalues..cSsg|]}|djqS)rr*)r(rtrrrr,r-)rYrCrrar9ror1rrrro~s zomdict.itervaluescCst||S)z Raises: KeyError if is provided and not in the dictionary. Returns: List created from iterallitems(). )rXrBr1rrrr#szomdict.allitemscCs t|S)z Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.allkeys() == [1,1,1,2,3] Returns: List created from iterallkeys(). )rX iterallkeysr$rrrallkeysszomdict.allkeyscCst||S)a1 Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.allvalues() == [1,11,111,2,3] omd.allvalues(1) == [1,11,111] Raises: KeyError if is provided and not in the dictionary. Returns: List created from iterallvalues(). )rX iterallvaluesr1rrr allvaluess zomdict.allvaluescCs|tur||S|jS)a Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.iterallitems() == (1,1) -> (1,11) -> (1,111) -> (2,2) -> (3,3) omd.iterallitems(1) == (1,1) -> (1,11) -> (1,111) Raises: KeyError if is provided and not in the dictionary. Returns: An iterator over every item in the diciontary. If is provided, only items with the key are iterated over. )rYr rr1rrrrBs  zomdict.iterallitemscCs |jS)z Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.iterallkeys() == 1 -> 1 -> 1 -> 2 -> 3 Returns: An iterator over the keys of every item in the dictionary. )rrnr$rrrrvszomdict.iterallkeyscCs0|tur&||vrt||St||jS)z Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.iterallvalues() == 1 -> 11 -> 111 -> 2 -> 3 Returns: An iterator over the values of every item in the dictionary. )rYrCrNrarror1rrrrxs zomdict.iterallvaluescstfddS)z Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.iterlists() -> [1,11,111] -> [2] -> [3] Returns: An iterator over the list comprised of the lists of values for each key. cs |Sr rNr)r$rrr-z"omdict.iterlists..rr$rr$rrps zomdict.iterlistscstfddS)z Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.iterlistitems() -> (1,[1,11,111]) -> (2,[2]) -> (3,[3]) Returns: An iterator over the list of key:valuelist items. cs||fSr rzr{r$rrr|r-z&omdict.iterlistitems..r}r$rr$rrrszomdict.iterlistitemscCs.t|jD]}|j|q |j|S)a  Reverse the order of all items in the dictionary. Example: omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)]) omd.reverse() omd.allitems() == [(3,3), (2,2), (1,111), (1,11), (1,1)] Returns: . )r9rnrreverserr1rrrr~s  zomdict.reversecCst|drT||}}t||tdD]&\}}||ksJ|tusJ|tur*dSq*nVt|drht|tsldSt|t|krdSt|D]\}}| |t|krdSqdS)NrBrUF__len__T) rrBrrYr _items_attrr&r9r rJ)rotherZmyiterZ otheriteri1i2r)r+rrr__eq__s  z omdict.__eq__cCs || Sr )rrrrrr__ne__sz omdict.__ne__cCs t|jSr )r&rr$rrrrszomdict.__len__ccs|D] }|VqdSr )rnr1rrr__iter__s zomdict.__iter__cCs ||jvSr rMr1rrr __contains__szomdict.__contains__cCs||vr||St|dSr )rJrar1rrr __getitem__s zomdict.__getitem__cCs|||gdSr )r:rSrrr __setitem__!szomdict.__setitem__cCs ||Sr )rWr1rrr __delitem__$szomdict.__delitem__cCs t|jSr )boolrr$rrr __nonzero__'szomdict.__nonzero__cCsddtdd|S)Nz{%s}z, cSsd|d|dfS)Nz%r: %rrrr)prrrr|,r-z omdict.__str__..)joinrrBr$rrr__str__*szomdict.__str__cCsd|jj|fS)Nz%s(%s))r"rir#r$rrr__repr__.szomdict.__repr__cCs|tt|t|Sr )r"r _get_itemsrrrr__or__1sz omdict.__or__cCs$t|D]\}}|j||dq|S)Nr*)rr;)rrkr]rrr__ior__4szomdict.__ior__)N)N)N)N)N)FT)T)>ri __module__ __qualname____doc__rrr%r r' classmethodr0r2r5r!r4r7r8rJrNrPrRr;rQrTr:r_rYrWr`rhrkrmr rjr@rqrsr rnror#rwryrBrvrxrprrr~rrrrrrrrrrrrrrrrrrast2        )  ; +          rcs@d}fdd|D}|D]}t|StddS)z!Find item iterator for an object.)rBr#r r c3s|]}t|r|VqdSr )r)r(nr?rrru=r-z_get_items..z,Object {} has no compatible items interface.N)r TypeErrorformat)r?namesexistarrrr:sr) __future__rsys itertoolsrr9Z six.movesrrrPY2 collectionsrcollections.abcr r ImportErrorZ ordereddictobjectrY version_inforrrrrrrr s,     <^