codekingpro/portable-devtools
115k
1import functools2import types3from typing import Callable, TypeVar4 5 6# from jaraco.functools 3.37def method_cache(method, cache_wrapper=None):8 """9 Wrap lru_cache to support storing the cache data in the object instances.10 11 Abstracts the common paradigm where the method explicitly saves an12 underscore-prefixed protected property on first call and returns that13 subsequently.14 15 >>> class MyClass:16 ... calls = 017 ...18 ... @method_cache19 ... def method(self, value):20 ... self.calls += 121 ... return value22 23 >>> a = MyClass()24 >>> a.method(3)25 326 >>> for x in range(75):27 ... res = a.method(x)28 >>> a.calls29 7530 31 Note that the apparent behavior will be exactly like that of lru_cache32 except that the cache is stored on each instance, so values in one33 instance will not flush values from another, and when an instance is34 deleted, so are the cached values for that instance.35 36 >>> b = MyClass()37 >>> for x in range(35):38 ... res = b.method(x)39 >>> b.calls40 3541 >>> a.method(0)42 043 >>> a.calls44 7545 46 Note that if method had been decorated with ``functools.lru_cache()``,47 a.calls would have been 76 (due to the cached value of 0 having been48 flushed by the 'b' instance).49 50 Clear the cache with ``.cache_clear()``51 52 >>> a.method.cache_clear()53 54 Same for a method that hasn't yet been called.55 56 >>> c = MyClass()57 >>> c.method.cache_clear()58 59 Another cache wrapper may be supplied:60 61 >>> cache = functools.lru_cache(maxsize=2)62 >>> MyClass.method2 = method_cache(lambda self: 3, cache_wrapper=cache)63 >>> a = MyClass()64 >>> a.method2()65 366 67 Caution - do not subsequently wrap the method with another decorator, such68 as ``@property``, which changes the semantics of the function.69 70 See also71 http://code.activestate.com/recipes/577452-a-memoize-decorator-for-instance-methods/72 for another implementation and additional justification.73 """74 cache_wrapper = cache_wrapper or functools.lru_cache()75 76 def wrapper(self, *args, **kwargs):77 # it's the first call, replace the method with a cached, bound method78 bound_method = types.MethodType(method, self)79 cached_method = cache_wrapper(bound_method)80 setattr(self, method.__name__, cached_method)81 return cached_method(*args, **kwargs)82 83 # Support cache clear even before cache has been created.84 wrapper.cache_clear = lambda: None85 86 return wrapper87 88 89# From jaraco.functools 3.390def pass_none(func):91 """92 Wrap func so it's not called if its first param is None93 94 >>> print_text = pass_none(print)95 >>> print_text('text')96 text97 >>> print_text(None)98 """99 100 @functools.wraps(func)101 def wrapper(param, *args, **kwargs):102 if param is not None:103 return func(param, *args, **kwargs)104 105 return wrapper106 107 108# From jaraco.functools 4.4109def noop(*args, **kwargs):110 """111 A no-operation function that does nothing.112 113 >>> noop(1, 2, three=3)114 """115 116 117_T = TypeVar('_T')118 119 120# From jaraco.functools 4.4121def passthrough(func: Callable[..., object]) -> Callable[[_T], _T]:122 """123 Wrap the function to always return the first parameter.124 125 >>> passthrough(print)('3')126 3127 '3'128 """129 130 @functools.wraps(func)131 def wrapper(first: _T, *args, **kwargs) -> _T:132 func(first, *args, **kwargs)133 return first134 135 return wrapper # type: ignore[return-value]136 