n9eJ>ddlmZmZddlmZddlmZmZddlm Z ddl m Z edZ edd Z Gd d ZGd d eZGddeZGddee e fZej$eej$edZeidZidfdZdZy))MappingHashable)chain)GenericTypeVar)pvector transformKTVT_coT) covariantc(eZdZdZdZdZdZdZy)PMapViewaView type for the persistent map/dict type `PMap`. Provides an equivalent of Python's built-in `dict_values` and `dict_items` types that result from expreessions such as `{}.values()` and `{}.items()`. The equivalent for `{}.keys()` is absent because the keys are instead represented by a `PSet` object, which can be created in `O(1)` time. The `PMapView` class is overloaded by the `PMapValues` and `PMapItems` classes which handle the specific case of values and items, respectively Parameters ---------- m : mapping The mapping/dict-like object of which a view is to be created. This should generally be a `PMap` object. ct|ts't|tr t|}n t dt j |d|y)Nz"PViewMap requires a Mapping object_map) isinstancePMaprpmap TypeErrorobject __setattr__)selfms 2/usr/lib/python3/dist-packages/pyrsistent/_pmap.py__init__zPMapView.__init__s=!T"!W%G DEE4+c,t|jSN)lenrrs r__len__zPMapView.__len__&s499~rc0tt|d)Nz is immutable)rtype)rkvs rrzPMapView.__setattr__)sT$Z9::rctdNz"Persistent maps are not reversiblerr s r __reversed__zPMapView.__reversed__,<==rN)__name__ __module__ __qualname____doc__rr!rr)rrrr s",;>rrc.eZdZdZdZdZdZdZdZy) PMapValuesaView type for the values of the persistent map/dict type `PMap`. Provides an equivalent of Python's built-in `dict_values` type that result from expreessions such as `{}.values()`. See also `PMapView`. Parameters ---------- m : mapping The mapping/dict-like object of which a view is to be created. This should generally be a `PMap` object. c6|jjSrr itervaluesr s r__iter__zPMapValues.__iter__;syy##%%rc:||jjvSrr3)rargs r __contains__zPMapValues.__contains__>sdii**,,,rc2dtt|dSNz pmap_values()listiterr s r__str__zPMapValues.__str__Bd4:./q11rc2dtt|dSr:r<r s r__repr__zPMapValues.__repr__Er@rc||uryyNTFr/rxs r__eq__zPMapValues.__eq__Hs 9TrN r+r,r-r.r5r8r?rBrGr/rrr1r1/s  &-22rr1c.eZdZdZdZdZdZdZdZy) PMapItemsa|View type for the items of the persistent map/dict type `PMap`. Provides an equivalent of Python's built-in `dict_items` type that result from expreessions such as `{}.items()`. See also `PMapView`. Parameters ---------- m : mapping The mapping/dict-like object of which a view is to be created. This should generally be a `PMap` object. c6|jjSr)r iteritemsr s rr5zPMapItems.__iter__Zsyy""$$rcn|\}}||jvxr|j||k(S#t$rYywxYw)NF) Exceptionr)rr7r$r%s rr8zPMapItems.__contains__]s:UaDII~3$))A,!"33&&s ( 44c2dtt|dSNz pmap_items(r;r<r s rr?zPMapItems.__str__cT$t*-.a00rc2dtt|dSrPr<r s rrBzPMapItems.__repr__frQrcj||uryt|t|sy|j|jk(SrD)rr#rrEs rrGzPMapItems.__eq__is. 9TAtDz*5YY!&&((rNrHr/rrrJrJNs  %4 11)rrJcNeZdZdZdZfdZedZedZdZ edZ dZ e jZ d Zd Zd Zd Zd ZdZdZdZdZdZdZdZe j4ZdZeZeZeZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&e&Z'dZ(dZ)d Z*Gd!d"e+Z,d#Z-xZ.S)$ra 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 == {'a': 1, 'b': 3} True >>> m2 == {'a': 1, 'b': 3, 'c': 3} True >>> m3 == {'b': 3, 'c': 3} True >>> m3['c'] 3 >>> m3.c 3 )_size_buckets __weakref__ _cached_hashcJtt| |}||_||_|Sr)superr__new__rUrV)clssizebucketsr __class__s rr[z PMap.__new__s'T3',   rcBt|t|z}||}||fSr)hashr)r^keyindexbuckets r _get_bucketzPMap._get_buckets'S CL(f}rcttj||\}}|r|D]\}}||k(s |cSt|r)rreKeyError)r^rb_rdr$r%s r_getitemz PMap._getitemsF$$Wc2 6  18H smrcBtj|j|Sr)rrirVrrbs r __getitem__zPMap.__getitem__s}}T]]C00rc^tj||\}}|r|D] \}}||k(s yyyrD)rre)r^rbrhrdr$s r _containszPMap._containss@$$Wc2 6  18 rc:|j|j|Sr)rnrVrks rr8zPMap.__contains__s~~dmmS11rc"|jSr)iterkeysr s rr5z PMap.__iter__}}rctdr'r(r s rr)zPMap.__reversed__r*rc ||S#t$r4}tdjt|j||d}~wwxYw)Nz{0} has no attribute '{1}')rgAttributeErrorformatr#r+)rrbes r __getattr__zPMap.__getattr__sK 9   ,33DJ4G4GM  s A/?Ac#DK|jD] \}}| ywrrL)rr$rhs rrqz PMap.iterkeys%NN$ DAqG  c#DK|jD] \}}| ywrrz)rrhr%s rr4zPMap.itervaluesr{r|c#TK|jD]}|s|D] \}}||f ywr)rV)rrdr$r%s rrLzPMap.iteritemss8mm F"DAqQ$J s((ct|Sr)r1r s rvaluesz PMap.valuess $rcddlm}||S)N)PSet)_psetr)rrs rkeysz PMap.keyssDzrct|Sr)rJr s ritemsz PMap.itemss rc|jSrrUr s rr!z PMap.__len__s zzrcHdjtt|S)Nz pmap({0}))rvstrdictr s rrBz PMap.__repr__s!!#d4j/22rcD||uryt|tstSt|t|k7ryt|trt |dr&t |dr|j |j k7ry|j|jk(ryt|jt|jk(St|trt|j|k(St|jt|jk(S)NTFrX) rrNotImplementedrrhasattrrXrVrrLrrothers rrGz PMap.__eq__s 5=%)! ! t9E " eT "n-'%2P))U-?-??}}.()T%//2C-DD D t $()U2 2DNN$%ekkm)<<>> m1 = m(a=1, b=2) >>> m2 = m1.set('a', 3) >>> m3 = m1.set('c' ,4) >>> m1 == {'a': 1, 'b': 2} True >>> m2 == {'a': 3, 'b': 2} True >>> m3 == {'a': 1, 'b': 2, 'c': 4} True )evolverset persistentrrbvals rrzPMap.sets&||~!!#s+6688rc\|jj|jS)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}) )rremoverrks rrz PMap.remove%s$||~$$S)4466rcH |j|S#t$r|cYSwxYw)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 )rrgrks rdiscardz PMap.discard0s* ;;s# # K s  !!c*|jdg|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}) == {'a': 17, 'b': 2, 'c': 3, 'd': 35} True c|Srr/)lrs rzPMap.update..IsQr) update_with)rmapss rupdatez PMap.update@s t666rc |j}|D]<}|jD]'\}}|j|||vr ||||n|)>|jS)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)) == {'a': 3, 'b': 2} True 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}) )rrrr)r update_fnrrmaprbvalues rrzPMap.update_withKsn ,,. ^C!iik ^ U C3'>73<!?W\] ^ ^!!##rc$|j|Sr)rrs r__add__z PMap.__add__bs{{5!!rc&tt|ffSr)rrr s r __reduce__zPMap.__reduce__gsd4j]""rct||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 )rtransformationss rr zPMap.transformks4//rc|Srr/r s rcopyz PMap.copys rcReZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zy ) PMap._Evolver)_buckets_evolverrU_original_pmapcr||_|jj|_|j|_yr)rrVrrrU)r original_pmaps rrzPMap._Evolver.__init__s."/D $1$:$:$B$B$DD !&,,DJrcBtj|j|Sr)rrirrks rrlzPMap._Evolver.__getitem__s==!6!6<  AR$R !"$DAqCxCz,PMap._Evolver._reallocate..s+D!!A+Ds) rrrr from_iterableraappendrrr)rnew_sizenew_listr^r$r%rcs rrzPMap._Evolver._reallocates3t4455H4&(H++668G+++Dw+DD /1Q(*E?UO**Aq62()1vhHUO  /%,I$5$5$7D !  ! ! ( ( 2rc6|jjSr)ris_dirtyr s rrzPMap._Evolver.is_dirtys((113 3rc|jr3t|j|jj |_|j Sr)rrrUrrrr s rrzPMap._Evolver.persistents9}}&*4::t7L7L7W7W7Y&Z#&& &rc|jSrrr s rr!zPMap._Evolver.__len__s :: rcBtj|j|Sr)rrnrrks rr8zPMap._Evolver.__contains__s>>$"7"7= =rc&|j|yr)rrks r __delitem__zPMap._Evolver.__delitem__s KK rcPtj|j|\}}|rb|Dcgc]\}}||k(r ||f}}}t|t|z }|dkDr*|r|nd|j|<|xj|zc_|St dj |cc}}w)Nrz{0})rrerrrUrgrv)rrbrcrdr$r%r size_diffs rrzPMap._Evolver.removes ,,T-B-BCHME639L!Q!s(q!fL LK#j/9 q=AK:QUD))%0JJ)+JK5<<,- -Ms B"B"N)r+r,r- __slots__rrlrrrrrr!r8rrr/rr_Evolverrs?C  -  =  D 3  4 '   >  .rrc$|j|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 == {'a': 1, 'b': 2} True The changes are kept in the evolver. An updated pmap can be created using the persistent() function on the evolver. >>> m2 = e.persistent() >>> m2 == {'b': 2, 'c': 3} True 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. )rr s rrz PMap.evolvers:}}T""r)/r+r,r-r.rr[ staticmethodrerirlrnr8rgetr5r)rxrqr4rLrrrr!rBrG__ne__r__le____gt____ge__r?rrrrrrr__or__rr rrrr __classcell__)r_s@rrrns$JEI  1  2 ++C >  3=$^^F3F F F! 9 7 7$."F#08^.6^.@#rrc|r|}n dt|zxsd}|dgz}t|ts t |}|j D]8\}}t |}||z}||}|r|j||f1||fg||<:tt|tj|S#t$rd}YwxYw)Nr) rrNrrrrrarrrr) initialpre_sizer]r^r$r%hrcrds r_turbo_mappingr s s7|#(qD dVmG gw 'w- &1 GD  MM1a& ! !fXGEN& G gi..w7 88/ D sB11 B?>B?c4|s |dk(rtSt||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}) == {'a': 13, 'b': 14} True r) _EMPTY_PMAPr)rrs rrr0s  x1} '8 ,,rc t|S)z Creates a new persistent map. Inserts all key value arguments into the newly created map. >>> m(a=13, b=14) == {'a': 13, 'b': 14} True )r)kwargss rrr@s <rN)collections.abcrr itertoolsrtypingrrpyrsistent._pvectorrpyrsistent._transformationsr r r rr1rJrregisterrrrrr/rrrs-#'1 T]4(#>#>J>))@Y#72u9 Y#v $9@R# a - r