Adopt more descriptive attribute names as suggested on python-dev.

This commit is contained in:
Raymond Hettinger 2010-09-04 22:46:06 +00:00
parent e9a4de51ab
commit 02566ec89f
5 changed files with 23 additions and 23 deletions

View file

@ -47,12 +47,12 @@ The :mod:`functools` module defines the following functions:
results, the positional and keyword arguments to the function must be results, the positional and keyword arguments to the function must be
hashable. hashable.
The wrapped function is instrumented with two attributes, :attr:`hits` The wrapped function is instrumented with two attributes, :attr:`cache_hits`
and :attr:`misses` which count the number of successful or unsuccessful and :attr:`cache_misses` which count the number of successful or unsuccessful
cache lookups. These statistics are helpful for tuning the *maxsize* cache lookups. These statistics are helpful for tuning the *maxsize*
parameter and for measuring the cache's effectiveness. parameter and for measuring the cache's effectiveness.
The wrapped function also has a :attr:`clear` attribute which can be The wrapped function also has a :attr:`cache_clear` attribute which can be
called (with no arguments) to clear the cache. called (with no arguments) to clear the cache.
The original underlying function is accessible through the The original underlying function is accessible through the

View file

@ -85,17 +85,17 @@ New, Improved, and Deprecated Modules
return c.fetchone()[0] return c.fetchone()[0]
To help with choosing an effective cache size, the wrapped function is To help with choosing an effective cache size, the wrapped function is
instrumented with two attributes *hits* and *misses*:: instrumented with two attributes *cache_hits* and *cache_misses*::
>>> for name in user_requests: >>> for name in user_requests:
... get_phone_number(name) ... get_phone_number(name)
>>> print(get_phone_number.hits, get_phone_number.misses) >>> print(get_phone_number.cache_hits, get_phone_number.cache_misses)
4805 980 4805 980
If the phonelist table gets updated, the outdated contents of the cache can be If the phonelist table gets updated, the outdated contents of the cache can be
cleared with:: cleared with::
>>> get_phone_number.clear() >>> get_phone_number.cache_clear()
(Contributed by Raymond Hettinger) (Contributed by Raymond Hettinger)

View file

@ -142,23 +142,23 @@ def lru_cache(maxsize=100):
with lock: with lock:
result = cache[key] result = cache[key]
cache_renew(key) # record recent use of this key cache_renew(key) # record recent use of this key
wrapper.hits += 1 wrapper.cache_hits += 1
except KeyError: except KeyError:
result = user_function(*args, **kwds) result = user_function(*args, **kwds)
with lock: with lock:
cache[key] = result # record recent use of this key cache[key] = result # record recent use of this key
wrapper.misses += 1 wrapper.cache_misses += 1
if len(cache) > maxsize: if len(cache) > maxsize:
cache_popitem(0) # purge least recently used cache entry cache_popitem(0) # purge least recently used cache entry
return result return result
def clear(): def cache_clear():
"""Clear the cache and cache statistics""" """Clear the cache and cache statistics"""
with lock: with lock:
cache.clear() cache.clear()
wrapper.hits = wrapper.misses = 0 wrapper.cache_hits = wrapper.cache_misses = 0
wrapper.hits = wrapper.misses = 0 wrapper.cache_hits = wrapper.cache_misses = 0
wrapper.clear = clear wrapper.cache_clear = cache_clear
return wrapper return wrapper
return decorating_function return decorating_function

View file

@ -207,8 +207,8 @@ def compile(pattern, flags=0):
def purge(): def purge():
"Clear the regular expression caches" "Clear the regular expression caches"
_compile_typed.clear() _compile_typed.cache_clear()
_compile_repl.clear() _compile_repl.cache_clear()
def template(pattern, flags=0): def template(pattern, flags=0):
"Compile a template pattern, returning a pattern object" "Compile a template pattern, returning a pattern object"

View file

@ -508,21 +508,21 @@ class TestLRU(unittest.TestCase):
actual = f(x, y) actual = f(x, y)
expected = orig(x, y) expected = orig(x, y)
self.assertEquals(actual, expected) self.assertEquals(actual, expected)
self.assert_(f.hits > f.misses) self.assert_(f.cache_hits > f.cache_misses)
self.assertEquals(f.hits + f.misses, 1000) self.assertEquals(f.cache_hits + f.cache_misses, 1000)
f.clear() # test clearing f.cache_clear() # test clearing
self.assertEqual(f.hits, 0) self.assertEqual(f.cache_hits, 0)
self.assertEqual(f.misses, 0) self.assertEqual(f.cache_misses, 0)
f(x, y) f(x, y)
self.assertEqual(f.hits, 0) self.assertEqual(f.cache_hits, 0)
self.assertEqual(f.misses, 1) self.assertEqual(f.cache_misses, 1)
# Test bypassing the cache # Test bypassing the cache
self.assertIs(f.__wrapped__, orig) self.assertIs(f.__wrapped__, orig)
f.__wrapped__(x, y) f.__wrapped__(x, y)
self.assertEqual(f.hits, 0) self.assertEqual(f.cache_hits, 0)
self.assertEqual(f.misses, 1) self.assertEqual(f.cache_misses, 1)
# test size zero (which means "never-cache") # test size zero (which means "never-cache")
@functools.lru_cache(0) @functools.lru_cache(0)