Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
02566ec8
Commit
02566ec8
authored
Sep 04, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adopt more descriptive attribute names as suggested on python-dev.
parent
e9a4de51
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
23 additions
and
23 deletions
+23
-23
Doc/library/functools.rst
Doc/library/functools.rst
+3
-3
Doc/whatsnew/3.2.rst
Doc/whatsnew/3.2.rst
+3
-3
Lib/functools.py
Lib/functools.py
+6
-6
Lib/re.py
Lib/re.py
+2
-2
Lib/test/test_functools.py
Lib/test/test_functools.py
+9
-9
No files found.
Doc/library/functools.rst
View file @
02566ec8
...
...
@@ -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:`c
ache_c
lear` attribute which can be
called (with no arguments) to clear the cache.
The original underlying function is accessible through the
...
...
Doc/whatsnew/3.2.rst
View file @
02566ec8
...
...
@@ -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.c
ache_c
lear()
(Contributed by Raymond Hettinger)
...
...
Lib/functools.py
View file @
02566ec8
...
...
@@ -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
c
ache_c
lear
():
"""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
.
c
lear
=
clear
wrapper
.
cache_hits
=
wrapper
.
cache_
misses
=
0
wrapper
.
c
ache_clear
=
cache_
clear
return
wrapper
return
decorating_function
Lib/re.py
View file @
02566ec8
...
...
@@ -207,8 +207,8 @@ def compile(pattern, flags=0):
def
purge
():
"Clear the regular expression caches"
_compile_typed
.
clear
()
_compile_repl
.
clear
()
_compile_typed
.
c
ache_c
lear
()
_compile_repl
.
c
ache_c
lear
()
def
template
(
pattern
,
flags
=
0
):
"Compile a template pattern, returning a pattern object"
...
...
Lib/test/test_functools.py
View file @
02566ec8
...
...
@@ -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
.
c
ache_c
lear
()
# 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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment