Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02566ec

Browse files
committed
Adopt more descriptive attribute names as suggested on python-dev.
1 parent e9a4de5 commit 02566ec

5 files changed

Lines changed: 23 additions & 23 deletions

File tree

Doc/library/functools.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ The :mod:`functools` module defines the following functions:
4747
results, the positional and keyword arguments to the function must be
4848
hashable.
4949

50-
The wrapped function is instrumented with two attributes, :attr:`hits`
51-
and :attr:`misses` which count the number of successful or unsuccessful
50+
The wrapped function is instrumented with two attributes, :attr:`cache_hits`
51+
and :attr:`cache_misses` which count the number of successful or unsuccessful
5252
cache lookups. These statistics are helpful for tuning the *maxsize*
5353
parameter and for measuring the cache's effectiveness.
5454

55-
The wrapped function also has a :attr:`clear` attribute which can be
55+
The wrapped function also has a :attr:`cache_clear` attribute which can be
5656
called (with no arguments) to clear the cache.
5757

5858
The original underlying function is accessible through the

Doc/whatsnew/3.2.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ New, Improved, and Deprecated Modules
8585
return c.fetchone()[0]
8686

8787
To help with choosing an effective cache size, the wrapped function is
88-
instrumented with two attributes *hits* and *misses*::
88+
instrumented with two attributes *cache_hits* and *cache_misses*::
8989

9090
>>> for name in user_requests:
9191
... get_phone_number(name)
92-
>>> print(get_phone_number.hits, get_phone_number.misses)
92+
>>> print(get_phone_number.cache_hits, get_phone_number.cache_misses)
9393
4805 980
9494

9595
If the phonelist table gets updated, the outdated contents of the cache can be
9696
cleared with::
9797

98-
>>> get_phone_number.clear()
98+
>>> get_phone_number.cache_clear()
9999

100100
(Contributed by Raymond Hettinger)
101101

Lib/functools.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,23 @@ def wrapper(*args, **kwds):
142142
with lock:
143143
result = cache[key]
144144
cache_renew(key) # record recent use of this key
145-
wrapper.hits += 1
145+
wrapper.cache_hits += 1
146146
except KeyError:
147147
result = user_function(*args, **kwds)
148148
with lock:
149149
cache[key] = result # record recent use of this key
150-
wrapper.misses += 1
150+
wrapper.cache_misses += 1
151151
if len(cache) > maxsize:
152152
cache_popitem(0) # purge least recently used cache entry
153153
return result
154154

155-
def clear():
155+
def cache_clear():
156156
"""Clear the cache and cache statistics"""
157157
with lock:
158158
cache.clear()
159-
wrapper.hits = wrapper.misses = 0
159+
wrapper.cache_hits = wrapper.cache_misses = 0
160160

161-
wrapper.hits = wrapper.misses = 0
162-
wrapper.clear = clear
161+
wrapper.cache_hits = wrapper.cache_misses = 0
162+
wrapper.cache_clear = cache_clear
163163
return wrapper
164164
return decorating_function

Lib/re.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ def compile(pattern, flags=0):
207207

208208
def purge():
209209
"Clear the regular expression caches"
210-
_compile_typed.clear()
211-
_compile_repl.clear()
210+
_compile_typed.cache_clear()
211+
_compile_repl.cache_clear()
212212

213213
def template(pattern, flags=0):
214214
"Compile a template pattern, returning a pattern object"

Lib/test/test_functools.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -508,21 +508,21 @@ def orig(x, y):
508508
actual = f(x, y)
509509
expected = orig(x, y)
510510
self.assertEquals(actual, expected)
511-
self.assert_(f.hits > f.misses)
512-
self.assertEquals(f.hits + f.misses, 1000)
511+
self.assert_(f.cache_hits > f.cache_misses)
512+
self.assertEquals(f.cache_hits + f.cache_misses, 1000)
513513

514-
f.clear() # test clearing
515-
self.assertEqual(f.hits, 0)
516-
self.assertEqual(f.misses, 0)
514+
f.cache_clear() # test clearing
515+
self.assertEqual(f.cache_hits, 0)
516+
self.assertEqual(f.cache_misses, 0)
517517
f(x, y)
518-
self.assertEqual(f.hits, 0)
519-
self.assertEqual(f.misses, 1)
518+
self.assertEqual(f.cache_hits, 0)
519+
self.assertEqual(f.cache_misses, 1)
520520

521521
# Test bypassing the cache
522522
self.assertIs(f.__wrapped__, orig)
523523
f.__wrapped__(x, y)
524-
self.assertEqual(f.hits, 0)
525-
self.assertEqual(f.misses, 1)
524+
self.assertEqual(f.cache_hits, 0)
525+
self.assertEqual(f.cache_misses, 1)
526526

527527
# test size zero (which means "never-cache")
528528
@functools.lru_cache(0)

0 commit comments

Comments
 (0)