1import functools
2import types
3from typing import Callable, TypeVar
4
5
6# from jaraco.functools 3.3
7def 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 an
12 underscore-prefixed protected property on first call and returns that
13 subsequently.
14
15 >>> class MyClass:
16 ... calls = 0
17 ...
18 ... @method_cache
19 ... def method(self, value):
20 ... self.calls += 1
21 ... return value
22
23 >>> a = MyClass()
24 >>> a.method(3)
25 3
26 >>> for x in range(75):
27 ... res = a.method(x)
28 >>> a.calls
29 75
30
31 Note that the apparent behavior will be exactly like that of lru_cache
32 except that the cache is stored on each instance, so values in one
33 instance will not flush values from another, and when an instance is
34 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.calls
40 35
41 >>> a.method(0)
42 0
43 >>> a.calls
44 75
45
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 been
48 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 3
66
67 Caution - do not subsequently wrap the method with another decorator, such
68 as ``@property``, which changes the semantics of the function.
69
70 See also
71 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 method
78 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: None
85
86 return wrapper
87
88
89# From jaraco.functools 3.3
90def pass_none(func):
91 """
92 Wrap func so it's not called if its first param is None
93
94 >>> print_text = pass_none(print)
95 >>> print_text('text')
96 text
97 >>> 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 wrapper
106
107
108# From jaraco.functools 4.4
109def 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.4
121def passthrough(func: Callable[..., object]) -> Callable[[_T], _T]:
122 """
123 Wrap the function to always return the first parameter.
124
125 >>> passthrough(print)('3')
126 3
127 '3'
128 """
129
130 @functools.wraps(func)
131 def wrapper(first: _T, *args, **kwargs) -> _T:
132 func(first, *args, **kwargs)
133 return first
134
135 return wrapper # type: ignore[return-value]