a `Å]_-9ã@s„ddlmZmZddlmZddlmZddlmZGdd„de ƒZ e  e ¡e  e ¡dd„Z e idƒZ idfd d „Zd d „Zd S)é)ÚMappingÚHashable)Úchain)Ú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({'b': 3, 'a': 1}) >>> m2 pmap({'c': 3, 'b': 3, 'a': 1}) >>> 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__©ú6/usr/lib64/python3.9/site-packages/pyrsistent/_pmap.pyr/sz PMap.__new__cCs t|ƒt|ƒ}||}||fSr )ÚhashÚlen)rÚkeyÚindexÚbucketrrrÚ _get_bucket5szPMap._get_bucketcCs>t ||¡\}}|r2|D]\}}||kr|Sqt|ƒ‚dSr )rrÚKeyError)rrÚ_rÚkÚvrrrÚ_getitem;s   z PMap._getitemcCst |j|¡Sr )rr"r ©rrrrrÚ __getitem__EszPMap.__getitem__cCs8t ||¡\}}|r4|D]\}}||krdSqdSdS)NTF)rr)rrrrr rrrÚ _containsHs zPMap._containscCs| |j|¡Sr )r%r r#rrrÚ __contains__TszPMap.__contains__cCs| ¡Sr )Úiterkeys©rrrrÚ__iter__Ysz PMap.__iter__c CsLz ||WStyF}z$td t|ƒj|¡ƒ|‚WYd}~n d}~00dS)Nz{0} has no attribute '{1}')rÚAttributeErrorÚformatÚtypeÚ__name__)rrÚerrrÚ __getattr__\s ÿþzPMap.__getattr__ccs| ¡D]\}}|VqdSr ©Ú iteritems)rr rrrrr'dsz PMap.iterkeysccs| ¡D]\}}|VqdSr r0)rrr!rrrÚ itervalueskszPMap.itervaluesccs,|jD] }|r|D]\}}||fVqqdSr )r )rrr r!rrrr1os  zPMap.iteritemscCs t| ¡ƒSr )rr2r(rrrÚvaluesusz PMap.valuescCs t| ¡ƒSr )rr'r(rrrÚkeysxsz PMap.keyscCs t| ¡ƒSr )rr1r(rrrÚitems{sz PMap.itemscCs|jSr ©r r(rrrÚ__len__~sz PMap.__len__cCsd tt|ƒƒ¡S)Nz pmap({0}))r+ÚstrÚdictr(rrrÚ__repr__�sz PMap.__repr__cCs¶||ur 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|  ¡ƒt|  ¡ƒkS)NTFr ) Ú isinstancerÚNotImplementedrrÚhasattrr r r9r1r5©rÚotherrrrÚ__eq__„s"   ÿ  z PMap.__eq__cCs tdƒ‚dS)NzPMaps are not orderable)Ú TypeErrorr>rrrÚ__lt__˜sz PMap.__lt__cCs| ¡Sr )r:r(rrrÚ__str__Ÿsz PMap.__str__cCs"t|dƒstt| ¡ƒƒ|_|jS)Nr )r=rÚ frozensetr1r r(rrrÚ__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({'b': 2, 'a': 1}) >>> m2 pmap({'b': 2, 'a': 3}) >>> m3 pmap({'c': 4, 'b': 2, 'a': 1}) )ÚevolverÚsetÚ persistent©rrÚvalrrrrG§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}) )rFÚremoverHr#rrrrK·s z PMap.removecCs(z | |¡WSty"|YS0dS)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)rKrr#rrrÚdiscardÂs   z PMap.discardcGs|jdd„g|¢RŽ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({'c': 3, 'b': 2, 'a': 17, 'd': 35}) cSs|Sr r)ÚlÚrrrrÚÛózPMap.update..)Ú update_with)rÚmapsrrrÚupdateÒs z PMap.updatecGsN| ¡}|D]8}| ¡D]*\}}| |||vr<||||ƒn|¡qq | ¡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({'b': 2, 'a': 3}) 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}) )rFr5rGrH)rZ update_fnrRrFÚmaprÚvaluerrrrQÝs &zPMap.update_withcCs | |¡Sr )rSr>rrrÚ__add__ôsz PMap.__add__cCstt|ƒffSr )Úpmapr9r(rrrÚ __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|Sr rr(rrrÚ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|_dSr )r[r rFrZr )rZ original_pmaprrrÚ__init__s zPMap._Evolver.__init__cCst |j|¡Sr )rr"rZr#rrrr$"szPMap._Evolver.__getitem__cCs| ||¡dSr )rGrIrrrÚ __setitem__%szPMap._Evolver.__setitem__csÒt|jƒd|jkr(| dt|jƒ¡|ˆf}t |j|¡\}}|r´|D]>\‰}ˆ|krJ|ˆur€‡‡fdd„|Dƒ}||j|<|SqJ|g}| |¡||j|<|jd7_n|g|j|<|jd7_|S)Ngq= ×£på?écs(g|] \}}|ˆkr||fn|ˆf‘qSrr)Ú.0Zk2Zv2©r rJrrÚ 2rPz%PMap._Evolver.set..é)rrZr Ú _reallocaterrÚextend)rrrJZkvrrr!Ú new_bucketrr`rrG(s$      zPMap._Evolver.setcCs„|dg}|j ¡}t dd„|Dƒ¡D]>\}}t|ƒ|}||rX|| ||f¡q(||fg||<q(tƒ ¡|_|j |¡dS)Ncss|]}|r|VqdSr r)r_ÚxrrrÚ DrPz,PMap._Evolver._reallocate..) rZrHrÚ from_iterablerÚappendrrFrd)rZnew_sizeZnew_listrr r!rrrrrcAs    zPMap._Evolver._reallocatecCs |j ¡Sr )rZÚis_dirtyr(rrrrjPszPMap._Evolver.is_dirtycCs"| ¡rt|j|j ¡ƒ|_|jSr )rjrr rZrHr[r(rrrrHSszPMap._Evolver.persistentcCs|jSr r6r(rrrr7YszPMap._Evolver.__len__cCst |j|¡Sr )rr%rZr#rrrr&\szPMap._Evolver.__contains__cCs| |¡dSr )rKr#rrrÚ __delitem___szPMap._Evolver.__delitem__csnt |jˆ¡\}}|r\‡fdd„|Dƒ}t|ƒt|ƒkr\|r@|nd|j|<|jd8_|Std ˆ¡ƒ‚dS)Ncs g|]\}}|ˆkr||f‘qSrr)r_r r!©rrrrafrPz(PMap._Evolver.remove..rbz{0})rrrZrr rr+)rrrrrerrlrrKbszPMap._Evolver.removeN)r-Ú __module__Ú __qualname__Ú __slots__r\r$r]rGrcrjrHr7r&rkrKrrrrÚ_EvolversrpcCs | |¡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({'b': 2, 'a': 1}) 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. )rpr(rrrrFnsz PMap.evolver)-r-rmrnÚ__doc__rorÚ staticmethodrr"r$r%r&rÚgetr)r/r'r2r1r3r4r5r7r:r@Ú__ne__rBÚ__le__Ú__gt__Ú__ge__rCrErGrKrLrSrQrVrXrrYÚobjectrprFÚ __classcell__rrrrrsR%      Trc Cs´|r |}n,zdt|ƒpd}Wnty4d}Yn0|dg}t|tƒsRt|ƒ}| ¡D]B\}}t|ƒ}||}||}|rŽ| ||f¡qZ||fg||<qZtt|ƒt ƒ  |¡ƒS)Nr^é) rÚ Exceptionr;rr9r5rrirrrd) ÚinitialÚpre_sizerrr r!ÚhrrrrrÚ_turbo_mapping‘s"    rcCs|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({'b': 14, 'a': 13}) )Ú _EMPTY_PMAPr)r|r}rrrrW´s rWcKst|ƒS)z– Creates a new persitent map. Inserts all key value arguments into the newly created map. >>> m(a=13, b=14) pmap({'b': 14, 'a': 13}) )rW)ÚkwargsrrrÚmÄsr‚N)Úcollections.abcrrÚ itertoolsrZpyrsistent._pvectorrZpyrsistent._transformationsrrxrÚregisterrr€rWr‚rrrrÚs