B +Ã[39ã@sŒddlmZmZddlmZddlZddlmZddlm Z Gdd„de ƒZ e  e ¡e  e ¡d d „Z e idƒZidfd d „Zd d„ZdS)é)ÚMappingÚHashableé)ÚchainN)Úpvector)Ú transformcs@eZdZdZdZ‡fdd„Zedd„ƒZedd„ƒZd d „Z ed d „ƒZ d d„Z e j Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Ze jZd%d&„ZeZeZeZd'd(„Zd)d*„Zd+d,„Z d-d.„Z!d/d0„Z"d1d2„Z#d3d4„Z$d5d6„Z%d7d8„Z&d9d:„Z'd;d<„Z(Gd=d>„d>e)ƒZ*d?d@„Z+‡Z,S)AÚPMapaâ Persistent map/dict. Tries to follow the same naming conventions as the built in dict where feasible. Do not instantiate directly, instead use the factory functions :py:func:`m` or :py:func:`pmap` to create an instance. Was originally written as a very close copy of the Clojure equivalent but was later rewritten to closer re-assemble the python dict. This means that a sparse vector (a PVector) of buckets is used. The keys are hashed and the elements inserted at position hash % len(bucket_vector). Whenever the map size exceeds 2/3 of the containing vectors size the map is reallocated to a vector of double the size. This is done to avoid excessive hash collisions. This structure corresponds most closely to the built in dict type and is intended as a replacement. Where the semantics are the same (more or less) the same function names have been used but for some cases it is not possible, for example assignments and deletion of values. PMap implements the Mapping protocol and is Hashable. It also supports dot-notation for element access. Random access and insert is log32(n) where n is the size of the map. The following are examples of some common operations on persistent maps >>> m1 = m(a=1, b=3) >>> m2 = m1.set('c', 3) >>> m3 = m2.remove('a') >>> m1 pmap({'a': 1, 'b': 3}) >>> m2 pmap({'a': 1, 'c': 3, 'b': 3}) >>> m3 pmap({'c': 3, 'b': 3}) >>> m3['c'] 3 >>> m3.c 3 )Ú_sizeÚ_bucketsÚ __weakref__Ú _cached_hashcs tt|ƒ |¡}||_||_|S)N)ÚsuperrÚ__new__r r )ÚclsÚsizeÚbucketsÚself)Ú __class__©úC/opt/alt/python37/lib64/python3.7/site-packages/pyrsistent/_pmap.pyr0sz PMap.__new__cCs t|ƒt|ƒ}||}||fS)N)ÚhashÚlen)rÚkeyÚindexÚbucketrrrÚ _get_bucket6szPMap._get_bucketcCs>t ||¡\}}|r2x|D]\}}||kr|SqWt|ƒ‚dS)N)rrÚKeyError)rrÚ_rÚkÚvrrrÚ_getitem<s z PMap._getitemcCst |j|¡S)N)rr r )rrrrrÚ __getitem__FszPMap.__getitem__cCs:t ||¡\}}|r6x|D]\}}||krdSqWdSdS)NTF)rr)rrrrrrrrÚ _containsIszPMap._containscCs| |j|¡S)N)r"r )rrrrrÚ __contains__UszPMap.__contains__cCs| ¡S)N)Úiterkeys)rrrrÚ__iter__Zsz PMap.__iter__cCs8y||Stk r2td t|ƒj|¡ƒ‚YnXdS)Nz{0} has no attribute '{1}')rÚAttributeErrorÚformatÚtypeÚ__name__)rrrrrÚ __getattr__]s zPMap.__getattr__ccs x| ¡D]\}}|Vq WdS)N)Ú iteritems)rrrrrrr$esz PMap.iterkeysccs x| ¡D]\}}|Vq WdS)N)r+)rrrrrrÚ itervalueslszPMap.itervaluesccs4x.|jD]$}|rx|D]\}}||fVqWqWdS)N)r )rrrrrrrr+ps zPMap.iteritemscCs t| ¡ƒS)N)rr,)rrrrÚvaluesvsz PMap.valuescCs t| ¡ƒS)N)rr$)rrrrÚkeysysz PMap.keyscCs t| ¡ƒS)N)rr+)rrrrÚitems|sz PMap.itemscCs|jS)N)r )rrrrÚ__len__sz PMap.__len__cCsd tt|ƒƒ¡S)Nz pmap({0}))r'ÚstrÚdict)rrrrÚ__repr__‚sz PMap.__repr__cCs¸||kr dSt|tƒstSt|ƒt|ƒkr.dSt|tƒr„t|dƒr\t|dƒr\|j|jkr\dS|j|jkrldSt|  ¡ƒt|  ¡ƒkSt|tƒržt|  ¡ƒ|kSt|  ¡ƒtt   |¡ƒkS)NTFr ) Ú isinstancerÚNotImplementedrrÚhasattrr r r2r+Úsix)rÚotherrrrÚ__eq__…s      z PMap.__eq__cCs tdƒ‚dS)NzPMaps are not orderable)Ú TypeError)rr8rrrÚ__lt__™sz PMap.__lt__cCs| ¡S)N)r3)rrrrÚ__str__ sz PMap.__str__cCs"t|dƒstt| ¡ƒƒ|_|jS)Nr )r6rÚ frozensetr+r )rrrrÚ__hash__£s z PMap.__hash__cCs| ¡ ||¡ ¡S)a. Return a new PMap with key and val inserted. >>> m1 = m(a=1, b=2) >>> m2 = m1.set('a', 3) >>> m3 = m1.set('c' ,4) >>> m1 pmap({'a': 1, 'b': 2}) >>> m2 pmap({'a': 3, 'b': 2}) >>> m3 pmap({'a': 1, 'c': 4, 'b': 2}) )ÚevolverÚsetÚ persistent)rrÚvalrrrr@¨szPMap.setcCs| ¡ |¡ ¡S)zÐ Return a new PMap without the element specified by key. Raises KeyError if the element is not present. >>> m1 = m(a=1, b=2) >>> m1.remove('a') pmap({'b': 2}) )r?ÚremoverA)rrrrrrC¸s z PMap.removecCs$y | |¡Stk r|SXdS)a Return a new PMap without the element specified by key. Returns reference to itself if element is not present. >>> m1 = m(a=1, b=2) >>> m1.discard('a') pmap({'b': 2}) >>> m1 is m1.discard('c') True N)rCr)rrrrrÚdiscardÃs  z PMap.discardcGs|jdd„f|žŽS)a* Return a new PMap with the items in Mappings inserted. If the same key is present in multiple maps the rightmost (last) value is inserted. >>> m1 = m(a=1, b=2) >>> m1.update(m(a=2, c=3), {'a': 17, 'd': 35}) pmap({'a': 17, 'c': 3, 'b': 2, 'd': 35}) cSs|S)Nr)ÚlÚrrrrÚÜózPMap.update..)Ú update_with)rÚmapsrrrÚupdateÓs z PMap.updatecGsV| ¡}xD|D]<}x6| ¡D]*\}}| |||kr@||||ƒn|¡qWqW| ¡S)a# Return a new PMap with the items in Mappings maps inserted. If the same key is present in multiple maps the values will be merged using merge_fn going from left to right. >>> from operator import add >>> m1 = m(a=1, b=2) >>> m1.update_with(add, m(a=2)) pmap({'a': 3, 'b': 2}) The reverse behaviour of the regular merge. Keep the leftmost element instead of the rightmost. >>> m1 = m(a=1) >>> m1.update_with(lambda l, r: l, m(a=2), {'a':3}) pmap({'a': 1}) )r?r/r@rA)rZ update_fnrJr?ÚmaprÚvaluerrrrIÞs  *zPMap.update_withcCs | |¡S)N)rK)rr8rrrÚ__add__õsz PMap.__add__cCstt|ƒffS)N)Úpmapr2)rrrrÚ __reduce__øszPMap.__reduce__cGs t||ƒS)aÿ Transform arbitrarily complex combinations of PVectors and PMaps. A transformation consists of two parts. One match expression that specifies which elements to transform and one transformation function that performs the actual transformation. >>> from pyrsistent import freeze, ny >>> news_paper = freeze({'articles': [{'author': 'Sara', 'content': 'A short article'}, ... {'author': 'Steve', 'content': 'A slightly longer article'}], ... 'weather': {'temperature': '11C', 'wind': '5m/s'}}) >>> short_news = news_paper.transform(['articles', ny, 'content'], lambda c: c[:25] + '...' if len(c) > 25 else c) >>> very_short_news = news_paper.transform(['articles', ny, 'content'], lambda c: c[:15] + '...' if len(c) > 15 else c) >>> very_short_news.articles[0].content 'A short article' >>> very_short_news.articles[1].content 'A slightly long...' When nothing has been transformed the original data structure is kept >>> short_news is news_paper True >>> very_short_news is news_paper False >>> very_short_news.articles[0] is news_paper.articles[0] True )r)rZtransformationsrrrrüszPMap.transformcCs|S)Nr)rrrrÚcopysz PMap.copyc@sheZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„ZdS)z PMap._Evolver)Ú_buckets_evolverr Ú_original_pmapcCs||_|j ¡|_|j|_dS)N)rSr r?rRr )rZ original_pmaprrrÚ__init__s zPMap._Evolver.__init__cCst |j|¡S)N)rr rR)rrrrrr!#szPMap._Evolver.__getitem__cCs| ||¡dS)N)r@)rrrBrrrÚ __setitem__&szPMap._Evolver.__setitem__csÒt|jƒd|jkr(| dt|jƒ¡|ˆf}t |j|¡\}}|r´xB|D]:\‰}ˆ|krL|ˆk r‚‡‡fdd„|Dƒ}||j|<|SqLW|g}| |¡||j|<|jd7_n|g|j|<|jd7_|S)Ngq= ×£på?écs(g|] \}}|ˆkr||fn|ˆf‘qSrr)Ú.0Zk2Zv2)rrBrrú 3sz%PMap._Evolver.set..r)rrRr Ú _reallocaterrÚextend)rrrBZkvrrrÚ new_bucketr)rrBrr@)s$    zPMap._Evolver.setcCsˆ|dg}|j ¡}xVt dd„|Dƒ¡D]>\}}t|ƒ|}||rZ|| ||f¡q*||fg||<q*Wtƒ ¡|_|j |¡dS)Ncss|]}|r|VqdS)Nr)rWÚxrrrú Esz,PMap._Evolver._reallocate..) rRrArÚ from_iterablerÚappendrr?rZ)rZnew_sizeZnew_listrrrrrrrrYBs    zPMap._Evolver._reallocatecCs |j ¡S)N)rRÚis_dirty)rrrrr`QszPMap._Evolver.is_dirtycCs"| ¡rt|j|j ¡ƒ|_|jS)N)r`rr rRrArS)rrrrrATszPMap._Evolver.persistentcCs|jS)N)r )rrrrr0ZszPMap._Evolver.__len__cCst |j|¡S)N)rr"rR)rrrrrr#]szPMap._Evolver.__contains__cCs| |¡dS)N)rC)rrrrrÚ __delitem__`szPMap._Evolver.__delitem__csnt |jˆ¡\}}|r\‡fdd„|Dƒ}t|ƒt|ƒkr\|r@|nd|j|<|jd8_|Std ˆ¡ƒ‚dS)Ncs g|]\}}|ˆkr||f‘qSrr)rWrr)rrrrXgsz(PMap._Evolver.remove..rz{0})rrrRrr rr')rrrrr[r)rrrCcszPMap._Evolver.removeN)r)Ú __module__Ú __qualname__Ú __slots__rTr!rUr@rYr`rAr0r#rarCrrrrÚ_EvolversrecCs | |¡S)a) Create a new evolver for this pmap. For a discussion on evolvers in general see the documentation for the pvector evolver. Create the evolver and perform various mutating updates to it: >>> m1 = m(a=1, b=2) >>> e = m1.evolver() >>> e['c'] = 3 >>> len(e) 3 >>> del e['a'] The underlying pmap remains the same: >>> m1 pmap({'a': 1, 'b': 2}) The changes are kept in the evolver. An updated pmap can be created using the persistent() function on the evolver. >>> m2 = e.persistent() >>> m2 pmap({'c': 3, 'b': 2}) The new pmap will share data with the original pmap in the same way that would have been done if only using operations on the pmap. )re)rrrrr?osz PMap.evolver)-r)rbrcÚ__doc__rdrÚ staticmethodrr r!r"r#rÚgetr%r*r$r,r+r-r.r/r0r3r9Ú__ne__r;Ú__le__Ú__gt__Ú__ge__r<r>r@rCrDrKrIrNrPrrQÚobjectrer?Ú __classcell__rr)rrrsL%      Trc Cs¼|r |}n.ydt|ƒpd}Wntk r6d}YnX|dg}t|tƒsTt|ƒ}xPt |¡D]B\}}t|ƒ}||}||}|r”| ||f¡q`||fg||<q`Wt t|ƒt ƒ  |¡ƒS)NrVé) rÚ Exceptionr4rr2r7r+rr_rrrZ) ÚinitialÚpre_sizerrrrÚhrrrrrÚ_turbo_mapping’s"   rtcCs|stSt||ƒS)aé Create new persistent map, inserts all elements in initial into the newly created map. The optional argument pre_size may be used to specify an initial size of the underlying bucket vector. This may have a positive performance impact in the cases where you know beforehand that a large number of elements will be inserted into the map eventually since it will reduce the number of reallocations required. >>> pmap({'a': 13, 'b': 14}) pmap({'a': 13, 'b': 14}) )Ú _EMPTY_PMAPrt)rqrrrrrrOµs rOcKst|ƒS)z– Creates a new persitent map. Inserts all key value arguments into the newly created map. >>> m(a=13, b=14) pmap({'a': 13, 'b': 14}) )rO)ÚkwargsrrrÚmÅsrw)Z_compatrrÚ itertoolsrr7Zpyrsistent._pvectorrZpyrsistent._transformationsrrmrÚregisterrtrurOrwrrrrÚs