QfdZgdZddlmZddlmZddlmZddlm Z ddl m Z dZ d Z e e fd Ze e fd Zd Zd ZdZdZdZdZdZdZdZdZdZdZdefdefdefgdefdefdefgdefdefdefgdefdefdefgdZdZdZ ddlmZe!Z"e"fd Z# dd!lm#Z#Gd"d#Z$ dd$lm$Z$Gd%d&e!Z%d'Z&ed(gd)Z'Gd*d+e(Z)e!fe*e+he,e-e.fd,Z/d;d-Z0d.Z1 dd/lm1Z1d0Z2d1Z3d7$g 77c`t|j||}|tur|S| xr||k7S)zIReturn a > b. Computed by @total_ordering from (not a < b) and (a != b).type__lt__NotImplementedselfother op_results r) _gt_from_ltr6Y7T !!$.IN"= *TU]*r,c^t|j||}|tur|S|xs||k(S)zEReturn a <= b. Computed by @total_ordering from (a < b) or (a == b).r.r2s r) _le_from_ltr9`5T !!$.IN"  % %r,cRt|j||}|tur|S| S)z=Return a >= b. Computed by @total_ordering from (not a < b).r.r2s r) _ge_from_ltr<g.T !!$.IN"=r,c`t|j||}|tur|S| xs||k(S)zJReturn a >= b. Computed by @total_ordering from (not a <= b) or (a == b).r/__le__r1r2s r) _ge_from_lerAn7T !!$.IN"= )DEM)r,c^t|j||}|tur|S|xr||k7S)zFReturn a < b. Computed by @total_ordering from (a <= b) and (a != b).r?r2s r) _lt_from_lerDu5T !!$.IN"  &&r,cRt|j||}|tur|S| S)z=Return a > b. Computed by @total_ordering from (not a <= b).r?r2s r) _gt_from_lerG|r=r,c`t|j||}|tur|S| xr||k7S)zIReturn a < b. Computed by @total_ordering from (not a > b) and (a != b).r/__gt__r1r2s r) _lt_from_gtrKr7r,c^t|j||}|tur|S|xs||k(S)zEReturn a >= b. Computed by @total_ordering from (a > b) or (a == b).rIr2s r) _ge_from_gtrMr:r,cRt|j||}|tur|S| S)z=Return a <= b. Computed by @total_ordering from (not a > b).rIr2s r) _le_from_gtrOr=r,c`t|j||}|tur|S| xs||k(S)zJReturn a <= b. Computed by @total_ordering from (not a >= b) or (a == b).r/__ge__r1r2s r) _le_from_gerSrBr,c^t|j||}|tur|S|xr||k7S)zFReturn a > b. Computed by @total_ordering from (a >= b) and (a != b).rQr2s r) _gt_from_gerUrEr,cRt|j||}|tur|S| S)z=Return a < b. Computed by @total_ordering from (not a >= b).rQr2s r) _lt_from_gerWr=r,rJr@rRr0)r0r@rJrRc tDchc]#}t||dtt|dus"|%}}|s tdt |}t|D]\}}||vs ||_t ||| |Scc}w)z6Class decorator that fills in missing ordering methodsNz6must define at least one ordering operation: < > <= >=)_convertrobject ValueErrormaxrr)clsoprootsrootopnameopfuncs r)rrs# ^(Bgc2t&.Kobjc||_yNrf)r3rfs r)__init__zcmp_to_key..K.__init__s DHr,cD|j|jdkSNrrir3r4mycmps r)r0zcmp_to_key..K.__lt__599-1 1r,cD|j|jdkDSrlrirms r)rJzcmp_to_key..K.__gt__ror,cD|j|jdk(Srlrirms r)__eq__zcmp_to_key..K.__eq__599-2 2r,cD|j|jdkSrlrirms r)r@zcmp_to_key..K.__le__rsr,cD|j|jdk\Srlrirms r)rRzcmp_to_key..K.__ge__rsr,N) rrr __slots__rjr0rJrrr@rR__hash__)rnsr)Kres-G   2 2 3 3 3r,rx)rZ)rnrxs` r)rrsF Hr,)rct|}|tur t|}n|}|D] }|||} |S#t$r t ddwxYw)a reduce(function, iterable[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence or iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. z0reduce() of empty iterable with no initial valueN)iter_initial_missingnext StopIteration TypeError)functionsequenceinitialitr(elements r)r r sq hB"" NHE ( L NBDIM N Ns 5A )r cReZdZdZdZfdZdZedZdZ dZ xZ S)r zSNew function with partial application of the given arguments and keywords. )funcargskeywordsr __weakref__ct|s tdt|dr+|j|z}i|j|}|j }t t|#|}||_||_||_|S)Nz#the first argument must be callabler) callabler~hasattrrrrsuperr __new__)r]rrrr3 __class__s r)rzpartial.__new__su~AB B 4 99t#D4$--484H99DWc*3/     r,cdi|j|}|jg|j|i|Srhrrr)r3rrs r)__call__zpartial.__call__+s70dmm0x0tyy7$))7d7h77r,ct|j}t|jg}|j d|j D|j d|j jDt|jdk(rd|ddj|dS|ddj|dS)Nc32K|]}t|ywrh)repr).0xs r) z#partial.__repr__..3s/YDGYsc30K|]\}}|d|yw=Nrkvs r)rz#partial.__repr__..4s!E/DVaqc1%L/D functoolsz functools.(, )) r/rrrextendrritemsrjoin)r3qualnamers r)__repr__zpartial.__repr__/s:**TYY  /TYY// Et}}/B/B/DEE : K /z499T?*;1= =1TYYt_-Q//r,ct||jf|j|j|jxsd|jxsdffSrh)r/rrrrr3s r) __reduce__zpartial.__reduce__9sCDzDII<$))TYY}}$dmm&;t*== =r,ct|ts tdt|dk7rtdt||\}}}}t |r4t|tr$|t|t r|t|t s tdt|}|i}nt |t ur t |}|i}||_||_||_ ||_ y)Nz(argument to __setstate__ must be a tuplezexpected 4 items in state, got zinvalid partial state) isinstancetupler~lenrdictr/rrrr)r3staterrkwds namespaces r) __setstate__zpartial.__setstate__=s%'FG G u:?=c%j\JK K&+#dD)ju&=  Zd%;  !*Y*E34 4T{ <D $Zt #:D  I!    r,) rrrrrvrrrrrr __classcell__)rs@r)r r s;FI 800=r,r )r cJeZdZdZdZdZdZddZedZ e e Z y) r zMethod descriptor with partial application of the given arguments and keywords. Supports wrapping existing descriptors and handles non-descriptor callables as instance methods. c&t|s&t|dstdj|t |t r;|j |_|j|z|_i|j||_y||_||_||_y)N__get__z${!r} is not callable or a descriptor) rrr~formatrr rrr)r3rrrs r)rjzpartialmethod.__init__cs~~gdI&>B"(&,0 0 dM * DI D(DI9t}}99DMDIDI$DMr,cZt|}|j}|j}t|jg}|j t t|j|j d|jjD|d|ddj|dS)Nc30K|]\}}|d|ywrrrs r)rz)partialmethod.__repr__..|s!C-BTQqc1%L-Br.rrr) r/rrrrrmaprrrr)r3r]modulerrs r)rzpartialmethod.__repr__vs4j##TYY  Cdii() CT]]-@-@-BCC8*Adiio%6a88r,cBfd}j|_|_|S)Nchij|}j|gj|i|Srhr) cls_or_selfrrr3s r)_methodz3partialmethod._make_unbound_method.._methods:4$--484H499[H499HtHxH Hr,)__isabstractmethod___partialmethod)r3rs` r)_make_unbound_methodz"partialmethod._make_unbound_methods& I(,'@'@$!%r,Nc6t|jdd}d}|L|||}||jur5t|g|ji|j} |j |_| |jj||}|S#t $rY/wxYw)Nr) rrr rr__self__r rr)r3rfr]getresultnew_funcs r)rzpartialmethod.__get__sdiiD1 ?3}Htyy(!GDIIGG&.&7&7FO >..088cBF &sB BBc0t|jddSNrFrrrs r)rz"partialmethod.__isabstractmethod__tyy"8%@@r,rh) rrrrrjrrrpropertyr classmethodr__class_getitem__rr,r)r r [s>%&9&AA$L1r,r c`t|tr|j}t|tr|Srh)rr rrs r)_unwrap_partialrs& T7 #yy T7 # Kr, CacheInfo)hitsmissesmaxsizecurrsizec$eZdZdZdZefdZdZy) _HashedSeqz This class guarantees that hash() will be called no more than once per element. This is important because the lru_cache() will hash the key multiple times on a cache miss. hashvaluec(||dd|||_yrhr)r3tuphashs r)rjz_HashedSeq.__init__sQcr,c|jSrhrrs r)rwz_HashedSeq.__hash__s ~~r,N)rrrrrvrrjrwrr,r)rrs I!%#r,rc$|}|r||z }|jD]} || z } |rC||fd|Dz }|rJ||fd|jDz }t|S||dk(r|d|vr|dSt|S)aMake a cache key from optionally typed positional and keyword arguments The key is constructed in a way that is flat as possible rather than as a nested structure that would take more memory. If there is only a single argument and its data type is known to cache its hash value, then that argument is returned without a wrapper. This saves space and improves lookup speed. c3.K|] }|ywrhrrrr/s r)rz_make_key..s+dT!Wdc3.K|] }|ywrhrrs r)rz_make_key..s8-Qa-rr)rvaluesr) rrtypedkwd_mark fasttypesrr/rkeyitems ` r) _make_keyrs$ C  xJJLD 4KC!  u+d+++  58$++-88 8C c? SQ4A<941v c?r,cttrdkrYdnVtr>ttr.dc}t |t }fd|_t||S tdfd}|S)aLeast-recently-used cache decorator. If *maxsize* is set to None, the LRU features are disabled and the cache can grow without bound. If *typed* is True, arguments of different types will be cached separately. For example, f(3.0) and f(3) will be treated as distinct calls with distinct results. Arguments to the cached function must be hashable. View the cache statistics named tuple (hits, misses, maxsize, currsize) with f.cache_info(). Clear the cache and statistics with f.cache_clear(). Access the underlying function with f.__wrapped__. See: https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU) rcdSNrrrrsr)zlru_cache.. %,Pr,z=Expected first argument to be an integer, a callable, or NonecVt|t}fd|_t||S)NcdSrrrsr)rz8lru_cache..decorating_function..rr,)_lru_cache_wrapper _CacheInfocache_parametersr) user_functionr#rrs r)decorating_functionz&lru_cache..decorating_functions($]GUJO#P g}55r,) rintrboolrrrrr~)rrrr#rs`` r)r r s2'3 Q;G ' z%6!(# w$]GUJO#P g}55   KM M6 r,cd ttd\  i dxd j j t gddgdddk(rfd}n# fd}n fd} fd} fd}||_||_|S) N)rrrFc"dz |i|}|SNrr)rrrrrs r)r#z#_lru_cache_wrapper..wrappers" aKF"D1D1FMr,ch|| }| }| urdz |Sdz  |i|}||<|Srr) rrrrr cache_getrmake_keyrsentinelrrs r)r#z#_lru_cache_wrapper..wrapper%s[4u-CsH-FX%  aKF"D1D1FE#JMr,c||}5|}|:|\}}}}|| <||<}|x| <<||<| <dz |cdddSdz ddd|i|}5|vrnTr.} || <|| <|  } } dx <<| =| |<n$}|||g}|x| <x<|<k\ddd|S#1swYwxYw#1swY|SxYwrr)rrrlink link_prev link_next_keyrlastoldrootoldkey oldresultKEYNEXTPREVRESULTrr cache_lenfullrlockrrrr`rrs r)r#z#_lru_cache_wrapper..wrapper4sz4u-C ~#9=6Iy$&/IdO&/IdO:D.22DJd!%DJ!%DJAID!  ! #D1D1F%< "G#&GCL&,GFO#4=D!#YF $V I/33DIV f ")E#J :D $V4D;??DJ?deCj&K72DGHMgHMs;CC*ACCC$cT5cdddS#1swYyxYw)zReport cache statisticsNr)rrrrrrsr) cache_infoz&_lru_cache_wrapper..cache_infoms' dFGY[A B BTTs'cr5jddgdddxddddy#1swYyxYw)z$Clear the cache and cache statisticsNrF)clear)rrrrrr`sr) cache_clearz'_lru_cache_wrapper..cache_clearrs< KKMT4.DG D6D TTs -6)rZrr__len__rrr)rrrrr#rrrrrrrrrrrrrrr`rs```` @@@@@@@@@@@@@@r)rr sxHH(D$V ED6 D I I 7D DT4&DG!|    7 7 7 rBB $G%G Nr,)rc&td|S)z@Simple lightweight unbounded cache. Sometimes called "memoize".N)r)r )rs r)rrs "9T "= 11r,cg} |Dcgc]}|s| }}|s|S|D]}|d}|D] }||ddvs d}n td|j||D]}|d|k(s |d=gcc}w)zMerges MROs in *sequences* to a single MRO using the C3 algorithm. Adapted from https://docs.python.org/3/howto/mro.html. rrNzInconsistent hierarchy) RuntimeErrorappend) sequencesrss1 candidates2seqs r) _c3_merger)s F  )/ 1QQ /MB1I12& $I   78 8 i C1v"F! /s A+A+Nctt|jD]+\}tdst |j|z }nd}|r t |ng}t |jd|}g}t |j|d}|D]?t |stfd|jDr/|jA|D]|j|Dcgc]}t||}}|Dcgc]}t||} }|Dcgc]}t||} }t|gg|z| z| z|gz|gz|gzScc}wcc}wcc}w)aComputes the method resolution order using extended C3 linearization. If no *abcs* are given, the algorithm works exactly like the built-in C3 linearization used for method resolution. If given, *abcs* is a list of abstract base classes that should be inserted into the resulting MRO. Unrelated ABCs are ignored and don't end up in the result. The algorithm inserts ABCs where their functionality is introduced, i.e. issubclass(cls, abc) returns True for the class itself but returns False for all its direct base classes. Implicit ABCs for a given class (either registered or inferred from the presence of a special method like __len__) are inserted directly after the last ABC explicitly listed in the MRO of said class. If two implicit ABCs end up next to each other in the resulting MRO, their ordering depends on the order of types in *abcs*. __abstractmethods__rNc36K|]}t|ywrh) issubclass)rbbases r)rz_c3_mro..s--: 1d#]sabcs) enumeratereversed __bases__rrlistr-anyr"remove_c3_mror)) r]r1iboundaryexplicit_basesabstract_bases other_basesr/explicit_c3_mrosabstract_c3_mros other_c3_mross ` r)r8r8s"Xcmm454 4. /3==)A-H 6 4:2D#-- 23NNs}}XY/0K c4 --0]]-*  ! !$ '  D=KL^T40^L=KL^T40^L:EF+$WT-+MF   + ,.; <  *+ ,/:m < MLFs1E E"%E'c X tj fd}Dcgc] }||s |c}fd}Dcgc] }||r |c}t}g}D]}g}|jD]B} | vst| s|j | jD cgc] } | |vs|  c} D|s|j |n|j t d|D]} | D]} | |vs|j | !t|Scc}wcc}wcc} w)zCalculates the method resolution order for a given class *cls*. Includes relevant abstract base classes (with their respective bases) from the *types* iterable. Uses a modified C3 linearization algorithm. cj|vxr-t|dxrt|t xr t|S)N__mro__)rrrr-)typbasesr]s r) is_relatedz _compose_mro..is_relateds?5 :WS)%<:)3C)F%F:%/S%9 ;r,c@D]}||k7s ||jvsyy)NTF)rC)rDr4typess r)is_strict_basez$_compose_mro..is_strict_bases'Ee|u}} 4r,T)rreverser0)setrC__subclasses__r-r"sortrr8) r]rHrFnrItype_setmrorDfoundsubr$subclsrEs `` @r) _compose_mrorTs(  E; /1AQ /E  71^A%6Q 7E5zH C%%'C%JsC$8 FAX aFG( JJsO  sD )C$JJv& 3S !!7 0 8Gs" DD D"D"( D' 2D' ct||j}d}|D]T}|I||vrC||jvr5||jvr't||st dj ||n ||vsS|}V|j |S)a^Returns the best matching implementation from *registry* for type *cls*. Where there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation. Note: if *registry* does not contain an implementation for the base *object* type, this function may return None. NzAmbiguous dispatch: {} or {})rTkeysrCr-r!rr)r]registryrPmatchts r) _find_implrZs sHMMO ,C E   X !3;;"6"'s{{":&0&:"#A#H#H1$  =E << r,cB ddl ddl}i |jd fd fdfdd  fd fd}t|dd | t< |_|_ j |_j|_ t|||S) akSingle-dispatch generic function decorator. Transforms a function into a generic function, which can have different behaviours depending upon the type of its first argument. The decorated function acts as the default implementation, and additional implementations can be registered using the register() attribute of the generic function. rNc!t}|k7rj| |}|S#t$r+ |}n#t$rt|}YnwxYw||<Y|SwxYw)zgeneric_func.dispatch(cls) -> Runs the dispatch algorithm to return the best available implementation for the given *cls* registered on *generic_func*. )rrKeyErrorrZ)r] current_tokenimpl cache_tokendispatch_cacherWs r)dispatchz singledispatch..dispatch0s  "+-Mm+$$&+  '!#&D  ' 1} 1!#x0 1"&N3   's/- A!=A!AA!AA! A!c@ddlm}m}|||jhvS)Nr) get_originUnion)typingrdre UnionType)r]rdrerHs r)_is_union_typez&singledispatch.._is_union_typeGs,#5%//":::r,cvt|tryddlm}|xrt d||DS)NTrget_argsc3<K|]}t|tywrh)rr/)rargs r)rzBsingledispatch.._is_valid_dispatch_type..PsC]cJsD)]s)rr/rfrkall)r]rkrhs r)_is_valid_dispatch_typez/singledispatch.._is_valid_dispatch_typeKs9 c4 #s#DCXc]CC Er,cr| fdS|tddtdi}|stdd}ddlm}t t ||j \}s,rtd |d d td |d d rdd lm}|D]}| |< n| < tdr t j|S)zgeneric_func.register(cls, func) -> func Registers a new implementation for the given *cls* on a *generic_func*. c|Srhr)fr]registers r)rz2singledispatch..register..[s #q!1r,z(Invalid first argument to `register()`. z is not a class or union type.rz(Invalid first argument to `register()`: zS. Use either `@register(some_class)` or plain `@register` on an annotated function.r)get_type_hintszInvalid annotation for z. z not all arguments are classes.z is not a class.rjr+) r~rrfrtr|rzrrkrrr) r]rannrtargnamerkrmrhror`rarsrWs ` r)rsz singledispatch..registerRsL #3 '|11>g;=#0"5C>sgF01 D .^D%9%?%?%A BCLGS*3/!#&#1'B'!@B $1'B'!13 #  '} $ %!HSM  730E#F)+K r,cZ|std|dj|i|S)Nz( requires at least 1 positional argumentr)r~r)rkwrbfuncnames r)r#zsingledispatch..wrappersDxj)445 5+xQ))*D7B77r,rzsingledispatch functionrh) rHweakrefWeakKeyDictionaryrrZrsrbMappingProxyTyperWr _clear_cacher) rrzr#rhror`rbraryrsrWrHs @@@@@@@@@r)r r sH..0NK.;E22h8tZ)BCHHVGG--h7G)//G7D! Nr,c6eZdZdZdZddZddZedZy)rzSingle-dispatch generic method descriptor. Supports wrapping existing descriptors and handles non-descriptor callables as instance methods. c|t|st|dst|dt||_||_y)Nrz is not callable or a descriptor)rrr~r dispatcherrr3rs r)rjzsingledispatchmethod.__init__s8~gdI&>th&FGH H(. r,Nc<|jj||S)zgeneric_method.register(cls, func) -> func Registers a new implementation for the given *cls* on a *generic_method*. r)rrs)r3r]methods r)rszsingledispatchmethod.registers ''&'99r,cfd}j|_j|_t|j|S)Ncjj|dj}|j|i|Srl)rrbrr)rkwargsrr]rfr3s r)rz-singledispatchmethod.__get__.._methods>__--d1g.?.?@F+6>>#s+T3J3J2MNDDHMMCTT^` n$.  /s A6#B56rs:   ""=2,>)# 70+&*'+&*'+&+&+&(+&+&+&(+&+&+&(+&+&+&(  $ & %8'78 !??B " C2FC2P %N O "  st>,\rh -26+Z("T4vt!A!APX *2*2  P  X  T   sH D'D2/D=2E'D/.D/2D:9D:=EEEE