o ?Og@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 mZWn e!yYnwe"Z#e#fd0d1Z$zdd2l m$Z$Wn e!yYnwGd3d4d4Z%zdd5l m%Z%Wn e!yYnwGd6d7d7e"Z&d8d9Z'ed:gd;Z(Gdd?Z0dXdBdCZ1dDdEZ2zddFl m2Z2Wn e!y;YnwdGdHZ3dIdJZ4dYdLdMZ5dNdOZ6dPdQZ7dRdSZ8GdTdUdUZ9e"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]}zt||}Wn tyYqwt|||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&0/opt/alt/python310/lib64/python3.10/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 rcC*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_ltYr2cC(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` r5cC"t|||}||ur|S| S)z=Return a >= b. Computed by @total_ordering from (not a < b).r*r-r&r&r' _ge_from_ltgr8cC*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_lenr3r=cC(t|||}||ur|S|o||kS)zFReturn a < b. Computed by @total_ordering from (a <= b) and (a != b).r;r-r&r&r' _lt_from_leur6r?cCr7)z=Return a > b. Computed by @total_ordering from (not a <= b).r;r-r&r&r' _gt_from_le|r9r@cCr))zIReturn a < b. Computed by @total_ordering from (not a > b) and (a != b).r+__gt__r-r&r&r' _lt_from_gtr3rCcCr4)zEReturn a >= b. Computed by @total_ordering from (a > b) or (a == b).rAr-r&r&r' _ge_from_gtr6rDcCr7)z=Return a <= b. Computed by @total_ordering from (not a > b).rAr-r&r&r' _le_from_gtr9rEcCr:)zJReturn a <= b. Computed by @total_ordering from (not a >= b) or (a == b).r+__ge__r-r&r&r' _le_from_ger3rHcCr>)zFReturn a > b. Computed by @total_ordering from (a >= b) and (a != b).rFr-r&r&r' _gt_from_ger6rIcCr7)z=Return a < b. Computed by @total_ordering from (not a >= b).rFr-r&r&r' _lt_from_ger9rJrBr<rGr,)r,r<rBrGcsVfddtD}|stdt|}t|D]\}}||vr(||_t||qS)z6Class decorator that fills in missing ordering methodscs(h|]}t|dtt|dur|qSN)robject).0opclsr&r' s(z!total_ordering..z6must define at least one ordering operation: < > <= >=)_convert ValueErrormaxrr)rProotsrootopnameopfuncr&rOr'rs rcsGfdddt}|S)z,Convert a cmp= function into a key= functioncsZeZdZdgZddZfddZfddZfdd Zfd d Zfd d Z dZ dS)zcmp_to_key..KobjcSs ||_dSrKrY)r.rYr&r&r'__init__s zcmp_to_key..K.__init__cs|j|jdkSNrrZr.r/mycmpr&r'r,zcmp_to_key..K.__lt__cs|j|jdkSr\rZr]r^r&r'rBr`zcmp_to_key..K.__gt__cs|j|jdkSr\rZr]r^r&r'__eq__r`zcmp_to_key..K.__eq__cs|j|jdkSr\rZr]r^r&r'r<r`zcmp_to_key..K.__le__cs|j|jdkSr\rZr]r^r&r'rGr`zcmp_to_key..K.__ge__N) rrr __slots__r[r,rBrar<rG__hash__r&r^r&r'Ks     rd)rL)r_rdr&r^r'rsr)rcCsRt|}|turzt|}Wntytddw|}|D]}|||}q|S)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)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|dr|j|}i|j|}|j}tt||}||_||_||_|S)Nz#the first argument must be callablerl) callablerihasattrrmrnrlsuperr __new__)rPrlrmrnr. __class__r&r'rss  zpartial.__new__cOs*i|j|}|jg|j|Ri|SrKrnrlrm)r.rmrnr&r&r'__call__+szpartial.__call__cCst|j}t|jg}|dd|jD|dd|jDt|jdkr6d|dd |dS|dd |dS) Ncss|]}t|VqdSrK)repr)rMxr&r&r' 3z#partial.__repr__..css"|] \}}|d|VqdS)=Nr&rMkvr&r&r'rz4s functoolsz functools.(, )) r+rrxrlextendrmrnitemsrjoin)r.qualnamermr&r&r'__repr__/s  zpartial.__repr__cCs*t||jf|j|j|jpd|jpdffSrK)r+rlrmrnrr.r&r&r' __reduce__9szpartial.__reduce__cCst|ts tdt|dkrtdt||\}}}}t|r9t|tr9|dur0t|tr9|dur=t|ts=tdt|}|durHi}n t|turRt|}|durXi}||_||_||_ ||_ dS)Nz(argument to __setstate__ must be a tuplezexpected 4 items in state, got zinvalid partial state) isinstancetuplerilenrpdictr+rrlrmrn)r.staterlrmkwds namespacer&r&r' __setstate__=s(     zpartial.__setstate__) rrrrrbrsrwrrrr __classcell__r&r&rtr'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. cOsht|st|dstd|t|tr)|j|_|j||_i|j||_dS||_||_||_dS)N__get__z${!r} is not callable or a descriptor) rprqriformatrr rlrmrn)r.rlrmrnr&r&r'r[cs   zpartialmethod.__init__cCsNdtt|j}ddd|jD}d}|j|jj|jj |j ||dS)Nrcss |] \}}d||VqdS)z{}={!r}N)rr}r&r&r'rzxsz)partialmethod.__repr__..z*{module}.{cls}({func}, {args}, {keywords}))modulerPrlrmrn) rmaprxrmrnrrrurrrl)r.rmrn format_stringr&r&r'rvs  zpartialmethod.__repr__csfdd}j|_|_|S)Ncs,ij|}j|gj|Ri|SrKrv) cls_or_selfrmrnrr&r'_methodsz3partialmethod._make_unbound_method.._method)__isabstractmethod___partialmethod)r.rr&rr'_make_unbound_methods z"partialmethod._make_unbound_methodNcCst|jdd}d}|dur4|||}||jur4t|g|jRi|j}z|j|_Wn ty3Ynw|dur@|||}|S)Nr) rrlr rmrn__self__rrr)r.rYrPgetresultnew_funcr&r&r'rs    zpartialmethod.__get__cCt|jddSNrFrrlrr&r&r'rz"partialmethod.__isabstractmethod__rK) rrrrr[rrrpropertyr classmethodr__class_getitem__r&r&r&r'r [s   r cCst|tr |j}t|ts|SrK)rr rlrlr&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<|||_dSrKr)r.tuphashr&r&r'r[s z_HashedSeq.__init__cCs|jSrKrrr&r&r'rcsz_HashedSeq.__hash__N)rrrrrbrr[rcr&r&r&r'rs   rc s|}|r||7}|D]} || 7}q |r7||fdd|D7}|r3||fdd|D7}t|S||dkrI|d|vrI|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|]}|VqdSrKr&rMrr+r&r'rzr{z_make_key..c3rrKr&rrr&r'rzr{r)rvaluesr) rmrtypedkwd_mark fasttypesrr+rkeyitemr&rr' _make_keys  rFcs~ttr dkr dn*tr.ttr.d}t|t}fdd|_t||Sdur6tdfdd}|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) rrc dSNrrr&r&rr&r' zlru_cache..Nz=Expected first argument to be an integer, a callable, or Nonecs(t|t}fdd|_t||S)Ncrrr&r&rr&r'rrz8lru_cache..decorating_function..)_lru_cache_wrapper _CacheInfocache_parametersr) user_functionr rr&r'decorating_functions z&lru_cache..decorating_function) rintrpboolrrrrri)rrrr rr&rr'rs   rcstt d\id djjt gddgdd< dkr4 fdd}n' durF fdd}n fdd} fdd } fd d }||_||_|S) N)rrrFcsd7|i|}|SNrr&)rmrr)rrr&r'r sz#_lru_cache_wrapper..wrappercsL||}|}|urd7|Sd7|i|}||<|Srr&)rmrrr)r cache_getrmake_keyrsentinelrrr&r'r 's  c s ||} C|}|dur@|\}}}}||<||< }||< <||< |<d7|WdS d7 Wdn1sNwY|i|} a|vrbnKr } || <|| <|  } } d < <| =| |<n+ }| ||g}||< <|< kWd|SWd|SWd|S1swY|Srr&) rmrrlinkZ link_prevZ link_nextZ_keyrZlastZoldrootZoldkeyZ oldresult)KEYNEXTPREVRESULTrr cache_lenfullrlockrrrrVrrr&r'r 6sX       $ $ $$cs8WdS1swYdS)zReport cache statisticsNr&r&)rrrrrrr&r' cache_infoos$z&_lru_cache_wrapper..cache_infocsRddgdd<ddWddS1s"wYdS)z$Clear the cache and cache statisticsNrF)clearr&)rrrrrrVr&r' cache_clearts "z'_lru_cache_wrapper..cache_clear)rLrr__len__rrr)rrrrr rrr&)rrrrrrrrrrrrrrrVrrrr'r s* *9 r)rcCstdd|S)z@Simple lightweight unbounded cache. Sometimes called "memoize".N)r)r)rr&r&r'rrrcCsg} dd|D}|s|S|D]}|d}|D]}||ddvr&d}nqnq|dur2td|||D] }|d|krD|d=q9q)zMerges MROs in *sequences* to a single MRO using the C3 algorithm. Adapted from https://www.python.org/download/releases/2.3/mro/. TcSsg|]}|r|qSr&r&rMsr&r&r' sz_c3_merge..rrNzInconsistent hierarchy) RuntimeErrorappend) sequencesrs1 candidates2seqr&r&r' _c3_merges,  rNc stt|jD]\}tdrt|j|}nqd}r"tngt|jd|}g}t|j|d}D]t|rRtfdd|jDsR|q:|D] qUfdd|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|VqdSrK) issubclass)rMb)baser&r'rzs  z_c3_mro..cg|]}t|dqSabcs_c3_mrorMrrr&r'rz_c3_mro..crrrrrr&r'rrcrrrrrr&r'rr) enumeratereversed __bases__rqrlistranyrremover) rPriboundaryexplicit_basesabstract_bases other_basesexplicit_c3_mrosabstract_c3_mros other_c3_mrosr&)rrr'rsH   rcstjfddfddDfddfddDtg}D]C}g}|D]}|vrLt|rL|fdd|jDq4|sU||q,|jtd d |D]}|D] }||vrm||qbq^q,t|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(|vot|dot|t ot|S)N__mro__)rqrrr)typ)basesrPr&r' is_relateds  z _compose_mro..is_relatedcsg|]}|r|qSr&r&rMn)rr&r'rrz _compose_mro..cs&D]}||kr||jvrdSqdS)NTF)r)rr/)typesr&r'is_strict_bases z$_compose_mro..is_strict_basecsg|]}|s|qSr&r&r )r r&r'rrcsg|]}|vr|qSr&r&r)type_setr&r'rrT)rreverser)setr__subclasses__rrsortrr)rPr mrorfoundsubsubclsr&)rrPrr r r r' _compose_mros2      rcCstt||}d}|D])}|dur.||vr,||jvr,||jvr,t||s,td||n||vr4|}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)rPregistryrmatchtr&r&r' _find_impls    rcsddl}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 sxdurt}|kr|z|}W|Sty;z|}Wnty3t|}Ynw||<Y|Sw)zgeneric_func.dispatch(cls) -> Runs the dispatch algorithm to return the best available implementation for the given *cls* registered on *generic_func*. N)rrKeyErrorr)rP current_tokenimpl) cache_tokendispatch_cacherr&r'dispatch2s"     z singledispatch..dispatchcSst|to t|t SrK)rr+rrOr&r&r'_is_valid_dispatch_typeIsz/singledispatch.._is_valid_dispatch_typecsr|durfddSn?|durtddtdi}|s,tdd}d d lm}tt||\}sOtd |d d|<dur_td r_t |S)zgeneric_func.register(cls, func) -> func Registers a new implementation for the given *cls* on a *generic_func*. Ncs |SrKr&)f)rPregisterr&r'rUrz2singledispatch..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) rirtypingr%rgrerrqrr)rPrlannr%argname)r"rr r$rrOr'r$Ls:   z singledispatch..registercs*|s td|dj|i|S)Nz( requires at least 1 positional argumentr)riru)rmkw)r!funcnamer&r'r tszsingledispatch..wrapperrzsingledispatch functionrK) r weakrefWeakKeyDictionaryrrLr$r!MappingProxyTyperr _clear_cacher)rlr r+r r&)r"rr!r r*r$rr'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. cCs4t|st|dst|dt||_||_dS)Nrz is not callable or a descriptor)rprqrir dispatcherrlr.rlr&r&r'r[s  zsingledispatchmethod.__init__NcCs|jj||dS)zgeneric_method.register(cls, func) -> func Registers a new implementation for the given *cls* on a *generic_method*. r)r/r$)r.rPmethodr&r&r'r$szsingledispatchmethod.registercs0fdd}j|_j|_t|j|S)Ncs(j|dj}||i|Sr\)r/r!rur)rmkwargsr1rPrYr.r&r'rsz-singledispatchmethod.__get__.._method)rr$rrl)r.rYrPrr&r3r'rs  zsingledispatchmethod.__get__cCrrrrr&r&r'rrz)singledispatchmethod.__isabstractmethod__rK) rrrrr[r$rrrr&r&r&r'r s   r c@s.eZdZddZddZdddZeeZdS) rcCs ||_d|_|j|_t|_dSrK)rlattrnamerrrr0r&r&r'r[s zcached_property.__init__cCs:|jdur ||_dS||jkrtd|jd|ddS)Nz?Cannot assign the same cached_property to two different names (z and z).)r4ri)r.ownernamer&r&r' __set_name__s   zcached_property.__set_name__Nc Cs|dur|S|jdurtdz|j}Wnty-dt|jd|jd}t|dw||jt}|tur|jB||jt}|turn| |}z|||j<Wn!tymdt|jd|jd}t|dwWd|SWd|S1swY|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 ) r4rirrr+rr _NOT_FOUNDrrl)r.instancer5rmsgvalr&r&r'rsL            zcached_property.__get__rK) rrrr[r7rrrrr&r&r&r'rs  r)rFrK)s                       A J  .t -*f(