From 02566ec89f90e5696eef062414c69a7c7ee991df Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 4 Sep 2010 22:46:06 +0000 Subject: Adopt more descriptive attribute names as suggested on python-dev. --- Doc/library/functools.rst | 6 +++--- Doc/whatsnew/3.2.rst | 6 +++--- Lib/functools.py | 12 ++++++------ Lib/re.py | 4 ++-- Lib/test/test_functools.py | 18 +++++++++--------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index 00ab624..a6e2382 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -47,12 +47,12 @@ The :mod:`functools` module defines the following functions: results, the positional and keyword arguments to the function must be hashable. - The wrapped function is instrumented with two attributes, :attr:`hits` - and :attr:`misses` which count the number of successful or unsuccessful + The wrapped function is instrumented with two attributes, :attr:`cache_hits` + and :attr:`cache_misses` which count the number of successful or unsuccessful cache lookups. These statistics are helpful for tuning the *maxsize* 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. The original underlying function is accessible through the diff --git a/Doc/whatsnew/3.2.rst b/Doc/whatsnew/3.2.rst index e3cf864..9181b4f 100644 --- a/Doc/whatsnew/3.2.rst +++ b/Doc/whatsnew/3.2.rst @@ -85,17 +85,17 @@ New, Improved, and Deprecated Modules return c.fetchone()[0] 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: ... 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 If the phonelist table gets updated, the outdated contents of the cache can be cleared with:: - >>> get_phone_number.clear() + >>> get_phone_number.cache_clear() (Contributed by Raymond Hettinger) diff --git a/Lib/functools.py b/Lib/functools.py index 658081f..1effc08 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -142,23 +142,23 @@ def lru_cache(maxsize=100): with lock: result = cache[key] cache_renew(key) # record recent use of this key - wrapper.hits += 1 + wrapper.cache_hits += 1 except KeyError: result = user_function(*args, **kwds) with lock: cache[key] = result # record recent use of this key - wrapper.misses += 1 + wrapper.cache_misses += 1 if len(cache) > maxsize: cache_popitem(0) # purge least recently used cache entry return result - def clear(): + def cache_clear(): """Clear the cache and cache statistics""" with lock: cache.clear() - wrapper.hits = wrapper.misses = 0 + wrapper.cache_hits = wrapper.cache_misses = 0 - wrapper.hits = wrapper.misses = 0 - wrapper.clear = clear + wrapper.cache_hits = wrapper.cache_misses = 0 + wrapper.cache_clear = cache_clear return wrapper return decorating_function diff --git a/Lib/re.py b/Lib/re.py index 1dcf4a7..92e4e4c 100644 --- a/Lib/re.py +++ b/Lib/re.py @@ -207,8 +207,8 @@ def compile(pattern, flags=0): def purge(): "Clear the regular expression caches" - _compile_typed.clear() - _compile_repl.clear() + _compile_typed.cache_clear() + _compile_repl.cache_clear() def template(pattern, flags=0): "Compile a template pattern, returning a pattern object" diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 41a98a2..82ad889 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -508,21 +508,21 @@ class TestLRU(unittest.TestCase): actual = f(x, y) expected = orig(x, y) self.assertEquals(actual, expected) - self.assert_(f.hits > f.misses) - self.assertEquals(f.hits + f.misses, 1000) + self.assert_(f.cache_hits > f.cache_misses) + self.assertEquals(f.cache_hits + f.cache_misses, 1000) - f.clear() # test clearing - self.assertEqual(f.hits, 0) - self.assertEqual(f.misses, 0) + f.cache_clear() # test clearing + self.assertEqual(f.cache_hits, 0) + self.assertEqual(f.cache_misses, 0) f(x, y) - self.assertEqual(f.hits, 0) - self.assertEqual(f.misses, 1) + self.assertEqual(f.cache_hits, 0) + self.assertEqual(f.cache_misses, 1) # Test bypassing the cache self.assertIs(f.__wrapped__, orig) f.__wrapped__(x, y) - self.assertEqual(f.hits, 0) - self.assertEqual(f.misses, 1) + self.assertEqual(f.cache_hits, 0) + self.assertEqual(f.cache_misses, 1) # test size zero (which means "never-cache") @functools.lru_cache(0) -- cgit v0.12