diff options
author | Raymond Hettinger <python@rcn.com> | 2012-03-17 07:24:09 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2012-03-17 07:24:09 (GMT) |
commit | bc8e81dcc32b42164fe158f1daf91ac93426efbf (patch) | |
tree | a0279d4feb3ef78d223bce97c719596ade3c9806 /Lib/functools.py | |
parent | dce583e0bdb63b7b98b748b4ad12d1fbdb22462b (diff) | |
download | cpython-bc8e81dcc32b42164fe158f1daf91ac93426efbf.zip cpython-bc8e81dcc32b42164fe158f1daf91ac93426efbf.tar.gz cpython-bc8e81dcc32b42164fe158f1daf91ac93426efbf.tar.bz2 |
Section-off the source for better readability.
Diffstat (limited to 'Lib/functools.py')
-rw-r--r-- | Lib/functools.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index dd653f4..f4c8271 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -18,6 +18,11 @@ try: except: from _dummy_thread import allocate_lock as Lock + +################################################################################ +### update_wrapper() and wraps() decorator +################################################################################ + # update_wrapper() and wraps() are tools to help write # wrapper functions that can handle naive introspection @@ -66,6 +71,11 @@ def wraps(wrapped, return partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated) + +################################################################################ +### total_ordering class decorator +################################################################################ + def total_ordering(cls): """Class decorator that fills in missing ordering methods""" convert = { @@ -94,6 +104,11 @@ def total_ordering(cls): setattr(cls, opname, opfunc) return cls + +################################################################################ +### cmp_to_key() function converter +################################################################################ + def cmp_to_key(mycmp): """Convert a cmp= function into a key= function""" class K(object): @@ -120,6 +135,11 @@ try: except ImportError: pass + +################################################################################ +### LRU Cache function decorator +################################################################################ + _CacheInfo = namedtuple("CacheInfo", ["hits", "misses", "maxsize", "currsize"]) def lru_cache(maxsize=100, typed=False): @@ -170,6 +190,7 @@ def lru_cache(maxsize=100, typed=False): return key if maxsize is None: + @wraps(user_function) def wrapper(*args, **kwds): # simple caching without ordering or size limit @@ -183,7 +204,9 @@ def lru_cache(maxsize=100, typed=False): cache[key] = result misses += 1 return result + else: + @wraps(user_function) def wrapper(*args, **kwds): # size limited caching that tracks accesses by recency |