Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1 | """functools.py - Tools for working with functions and callable objects |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2 | """ |
| 3 | # Python module wrapper for _functools C module |
| 4 | # to allow utilities written in Python to be added |
| 5 | # to the functools module. |
| 6 | # Written by Nick Coghlan <ncoghlan at gmail.com> |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 7 | # and Raymond Hettinger <python at rcn.com> |
| 8 | # Copyright (C) 2006-2010 Python Software Foundation. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 9 | # See C source code for _functools credits/copyright |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 10 | |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 11 | __all__ = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', |
Benjamin Peterson | 1017ae5 | 2010-09-10 23:35:52 +0000 | [diff] [blame] | 12 | 'total_ordering', 'cmp_to_key', 'lru_cache', 'reduce', 'partial'] |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 14 | from _functools import partial, reduce |
Raymond Hettinger | ec0e910 | 2012-03-16 01:16:31 -0700 | [diff] [blame] | 15 | from collections import namedtuple |
Raymond Hettinger | cbe8813 | 2010-08-14 22:22:10 +0000 | [diff] [blame] | 16 | try: |
| 17 | from _thread import allocate_lock as Lock |
| 18 | except: |
| 19 | from _dummy_thread import allocate_lock as Lock |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 20 | |
Raymond Hettinger | bc8e81d | 2012-03-17 00:24:09 -0700 | [diff] [blame] | 21 | |
| 22 | ################################################################################ |
| 23 | ### update_wrapper() and wraps() decorator |
| 24 | ################################################################################ |
| 25 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 26 | # update_wrapper() and wraps() are tools to help write |
| 27 | # wrapper functions that can handle naive introspection |
| 28 | |
Meador Inge | ff7f64c | 2011-12-11 22:37:31 -0600 | [diff] [blame] | 29 | WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__', |
| 30 | '__annotations__') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 31 | WRAPPER_UPDATES = ('__dict__',) |
| 32 | def update_wrapper(wrapper, |
| 33 | wrapped, |
| 34 | assigned = WRAPPER_ASSIGNMENTS, |
| 35 | updated = WRAPPER_UPDATES): |
| 36 | """Update a wrapper function to look like the wrapped function |
| 37 | |
| 38 | wrapper is the function to be updated |
| 39 | wrapped is the original function |
| 40 | assigned is a tuple naming the attributes assigned directly |
| 41 | from the wrapped function to the wrapper function (defaults to |
| 42 | functools.WRAPPER_ASSIGNMENTS) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 43 | updated is a tuple naming the attributes of the wrapper that |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 44 | are updated with the corresponding attribute from the wrapped |
| 45 | function (defaults to functools.WRAPPER_UPDATES) |
| 46 | """ |
Nick Coghlan | 9887683 | 2010-08-17 06:17:18 +0000 | [diff] [blame] | 47 | wrapper.__wrapped__ = wrapped |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 48 | for attr in assigned: |
Nick Coghlan | 9887683 | 2010-08-17 06:17:18 +0000 | [diff] [blame] | 49 | try: |
| 50 | value = getattr(wrapped, attr) |
| 51 | except AttributeError: |
| 52 | pass |
| 53 | else: |
| 54 | setattr(wrapper, attr, value) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 55 | for attr in updated: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | getattr(wrapper, attr).update(getattr(wrapped, attr, {})) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 57 | # Return the wrapper so this can be used as a decorator via partial() |
| 58 | return wrapper |
| 59 | |
| 60 | def wraps(wrapped, |
| 61 | assigned = WRAPPER_ASSIGNMENTS, |
| 62 | updated = WRAPPER_UPDATES): |
| 63 | """Decorator factory to apply update_wrapper() to a wrapper function |
| 64 | |
| 65 | Returns a decorator that invokes update_wrapper() with the decorated |
| 66 | function as the wrapper argument and the arguments to wraps() as the |
| 67 | remaining arguments. Default arguments are as for update_wrapper(). |
| 68 | This is a convenience function to simplify applying partial() to |
| 69 | update_wrapper(). |
| 70 | """ |
| 71 | return partial(update_wrapper, wrapped=wrapped, |
| 72 | assigned=assigned, updated=updated) |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 73 | |
Raymond Hettinger | bc8e81d | 2012-03-17 00:24:09 -0700 | [diff] [blame] | 74 | |
| 75 | ################################################################################ |
| 76 | ### total_ordering class decorator |
| 77 | ################################################################################ |
| 78 | |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 79 | def total_ordering(cls): |
Georg Brandl | e5a2673 | 2010-05-19 21:06:36 +0000 | [diff] [blame] | 80 | """Class decorator that fills in missing ordering methods""" |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 81 | convert = { |
Raymond Hettinger | 23f9fc3 | 2011-01-08 07:01:56 +0000 | [diff] [blame] | 82 | '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)), |
| 83 | ('__le__', lambda self, other: self < other or self == other), |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 84 | ('__ge__', lambda self, other: not self < other)], |
Raymond Hettinger | 23f9fc3 | 2011-01-08 07:01:56 +0000 | [diff] [blame] | 85 | '__le__': [('__ge__', lambda self, other: not self <= other or self == other), |
| 86 | ('__lt__', lambda self, other: self <= other and not self == other), |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 87 | ('__gt__', lambda self, other: not self <= other)], |
Raymond Hettinger | 23f9fc3 | 2011-01-08 07:01:56 +0000 | [diff] [blame] | 88 | '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)), |
| 89 | ('__ge__', lambda self, other: self > other or self == other), |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 90 | ('__le__', lambda self, other: not self > other)], |
Raymond Hettinger | 23f9fc3 | 2011-01-08 07:01:56 +0000 | [diff] [blame] | 91 | '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other), |
| 92 | ('__gt__', lambda self, other: self >= other and not self == other), |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 93 | ('__lt__', lambda self, other: not self >= other)] |
| 94 | } |
Raymond Hettinger | 3255c63 | 2010-09-16 00:31:21 +0000 | [diff] [blame] | 95 | # Find user-defined comparisons (not those inherited from object). |
Raymond Hettinger | 1006bd4 | 2010-09-14 22:55:13 +0000 | [diff] [blame] | 96 | roots = [op for op in convert if getattr(cls, op, None) is not getattr(object, op, None)] |
Raymond Hettinger | 56de7e2 | 2010-04-10 16:59:03 +0000 | [diff] [blame] | 97 | if not roots: |
| 98 | raise ValueError('must define at least one ordering operation: < > <= >=') |
| 99 | root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 100 | for opname, opfunc in convert[root]: |
| 101 | if opname not in roots: |
| 102 | opfunc.__name__ = opname |
| 103 | opfunc.__doc__ = getattr(int, opname).__doc__ |
| 104 | setattr(cls, opname, opfunc) |
| 105 | return cls |
| 106 | |
Raymond Hettinger | bc8e81d | 2012-03-17 00:24:09 -0700 | [diff] [blame] | 107 | |
| 108 | ################################################################################ |
| 109 | ### cmp_to_key() function converter |
| 110 | ################################################################################ |
| 111 | |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 112 | def cmp_to_key(mycmp): |
Georg Brandl | e5a2673 | 2010-05-19 21:06:36 +0000 | [diff] [blame] | 113 | """Convert a cmp= function into a key= function""" |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 114 | class K(object): |
Raymond Hettinger | a0d1d96 | 2011-03-21 17:50:28 -0700 | [diff] [blame] | 115 | __slots__ = ['obj'] |
Raymond Hettinger | 7ab9e22 | 2011-04-05 02:33:54 -0700 | [diff] [blame] | 116 | def __init__(self, obj): |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 117 | self.obj = obj |
| 118 | def __lt__(self, other): |
| 119 | return mycmp(self.obj, other.obj) < 0 |
| 120 | def __gt__(self, other): |
| 121 | return mycmp(self.obj, other.obj) > 0 |
| 122 | def __eq__(self, other): |
| 123 | return mycmp(self.obj, other.obj) == 0 |
| 124 | def __le__(self, other): |
| 125 | return mycmp(self.obj, other.obj) <= 0 |
| 126 | def __ge__(self, other): |
| 127 | return mycmp(self.obj, other.obj) >= 0 |
| 128 | def __ne__(self, other): |
| 129 | return mycmp(self.obj, other.obj) != 0 |
Raymond Hettinger | 003be52 | 2011-05-03 11:01:32 -0700 | [diff] [blame] | 130 | __hash__ = None |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 131 | return K |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 132 | |
Raymond Hettinger | 7ab9e22 | 2011-04-05 02:33:54 -0700 | [diff] [blame] | 133 | try: |
| 134 | from _functools import cmp_to_key |
| 135 | except ImportError: |
| 136 | pass |
| 137 | |
Raymond Hettinger | bc8e81d | 2012-03-17 00:24:09 -0700 | [diff] [blame] | 138 | |
| 139 | ################################################################################ |
| 140 | ### LRU Cache function decorator |
| 141 | ################################################################################ |
| 142 | |
Raymond Hettinger | dce583e | 2012-03-16 22:12:20 -0700 | [diff] [blame] | 143 | _CacheInfo = namedtuple("CacheInfo", ["hits", "misses", "maxsize", "currsize"]) |
Nick Coghlan | 234515a | 2010-11-30 06:19:46 +0000 | [diff] [blame] | 144 | |
Raymond Hettinger | cd9fdfd | 2011-10-20 08:57:45 -0700 | [diff] [blame] | 145 | def lru_cache(maxsize=100, typed=False): |
Benjamin Peterson | 1f594ad | 2010-08-08 13:17:07 +0000 | [diff] [blame] | 146 | """Least-recently-used cache decorator. |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 147 | |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 148 | If *maxsize* is set to None, the LRU features are disabled and the cache |
| 149 | can grow without bound. |
| 150 | |
Raymond Hettinger | cd9fdfd | 2011-10-20 08:57:45 -0700 | [diff] [blame] | 151 | If *typed* is True, arguments of different types will be cached separately. |
| 152 | For example, f(3.0) and f(3) will be treated as distinct calls with |
| 153 | distinct results. |
| 154 | |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 155 | Arguments to the cached function must be hashable. |
Raymond Hettinger | 5fa40c0 | 2010-11-25 08:11:57 +0000 | [diff] [blame] | 156 | |
Raymond Hettinger | 7f7a5a7 | 2012-03-30 21:50:40 -0700 | [diff] [blame] | 157 | View the cache statistics named tuple (hits, misses, maxsize, currsize) |
| 158 | with f.cache_info(). Clear the cache and statistics with f.cache_clear(). |
Raymond Hettinger | 00f2f97 | 2010-12-01 00:47:56 +0000 | [diff] [blame] | 159 | Access the underlying function with f.__wrapped__. |
Raymond Hettinger | 5fa40c0 | 2010-11-25 08:11:57 +0000 | [diff] [blame] | 160 | |
| 161 | See: http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 162 | |
Benjamin Peterson | 1f594ad | 2010-08-08 13:17:07 +0000 | [diff] [blame] | 163 | """ |
Raymond Hettinger | 1ff50df | 2012-03-30 13:15:48 -0700 | [diff] [blame] | 164 | |
Raymond Hettinger | 5fa40c0 | 2010-11-25 08:11:57 +0000 | [diff] [blame] | 165 | # Users should only access the lru_cache through its public API: |
Raymond Hettinger | 5e20bab | 2010-11-30 07:13:04 +0000 | [diff] [blame] | 166 | # cache_info, cache_clear, and f.__wrapped__ |
Raymond Hettinger | 5fa40c0 | 2010-11-25 08:11:57 +0000 | [diff] [blame] | 167 | # The internals of the lru_cache are encapsulated for thread safety and |
| 168 | # to allow the implementation to change (including a possible C version). |
| 169 | |
Raymond Hettinger | 6e8c817 | 2012-03-16 16:53:05 -0700 | [diff] [blame] | 170 | def decorating_function(user_function): |
Raymond Hettinger | 5fa40c0 | 2010-11-25 08:11:57 +0000 | [diff] [blame] | 171 | |
Raymond Hettinger | 7f7a5a7 | 2012-03-30 21:50:40 -0700 | [diff] [blame] | 172 | cache = {} |
Nick Coghlan | 234515a | 2010-11-30 06:19:46 +0000 | [diff] [blame] | 173 | hits = misses = 0 |
Raymond Hettinger | 7f7a5a7 | 2012-03-30 21:50:40 -0700 | [diff] [blame] | 174 | kwd_mark = (object(),) # separate positional and keyword args |
Raymond Hettinger | c689785 | 2012-03-31 02:19:06 -0700 | [diff] [blame] | 175 | cache_get = cache.get # bound method to lookup a key or return None |
Raymond Hettinger | 7f7a5a7 | 2012-03-30 21:50:40 -0700 | [diff] [blame] | 176 | sentinel = object() # unique object used with cache_get |
| 177 | _len = len # localize the global len() function |
| 178 | lock = Lock() # because linkedlist updates aren't threadsafe |
| 179 | root = [] # root of the circular doubly linked list |
| 180 | root[:] = [root, root, None, None] # initialize by pointing to self |
| 181 | PREV, NEXT, KEY, RESULT = 0, 1, 2, 3 # names for the link fields |
Raymond Hettinger | 6e8c817 | 2012-03-16 16:53:05 -0700 | [diff] [blame] | 182 | |
| 183 | def make_key(args, kwds, typed, tuple=tuple, sorted=sorted, type=type): |
Raymond Hettinger | 7f7a5a7 | 2012-03-30 21:50:40 -0700 | [diff] [blame] | 184 | # build a cache key from positional and keyword args |
Raymond Hettinger | 6e8c817 | 2012-03-16 16:53:05 -0700 | [diff] [blame] | 185 | key = args |
| 186 | if kwds: |
| 187 | sorted_items = tuple(sorted(kwds.items())) |
Raymond Hettinger | 678e7f3 | 2012-04-29 12:28:02 -0700 | [diff] [blame^] | 188 | key += kwd_mark |
| 189 | key += tuple(k for k, v in sorted_items) |
| 190 | key += tuple(v for k, v in sorted_items) |
Raymond Hettinger | 6e8c817 | 2012-03-16 16:53:05 -0700 | [diff] [blame] | 191 | if typed: |
| 192 | key += tuple(type(v) for v in args) |
| 193 | if kwds: |
| 194 | key += tuple(type(v) for k, v in sorted_items) |
| 195 | return key |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 196 | |
Raymond Hettinger | 7e0c581 | 2012-03-17 15:10:24 -0700 | [diff] [blame] | 197 | if maxsize == 0: |
| 198 | |
Raymond Hettinger | 7e0c581 | 2012-03-17 15:10:24 -0700 | [diff] [blame] | 199 | def wrapper(*args, **kwds): |
Raymond Hettinger | 7f7a5a7 | 2012-03-30 21:50:40 -0700 | [diff] [blame] | 200 | # no caching, just a statistics update after a successful call |
Raymond Hettinger | 7e0c581 | 2012-03-17 15:10:24 -0700 | [diff] [blame] | 201 | nonlocal misses |
Raymond Hettinger | 7dabfed | 2012-03-17 15:11:09 -0700 | [diff] [blame] | 202 | result = user_function(*args, **kwds) |
Raymond Hettinger | 7e0c581 | 2012-03-17 15:10:24 -0700 | [diff] [blame] | 203 | misses += 1 |
| 204 | return result |
| 205 | |
| 206 | elif maxsize is None: |
Raymond Hettinger | bc8e81d | 2012-03-17 00:24:09 -0700 | [diff] [blame] | 207 | |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 208 | def wrapper(*args, **kwds): |
Raymond Hettinger | ec0e910 | 2012-03-16 01:16:31 -0700 | [diff] [blame] | 209 | # simple caching without ordering or size limit |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 210 | nonlocal hits, misses |
Raymond Hettinger | 6e8c817 | 2012-03-16 16:53:05 -0700 | [diff] [blame] | 211 | key = make_key(args, kwds, typed) if kwds or typed else args |
Raymond Hettinger | 7f7a5a7 | 2012-03-30 21:50:40 -0700 | [diff] [blame] | 212 | result = cache_get(key, sentinel) |
| 213 | if result is not sentinel: |
Nick Coghlan | 234515a | 2010-11-30 06:19:46 +0000 | [diff] [blame] | 214 | hits += 1 |
Raymond Hettinger | 4b779b3 | 2011-10-15 23:50:42 -0700 | [diff] [blame] | 215 | return result |
Raymond Hettinger | 4b779b3 | 2011-10-15 23:50:42 -0700 | [diff] [blame] | 216 | result = user_function(*args, **kwds) |
| 217 | cache[key] = result |
| 218 | misses += 1 |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 219 | return result |
Raymond Hettinger | bc8e81d | 2012-03-17 00:24:09 -0700 | [diff] [blame] | 220 | |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 221 | else: |
Raymond Hettinger | bc8e81d | 2012-03-17 00:24:09 -0700 | [diff] [blame] | 222 | |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 223 | def wrapper(*args, **kwds): |
Raymond Hettinger | ec0e910 | 2012-03-16 01:16:31 -0700 | [diff] [blame] | 224 | # size limited caching that tracks accesses by recency |
Raymond Hettinger | 41eb79a | 2012-03-30 19:15:18 -0700 | [diff] [blame] | 225 | nonlocal root, hits, misses |
Raymond Hettinger | 6e8c817 | 2012-03-16 16:53:05 -0700 | [diff] [blame] | 226 | key = make_key(args, kwds, typed) if kwds or typed else args |
Raymond Hettinger | 4b779b3 | 2011-10-15 23:50:42 -0700 | [diff] [blame] | 227 | with lock: |
Raymond Hettinger | ec0e910 | 2012-03-16 01:16:31 -0700 | [diff] [blame] | 228 | link = cache_get(key) |
| 229 | if link is not None: |
Raymond Hettinger | 7f7a5a7 | 2012-03-30 21:50:40 -0700 | [diff] [blame] | 230 | # move the link to the front of the circular queue |
Raymond Hettinger | ec0e910 | 2012-03-16 01:16:31 -0700 | [diff] [blame] | 231 | link_prev, link_next, key, result = link |
| 232 | link_prev[NEXT] = link_next |
| 233 | link_next[PREV] = link_prev |
| 234 | last = root[PREV] |
| 235 | last[NEXT] = root[PREV] = link |
| 236 | link[PREV] = last |
| 237 | link[NEXT] = root |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 238 | hits += 1 |
Raymond Hettinger | 4b779b3 | 2011-10-15 23:50:42 -0700 | [diff] [blame] | 239 | return result |
Raymond Hettinger | 4b779b3 | 2011-10-15 23:50:42 -0700 | [diff] [blame] | 240 | result = user_function(*args, **kwds) |
| 241 | with lock: |
Raymond Hettinger | 41eb79a | 2012-03-30 19:15:18 -0700 | [diff] [blame] | 242 | if _len(cache) < maxsize: |
Raymond Hettinger | 7f7a5a7 | 2012-03-30 21:50:40 -0700 | [diff] [blame] | 243 | # put result in a new link at the front of the queue |
Raymond Hettinger | 41eb79a | 2012-03-30 19:15:18 -0700 | [diff] [blame] | 244 | last = root[PREV] |
| 245 | link = [last, root, key, result] |
| 246 | cache[key] = last[NEXT] = root[PREV] = link |
| 247 | else: |
| 248 | # use root to store the new key and result |
| 249 | root[KEY] = key |
| 250 | root[RESULT] = result |
| 251 | cache[key] = root |
| 252 | # empty the oldest link and make it the new root |
| 253 | root = root[NEXT] |
| 254 | del cache[root[KEY]] |
Raymond Hettinger | c689785 | 2012-03-31 02:19:06 -0700 | [diff] [blame] | 255 | root[KEY] = root[RESULT] = None |
Raymond Hettinger | ec0e910 | 2012-03-16 01:16:31 -0700 | [diff] [blame] | 256 | misses += 1 |
Raymond Hettinger | c79fb0e | 2010-12-01 03:45:41 +0000 | [diff] [blame] | 257 | return result |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 258 | |
Nick Coghlan | 234515a | 2010-11-30 06:19:46 +0000 | [diff] [blame] | 259 | def cache_info(): |
Raymond Hettinger | 5e20bab | 2010-11-30 07:13:04 +0000 | [diff] [blame] | 260 | """Report cache statistics""" |
Nick Coghlan | 234515a | 2010-11-30 06:19:46 +0000 | [diff] [blame] | 261 | with lock: |
Raymond Hettinger | 7496b41 | 2010-11-30 19:15:45 +0000 | [diff] [blame] | 262 | return _CacheInfo(hits, misses, maxsize, len(cache)) |
Nick Coghlan | 234515a | 2010-11-30 06:19:46 +0000 | [diff] [blame] | 263 | |
Raymond Hettinger | 02566ec | 2010-09-04 22:46:06 +0000 | [diff] [blame] | 264 | def cache_clear(): |
Benjamin Peterson | 1f594ad | 2010-08-08 13:17:07 +0000 | [diff] [blame] | 265 | """Clear the cache and cache statistics""" |
Raymond Hettinger | 4f5139b | 2012-03-16 17:08:37 -0700 | [diff] [blame] | 266 | nonlocal hits, misses |
Raymond Hettinger | cbe8813 | 2010-08-14 22:22:10 +0000 | [diff] [blame] | 267 | with lock: |
| 268 | cache.clear() |
Benjamin Peterson | 954cf57 | 2012-03-16 18:22:26 -0500 | [diff] [blame] | 269 | root[:] = [root, root, None, None] |
Nick Coghlan | 234515a | 2010-11-30 06:19:46 +0000 | [diff] [blame] | 270 | hits = misses = 0 |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 271 | |
Nick Coghlan | 234515a | 2010-11-30 06:19:46 +0000 | [diff] [blame] | 272 | wrapper.cache_info = cache_info |
Raymond Hettinger | 02566ec | 2010-09-04 22:46:06 +0000 | [diff] [blame] | 273 | wrapper.cache_clear = cache_clear |
Raymond Hettinger | 1ff50df | 2012-03-30 13:15:48 -0700 | [diff] [blame] | 274 | return update_wrapper(wrapper, user_function) |
Raymond Hettinger | 5fa40c0 | 2010-11-25 08:11:57 +0000 | [diff] [blame] | 275 | |
Georg Brandl | 2e7346a | 2010-07-31 18:09:23 +0000 | [diff] [blame] | 276 | return decorating_function |