a DOg@sdZgdZddlmZddlmZddlmZddlm Z ddl m Z dZ d Z e e fd d Ze e fd d ZefddZefddZefddZefddZefddZefddZefddZefddZefddZefd d!Zefd"d#Zefd$d%Zd&efd'efd(efgd(efd)efd&efgd)efd(efd'efgd'efd&efd)efgd*Zd+d,Zd-d.Zzdd/l mZWne!yYn0e"Z#e#fd0d1Z$zdd2l m$Z$Wne!yYn0Gd3d4d4Z%zdd5l m%Z%Wne!yYn0Gd6d7d7e"Z&d8d9Z'ed:gd;Z(Gdd?Z0dXdBdCZ1dDdEZ2zddFl m2Z2Wne!y|Yn0dGdHZ3dIdJZ4dYdLdMZ5dNdOZ6dPdQZ7dRdSZ8GdTdUdUZ9e"Z:GdVdWdWZ;dKS)ZzEfunctools.py - Tools for working with functions and callable objects )update_wrapperwrapsWRAPPER_ASSIGNMENTSWRAPPER_UPDATEStotal_orderingcache cmp_to_key lru_cachereducepartial partialmethodsingledispatchsingledispatchmethodcached_property)get_cache_token) namedtuple)recursive_repr)RLock) GenericAlias) __module____name__ __qualname____doc____annotations__)__dict__c Csd|D]2}zt||}Wnty(Yq0t|||q|D]}t||t||iq<||_|S)aUpdate a wrapper function to look like the wrapped function wrapper is the function to be updated wrapped is the original function assigned is a tuple naming the attributes assigned directly from the wrapped function to the wrapper function (defaults to functools.WRAPPER_ASSIGNMENTS) updated is a tuple naming the attributes of the wrapper that are updated with the corresponding attribute from the wrapped function (defaults to functools.WRAPPER_UPDATES) )getattrAttributeErrorsetattrupdate __wrapped__)wrapperwrappedassignedupdatedattrvaluer&./opt/alt/python39/lib64/python3.9/functools.pyr#s rcCstt|||dS)aDecorator factory to apply update_wrapper() to a wrapper function Returns a decorator that invokes update_wrapper() with the decorated function as the wrapper argument and the arguments to wraps() as the remaining arguments. Default arguments are as for update_wrapper(). This is a convenience function to simplify applying partial() to update_wrapper(). r!r"r#)r rr(r&r&r'rAs rcCs*t|||}||ur|S| o(||kS)zIReturn a > b. Computed by @total_ordering from (not a < b) and (a != b).type__lt__selfotherNotImplemented op_resultr&r&r' _gt_from_ltYsr1cCs(t|||}||ur|S|p&||kS)zEReturn a <= b. Computed by @total_ordering from (a < b) or (a == b).r)r,r&r&r' _le_from_lt`sr2cCs"t|||}||ur|S| S)z=Return a >= b. Computed by @total_ordering from (not a < b).r)r,r&r&r' _ge_from_ltgsr3cCs*t|||}||ur|S| p(||kS)zJReturn a >= b. Computed by @total_ordering from (not a <= b) or (a == b).r*__le__r,r&r&r' _ge_from_lensr6cCs(t|||}||ur|S|o&||kS)zFReturn a < b. Computed by @total_ordering from (a <= b) and (a != b).r4r,r&r&r' _lt_from_leusr7cCs"t|||}||ur|S| S)z=Return a > b. Computed by @total_ordering from (not a <= b).r4r,r&r&r' _gt_from_le|sr8cCs*t|||}||ur|S| o(||kS)zIReturn a < b. Computed by @total_ordering from (not a > b) and (a != b).r*__gt__r,r&r&r' _lt_from_gtsr;cCs(t|||}||ur|S|p&||kS)zEReturn a >= b. Computed by @total_ordering from (a > b) or (a == b).r9r,r&r&r' _ge_from_gtsr<cCs"t|||}||ur|S| S)z=Return a <= b. Computed by @total_ordering from (not a > b).r9r,r&r&r' _le_from_gtsr=cCs*t|||}||ur|S| p(||kS)zJReturn a <= b. Computed by @total_ordering from (not a >= b) or (a == b).r*__ge__r,r&r&r' _le_from_gesr@cCs(t|||}||ur|S|o&||kS)zFReturn a > b. Computed by @total_ordering from (a >= b) and (a != b).r>r,r&r&r' _gt_from_gesrAcCs"t|||}||ur|S| S)z=Return a < b. Computed by @total_ordering from (not a >= b).r>r,r&r&r' _lt_from_gesrBr:r5r?r+)r+r5r:r?csVfddtD}|stdt|}t|D]"\}}||vr.||_t||q.S)z6Class decorator that fills in missing ordering methodscs(h|] }t|dtt|dur|qSN)robject).0opclsr&r' z!total_ordering..z6must define at least one ordering operation: < > <= >=)_convert ValueErrormaxrr)rHrootsrootopnameopfuncr&rGr'rsrcsGfdddt}|S)z,Convert a cmp= function into a key= functioncsZeZdZdgZddZfddZfddZfdd Zfd d Zfd d Z dZ dS)zcmp_to_key..KobjcSs ||_dSrCrR)r-rRr&r&r'__init__szcmp_to_key..K.__init__cs|j|jdkSNrrSr-r.mycmpr&r'r+szcmp_to_key..K.__lt__cs|j|jdkSrUrSrVrWr&r'r:szcmp_to_key..K.__gt__cs|j|jdkSrUrSrVrWr&r'__eq__szcmp_to_key..K.__eq__cs|j|jdkSrUrSrVrWr&r'r5szcmp_to_key..K.__le__cs|j|jdkSrUrSrVrWr&r'r?szcmp_to_key..K.__ge__N) rrr __slots__rTr+r:rYr5r?__hash__r&rWr&r'Ks     r\)rD)rXr\r&rWr'rsr)rcCsXt|}|tur value Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence 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 sequence in the calculation, and serves as a default when the sequence is empty. z0reduce() of empty sequence with no initial valueN)iter_initial_missingnext StopIteration TypeError)ZfunctionZsequenceinitialitr%Zelementr&r&r'r s    r )r csJeZdZdZdZfddZddZeddZd d Z d d Z Z S) r zSNew function with partial application of the given arguments and keywords. )funcargskeywordsr __weakref__cs^t|stdt|dr8|j|}i|j|}|j}tt||}||_||_||_|S)Nz#the first argument must be callablerd) callablerahasattrrerfrdsuperr __new__)rHrdrerfr- __class__r&r'rks  zpartial.__new__cOs*i|j|}|jg|j|Ri|SrCrfrdre)r-rerfr&r&r'__call__*szpartial.__call__cCst|j}t|jg}|dd|jD|dd|jDt|jdkrld|dd |dS|dd |dS) Ncss|]}t|VqdSrC)repr)rExr&r&r' 2rJz#partial.__repr__..css |]\}}|d|VqdS)=Nr&rEkvr&r&r'rr3rJ functoolsz functools.(, )) r*rrprdextendrerfitemsrjoin)r-qualnamerer&r&r'__repr__.s  zpartial.__repr__cCs*t||jf|j|j|jpd|jp$dffSrC)r*rdrerfrr-r&r&r' __reduce__8szpartial.__reduce__cCst|tstdt|dkr0tdt||\}}}}t|rrt|trr|dur`t|trr|durzt|tsztdt|}|duri}nt|turt|}|duri}||_||_||_ ||_ dS)Nz(argument to __setstate__ must be a tuplezexpected 4 items in state, got zinvalid partial state) isinstancetupleralenrhdictr*rrdrerf)r-staterdrekwds namespacer&r&r' __setstate__<s4    zpartial.__setstate__) rrrrrZrkrorrrr __classcell__r&r&rlr'r s  r )r c@sFeZdZdZddZddZddZd d d Zed d Z e e Z dS)r zMethod descriptor with partial application of the given arguments and keywords. Supports wrapping existing descriptors and handles non-descriptor callables as instance methods. cOsft|s t|ds td|t|trP|j|_|j||_i|j||_n||_||_||_dS)N__get__z${!r} is not callable or a descriptor) rhriraformatrr rdrerf)r-rdrerfr&r&r'rTbs  zpartialmethod.__init__cCsNdtt|j}ddd|jD}d}|j|jj|jj |j ||dS)Nrycss|]\}}d||VqdS)z{}={!r}N)rrtr&r&r'rrwsz)partialmethod.__repr__..z*{module}.{cls}({func}, {args}, {keywords}))modulerHrdrerf) r}maprprerfr|rrmrrrd)r-rerf format_stringr&r&r'rus  zpartialmethod.__repr__csfdd}j|_|_|S)Ncs,ij|}j|gj|Ri|SrCrn) cls_or_selfrerfrr&r'_methodsz3partialmethod._make_unbound_method.._method)__isabstractmethod___partialmethod)r-rr&rr'_make_unbound_methods z"partialmethod._make_unbound_methodNcCst|jdd}d}|durh|||}||jurht|g|jRi|j}z |j|_WntyfYn0|dur|||}|S)Nr) rrdr rerf__self__rrr)r-rRrHgetresultnew_funcr&r&r'rs    zpartialmethod.__get__cCst|jddSNrFrrdrr&r&r'rsz"partialmethod.__isabstractmethod__)N) rrrrrTrrrpropertyr classmethodr__class_getitem__r&r&r&r'r Zs   r cCst|tr|j}q|SrC)rr rdrdr&r&r'_unwrap_partials r CacheInfo)hitsmissesmaxsizecurrsizec@s(eZdZdZdZefddZddZdS) _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. hashvaluecCs||dd<|||_dSrCr)r-tuphashr&r&r'rTs z_HashedSeq.__init__cCs|jSrCrrr&r&r'r[sz_HashedSeq.__hash__N)rrrrrZrrTr[r&r&r&r'rs rc s|}|r&||7}|D] } || 7}q|rh||fdd|D7}|r||fdd|D7}n$||dkr|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. c3s|]}|VqdSrCr&rErvr*r&r'rrrJz_make_key..c3s|]}|VqdSrCr&rrr&r'rrrJr)r|valuesr) rertypedkwd_mark fasttypesrr*rkeyitemr&rr' _make_keys   rFcs~ttrdkrldnTtr\ttr\d}t|t}fdd|_t||Sdurltdfdd}|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) rrcs dSNrrr&r&rr&r'rJzlru_cache..Nz=Expected first argument to be an integer, a callable, or Nonecs(t|t}fdd|_t||S)Ncs dSrr&r&rr&r'rrJz8lru_cache..decorating_function..)_lru_cache_wrapper _CacheInfocache_parametersr) user_functionr rr&r'decorating_functionsz&lru_cache..decorating_function) rintrhboolrrrrra)rrrr rr&rr'rs   rcstt d\id djjt gddgdd< dkrh fdd}nN dur fdd}n* fdd} fdd } fd d }||_||_|S) N)rrrFcsd7|i|}|SNrr&)rerr)rrr&r'r sz#_lru_cache_wrapper..wrappercsL||}|}|ur*d7|Sd7|i|}||<|Srr&)rerrr)r cache_getrmake_keyrsentinelrrr&r'r &s  c sp ||} |}|dur|\}}}}||<||< }||< <||< |<d7|WdS d7 Wdn1s0Y|i|} |vrnr } || <|| <|  } } d < <| =| |<n6 }| ||g}||< <|< kWdn1sb0Y|Srr&) rerrlinkZ link_prevZ link_nextZ_keyrZlastZoldrootZoldkeyZ oldresult)KEYNEXTPREVRESULTrr cache_lenfullrlockrrrrOrrr&r'r 5sB  &  *cs8 WdS1s*0YdS)zReport cache statisticsNr&r&)rrrrrrr&r' cache_infonsz&_lru_cache_wrapper..cache_infocsP8ddgdd<ddWdn1sB0YdS)z$Clear the cache and cache statisticsNrF)clearr&)rrrrrrOr&r' cache_clearss z'_lru_cache_wrapper..cache_clear)rDrr__len__rrr)rrrrr rrr&)rrrrrrrrrrrrrrrOrrrr'r s* *9 r)rcCstdd|S)z@Simple lightweight unbounded cache. Sometimes called "memoize".N)r)r)rr&r&r'rsrcCsg}dd|D}|s|S|D]2}|d}|D]}||ddvr.d}qq.qRq|durbtd|||D]}|d|krp|d=qpqdS)zMerges MROs in *sequences* to a single MRO using the C3 algorithm. Adapted from https://www.python.org/download/releases/2.3/mro/. cSsg|] }|r|qSr&r&rEsr&r&r' rJz_c3_merge..rrNzInconsistent hierarchy) RuntimeErrorappend) sequencesrs1 candidates2seqr&r&r' _c3_merges"  rNc stt|jD]$\}tdrt|j|}q8qd}rDtngt|jd|}g}t|j|d}D]0t|rttfdd|jDst|qt|D] qfdd|D}fdd|D}fd d|D} t |gg||| |g|g|gS) 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__rNc3s|]}t|VqdSrC) issubclass)rEb)baser&r'rrsz_c3_mro..csg|]}t|dqSabcs_c3_mrorErrr&r'rrJz_c3_mro..csg|]}t|dqSrrrrr&r'rrJcsg|]}t|dqSrrrrr&r'rrJ) enumeratereversed __bases__rirlistranyrremover) rHriboundaryexplicit_basesabstract_bases other_basesexplicit_c3_mrosabstract_c3_mros other_c3_mrosr&)rrr'rsD   rcstjfddfddDfddfddDtg}D]}g}|D]0}|vrht|rh|fdd|jDqh|s||qX|jtd d |D] }|D]}||vr||qqqXt|d S) 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. cs(|vo&t|do&t|t o&t|S)N__mro__)rirrr)typ)basesrHr&r' is_relateds  z _compose_mro..is_relatedcsg|]}|r|qSr&r&rEn)rr&r'rrJz _compose_mro..cs&D]}||kr||jvrdSqdS)NTF)r)rr.)typesr&r'is_strict_basesz$_compose_mro..is_strict_basecsg|]}|s|qSr&r&r)rr&r'rrJcsg|]}|vr|qSr&r&r)type_setr&r'rrJT)rreverser)setr__subclasses__rrsortrr)rHrmrorfoundsubsubclsr&)rrHrrrrr' _compose_mros*    rcCstt||}d}|D]R}|dur\||vrX||jvrX||jvrXt||sXtd||qj||vr|}q||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 {})rkeysrrrrr)rHregistryrmatchtr&r&r' _find_impls r csddl}ddl}i|dfddddd fdd fd d }t|d d |t<|_|_||_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 sxdur"t}|kr"|z |}WnDtyrz |}Wntydt|}Yn0||<Yn0|S)zgeneric_func.dispatch(cls) -> Runs the dispatch algorithm to return the best available implementation for the given *cls* registered on *generic_func*. N)rrKeyErrorr )rH current_tokenimpl) cache_tokendispatch_cacherr&r'dispatch1s    z singledispatch..dispatchcSst|tot|t SrC)rr*rrGr&r&r'_is_valid_dispatch_typeHsz/singledispatch.._is_valid_dispatch_typecsr |durfddSn~|dur8tddtdi}|sXtdd}d d lm}tt||\}std |d d|<durtd rt |S)zgeneric_func.register(cls, func) -> func Registers a new implementation for the given *cls* on a *generic_func*. Ncs |SrCr&)f)rHregisterr&r'rTrJz2singledispatch..register..z(Invalid first argument to `register()`. z is not a class.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. r) rartypingrr_r]r|rirr)rHrdannrargname)rrrrrrGr'rKs8   z singledispatch..registercs*|std|dj|i|S)Nz( requires at least 1 positional argumentr)rarm)rekw)rfuncnamer&r'r sszsingledispatch..wrapperrzsingledispatch function)N) rweakrefWeakKeyDictionaryrrDrrMappingProxyTyperr _clear_cacher)rdrrr r&)rrrrrrrr'r s (   r c@s8eZdZdZddZd ddZd ddZed d ZdS) r zSingle-dispatch generic method descriptor. Supports wrapping existing descriptors and handles non-descriptor callables as instance methods. cCsRt|s t|ds t|dt||_||_t|ttfrH|j |_ n||_ dS)Nrz is not callable or a descriptor) rhrirar dispatcherrdr staticmethodr__func__ _wrapped_funcr-rdr&r&r'rTs  zsingledispatchmethod.__init__NcCs.t|ttfrt|jdi|_|jj||dS)zgeneric_method.register(cls, func) -> func Registers a new implementation for the given *cls* on a *generic_method*. rr)rr rrr!rrr)r-rHmethodr&r&r'rs zsingledispatchmethod.registercs0fdd}j|_j|_t|j|S)Ncs(j|dj}||i|SrU)rrrmr)rekwargsr$rHrRr-r&r'rsz-singledispatchmethod.__get__.._method)rrrr")r-rRrHrr&r&r'rs  zsingledispatchmethod.__get__cCst|jddSrrrr&r&r'rsz)singledispatchmethod.__isabstractmethod__)N)N) rrrrrTrrrrr&r&r&r'r s  r c@s.eZdZddZddZdddZeeZdS) rcCs ||_d|_|j|_t|_dSrC)rdattrnamerrrr#r&r&r'rTszcached_property.__init__cCs8|jdur||_n"||jkr4td|jd|ddS)Nz?Cannot assign the same cached_property to two different names (z and z).)r'ra)r-ownernamer&r&r' __set_name__s  zcached_property.__set_name__Nc Cs|dur |S|jdurtdz |j}Wn6ty^dt|jd|jd}t|dYn0||jt}|tur|jv||jt}|tur| |}z|||j<Wn6tydt|jd|jd}t|dYn0Wdn1s0Y|S)NzGCannot use cached_property instance without calling __set_name__ on it.zNo '__dict__' attribute on z instance to cache z property.zThe '__dict__' attribute on z7 instance does not support item assignment for caching ) r'rarrr*rr _NOT_FOUNDrrd)r-instancer(rmsgvalr&r&r'rs:      .zcached_property.__get__)N) rrrrTr*rrrrr&r&r&r'rs r)rF)N)s                     AJ   .t  -*f5