an9ddlmZmZddlmZddlmZddlmZGdde Z ej e ej e dZ e idZ idfdZd Zd S) )MappingHashable)chain)pvector transformcLeZdZdZdZfdZedZedZdZ edZ dZ e j Z d Zd Zd Zd Zd ZdZdZdZdZdZdZe jZdZeZeZeZdZdZdZ dZ!dZ"dZ#dZ$dZ%e%Z&dZ'dZ(dZ)Gd d!e*Z+d"Z,xZ-S)#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 == {'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_hashcttt||}||_||_|SN)superr __new__r r )clssizebucketsself __class__s 2/usr/lib/python3/dist-packages/pyrsistent/_pmap.pyrz PMap.__new__/s3T3'',,   cXt|t|z}||}||fSr)hashlen)rkeyindexbuckets r _get_bucketzPMap._get_bucket5s+S CLL(f}rct||\}}|r|D]\}}||kr|cSt|r)r r KeyError)rr_rkvs r_getitemz PMap._getitem;s[$$Wc22 6    188HHHsmmrcBt|j|Sr)r r&r rrs r __getitem__zPMap.__getitem__Es}}T]C000rclt||\}}|r|D]\}}||krdSdSdS)NTF)r r )rrr#rr$s r _containszPMap._containsHsW$$Wc22 6    188445urc8||j|Sr)r+r r(s r __contains__zPMap.__contains__Ts~~dmS111rc*|Sr)iterkeysrs r__iter__z PMap.__iter__Y}}rc ||S#t$r;}tdt|j||d}~wwxYw)Nz{0} has no attribute '{1}')r"AttributeErrorformattype__name__)rres r __getattr__zPMap.__getattr__\sa 9     ,33DJJ4GMM  s A6A  Ac#FK|D] \}}|V dSr iteritems)rr$r#s rr/z PMap.iterkeysd8NN$$  DAqGGGG  rc#FK|D] \}}|V dSrr;)rr#r%s r itervalueszPMap.itervalueskr=rc#>K|jD]}|r|D] \}}||fV dSr)r )rrr$r%s rr<zPMap.iteritemsosLm  F "DAqQ$JJJJ  rcDt|Sr)rr?r0s rvaluesz PMap.valuesust(()))rcDt|Sr)rr/r0s rkeysz PMap.keysxst}}'''rcDt|Sr)rr<r0s ritemsz PMap.items{st~~''(((rc|jSrr r0s r__len__z PMap.__len__~s zrc`dtt|S)Nz pmap({0}))r5strdictr0s r__repr__z PMap.__repr__s"!!#d4jj//222rc||urdSt|tstSt|t|krdSt|trt |dr"t |dr|j|jkrdS|j|jkrdSt| t| kSt|tr%t| |kSt| t| kS)NTFr) isinstancerNotImplementedrr hasattrrr rLr<rFrothers r__eq__z PMap.__eq__s. 5==4%)) "! ! t99E " "5 eT " " 3n-- '%2P2P )U-???u}..t(())T%//2C2C-D-DD D t $ $ 3(())U2 2DNN$$%%ekkmm)<)<<>> 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 persistentrrvals rr_zPMap.sets.||~~!!#s++66888rct||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^remover`r(s rrdz PMap.removes,||~~$$S))44666rcR ||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 )rdr"r(s rdiscardz PMap.discards= ;;s## #   KKK s  &&c |jdg|RS)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|Sr)lrs rzPMap.update..sQr) update_with)rmapss rupdatez PMap.updates! t66666rc |}|D]H}|D]1\}}||||vr||||n|2I|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)) == {'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}) )r^rFr_r`)r update_fnrnr^maprvalues rrmzPMap.update_withs ,,.. ^ ^C!iikk ^ ^ U C3'>>73<!?!?!?W\]]]] ^!!###rc,||Sr)rorRs r__add__z PMap.__add__s{{5!!!rc0tt|ffSr)pmaprLr0s r __reduce__zPMap.__reduce__sd4jj]""rc"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)rtransformationss rrzPMap.transforms4///rc|Srrir0s rcopyz PMap.copys rcTeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd S) PMap._Evolver)_buckets_evolverr _original_pmapch||_|j|_|j|_dSr)rr r^rr )r original_pmaps r__init__zPMap._Evolver.__init__s/"/D $1$:$B$B$D$DD !&,DJJJrcBt|j|Sr)r r&rr(s rr)zPMap._Evolver.__getitem__$s==!6<< z%PMap._Evolver.set..4s4)b)b)bQWQSUWbAgg2r((B9)b)b)br)rrr _reallocater r extend) rrrbkvrrr% new_bucketr$s ` @rr_zPMap._Evolver.set*s-4())D4:,===  S)>%?%?!?@@@sB ,,T-BCHHME6 "$$DAqCxxC<<)b)b)b)b)b[a)b)b)bJ;ED1%8#  !T !!&)))/9%e, a 02t%e, a Krc|dgz}|j}tjd|DD]E\}}t ||z}||r||||f=||fg||<Ft |_|j|dS)Nc3K|]}||V dSrri)rxs r z,PMap._Evolver._reallocate..Fs'+D+D!!+DA+D+D+D+D+D+Dr) rr`r from_iterablerappendrr^r)rnew_sizenew_listrr$r%rs rrzPMap._Evolver._reallocateCs4&(H+6688G++D+Dw+D+D+DDD / /1Q(*E?/UO**Aq62222()1vhHUOO%,II$5$5$7$7D !  ! ( ( 2 2 2 2 2rc4|jSr)ris_dirtyr0s rrzPMap._Evolver.is_dirtyRs(1133 3rc|r1t|j|j|_|jSr)rr r rr`rr0s rr`zPMap._Evolver.persistentUs?}} [&*4:t7L7W7W7Y7Y&Z&Z#& &rc|jSrrHr0s rrIzPMap._Evolver.__len__[s : rcBt|j|Sr)r r+rr(s rr-zPMap._Evolver.__contains__^s>>$"7== =rc0||dSr)rdr(s r __delitem__zPMap._Evolver.__delitem__as KK     rc.t|j\}}|rNfd|D}t|t|kr |r|nd|j|<|xjdzc_|St d)Nc*g|]\}}|k ||fSriri)rr$r%rs rrz(PMap._Evolver.remove..hs&FFF!QQ#XXq!fXXXrrz{0})r r rrr r"r5)rrrrrs ` rrdzPMap._Evolver.removeds ,,T-BCHHME6 FFFF6FFF v;;Z00AK3U::QUD)%0JJ!OJJK5<<,,-- -rN)r7 __module__ __qualname__ __slots__rr)rr_rrr`rIr-rrdrirr_Evolverr~sC  - - -  = = =      2 3 3 3 4 4 4 ' ' '     > > >    . . . . .rrc,||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. )rr0s rr^z PMap.evolverps:}}T"""r).r7rr__doc__rr staticmethodr r&r)r+r-rgetr1r9r/r?r<rBrDrFrIrMrT__ne__rW__le____gt____ge__rYr\r_rdrfrormru__or__rxrr|objectrr^ __classcell__)rs@rr r s$$JEI \ \111  \ 222 +C ***((()))333===$^F333F F F!!! 999 7 7 7 7 7 7$$$."""F###0008R.R.R.R.R.6R.R.R.h#######rr c|r|}n( dt|zpd}n#t$rd}YnwxYw|dgz}t|tst |}|D]C\}}t |}||z}||}|r|||f;||fg||<Dtt|t |S)Nr) r ExceptionrOrrLrFrrr rr) initialpre_sizerrr$r%hrrs r_turbo_mappingrs s7||#(qDD   DDD  dVmG gw ' ' w-- &&1 GGD  & MM1a& ! ! ! ! !fXGENN G gii..w77 8 88s  ++c@|s |dkrtSt||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 rrwrws* 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 )rw)kwargss rmrs <<rN)collections.abcrr itertoolsrpyrsistent._pvectorrpyrsistent._transformationsrrr registerrrrwrrirrrs--------''''''111111F#F#F#F#F#6F#F#F#P $999@nR## a - - - - r