_i: : d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl m
Z
mZ ede
def Z
d Zd Zd Z e j" fd e
d
e
e
ge
f de
fdZd
Zd Zd Zd Zd Z G d d Zd Zd d dfdZd Zd Zd Zd Zd ZddddZ y) N)CallableTypeVar CallableT.)boundc 4 d }t j || S )a;
Compose any number of unary functions into a single unary function.
>>> import textwrap
>>> expected = str.strip(textwrap.dedent(compose.__doc__))
>>> strip_and_dedent = compose(str.strip, textwrap.dedent)
>>> strip_and_dedent(compose.__doc__) == expected
True
Compose also allows the innermost function to take arbitrary arguments.
>>> round_three = lambda x: round(x, ndigits=3)
>>> f = compose(round_three, int.__truediv__)
>>> [f(3*x, x+1) for x in range(1,10)]
[1.5, 2.0, 2.25, 2.4, 2.5, 2.571, 2.625, 2.667, 2.7]
c fdS )Nc | i | S N )argskwargsf1f2s /builddir/build/BUILDROOT/alt-python312-setuptools-69.0.2-3.el8.x86_64/opt/alt/python312/lib/python3.12/site-packages/setuptools/_vendor/jaraco/functools.pyz.compose..compose_two..$ s r"d*=f*='> r )r r s ``r compose_twozcompose..compose_two# s >>r ) functoolsreduce)funcsr s r composer s $? K//r c fd}|S )z
Return a function that will call a named method on the
target object with optional positional and keyword
arguments.
>>> lower = method_caller('lower')
>>> lower('MyString')
'mystring'
c , t | } |i S r
)getattr)targetfuncr r
method_names r call_methodz"method_caller..call_method4 s v{+T$V$$r r )r r r
r s ``` r
method_callerr ) s % r c Z t j fd fd_ S )ad
Decorate func so it's only ever called the first time.
This decorator can ensure that an expensive or non-idempotent function
will not be expensive on subsequent calls and is idempotent.
>>> add_three = once(lambda a: a+3)
>>> add_three(3)
6
>>> add_three(9)
6
>>> add_three('12')
6
To reset the stored value, simply clear the property ``saved_result``.
>>> del add_three.saved_result
>>> add_three(9)
12
>>> add_three(8)
12
Or invoke 'reset()' on it.
>>> add_three.reset()
>>> add_three(-3)
0
>>> add_three(0)
0
c N t d s
| i |_ j S Nsaved_result)hasattrr# )r r
r wrappers r r% zonce..wrapper[ s+ w/#'#8#8G ###r c 8 t j d S r" )vars__delitem__)r% s r r zonce..a s DM55nEr )r wrapsresetr r% s `@r oncer, ; s0 @ __T$ $
FGMNr method
cache_wrapperreturnc t dt dt dt dt f fd}d |_ t xs |S )aV
Wrap lru_cache to support storing the cache data in the object instances.
Abstracts the common paradigm where the method explicitly saves an
underscore-prefixed protected property on first call and returns that
subsequently.
>>> class MyClass:
... calls = 0
...
... @method_cache
... def method(self, value):
... self.calls += 1
... return value
>>> a = MyClass()
>>> a.method(3)
3
>>> for x in range(75):
... res = a.method(x)
>>> a.calls
75
Note that the apparent behavior will be exactly like that of lru_cache
except that the cache is stored on each instance, so values in one
instance will not flush values from another, and when an instance is
deleted, so are the cached values for that instance.
>>> b = MyClass()
>>> for x in range(35):
... res = b.method(x)
>>> b.calls
35
>>> a.method(0)
0
>>> a.calls
75
Note that if method had been decorated with ``functools.lru_cache()``,
a.calls would have been 76 (due to the cached value of 0 having been
flushed by the 'b' instance).
Clear the cache with ``.cache_clear()``
>>> a.method.cache_clear()
Same for a method that hasn't yet been called.
>>> c = MyClass()
>>> c.method.cache_clear()
Another cache wrapper may be supplied:
>>> cache = functools.lru_cache(maxsize=2)
>>> MyClass.method2 = method_cache(lambda self: 3, cache_wrapper=cache)
>>> a = MyClass()
>>> a.method2()
3
Caution - do not subsequently wrap the method with another decorator, such
as ``@property``, which changes the semantics of the function.
See also
http://code.activestate.com/recipes/577452-a-memoize-decorator-for-instance-methods/
for another implementation and additional justification.
selfr r
r/ c ~ t j | } | }t | j | ||i |S r
)types
MethodTypesetattr__name__)r1 r r
bound_method
cached_methodr. r- s r r% zmethod_cache..wrapper sD "'"2"2D#
&l3
foo}5d-f--r c y r
r r r r r zmethod_cache..