Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pytools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kaushik Kulkarni
pytools
Commits
ff1f7a80
Commit
ff1f7a80
authored
9 years ago
by
Andreas Klöckner
Browse files
Options
Downloads
Patches
Plain Diff
Implement memoize_method in terms of memoize_on_first_arg.
parent
efc40990
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pytools/__init__.py
+15
-42
15 additions, 42 deletions
pytools/__init__.py
with
15 additions
and
42 deletions
pytools/__init__.py
+
15
−
42
View file @
ff1f7a80
...
@@ -448,44 +448,7 @@ class _HasKwargs(object):
...
@@ -448,44 +448,7 @@ class _HasKwargs(object):
pass
pass
def
memoize_method
(
method
):
def
memoize_on_first_arg
(
function
,
cache_dict_name
=
None
):
"""
Supports cache deletion via ``method_name.clear_cache(self)``.
.. note::
*clear_cache* support requires Python 2.5 or newer.
"""
cache_dict_name
=
intern
(
"
_memoize_dic_
"
+
method
.
__name__
)
def
wrapper
(
self
,
*
args
,
**
kwargs
):
if
kwargs
:
key
=
(
_HasKwargs
,
frozenset
(
six
.
iteritems
(
kwargs
)))
+
args
else
:
key
=
args
try
:
return
getattr
(
self
,
cache_dict_name
)[
key
]
except
AttributeError
:
result
=
method
(
self
,
*
args
,
**
kwargs
)
setattr
(
self
,
cache_dict_name
,
{
key
:
result
})
return
result
except
KeyError
:
result
=
method
(
self
,
*
args
,
**
kwargs
)
getattr
(
self
,
cache_dict_name
)[
key
]
=
result
return
result
def
clear_cache
(
self
):
delattr
(
self
,
cache_dict_name
)
if
sys
.
version_info
>=
(
2
,
5
):
from
functools
import
update_wrapper
new_wrapper
=
update_wrapper
(
wrapper
,
method
)
new_wrapper
.
clear_cache
=
clear_cache
return
new_wrapper
def
memoize_on_first_arg
(
function
):
"""
Like :func:`memoize_method`, but for functions that take the object
"""
Like :func:`memoize_method`, but for functions that take the object
to do memoization as first argument.
to do memoization as first argument.
...
@@ -495,8 +458,9 @@ def memoize_on_first_arg(function):
...
@@ -495,8 +458,9 @@ def memoize_on_first_arg(function):
*clear_cache* support requires Python 2.5 or newer.
*clear_cache* support requires Python 2.5 or newer.
"""
"""
cache_dict_name
=
intern
(
"
_memoize_dic_
"
if
cache_dict_name
is
None
:
+
function
.
__module__
+
function
.
__name__
)
cache_dict_name
=
intern
(
"
_memoize_dic_
"
+
function
.
__module__
+
function
.
__name__
)
def
wrapper
(
obj
,
*
args
,
**
kwargs
):
def
wrapper
(
obj
,
*
args
,
**
kwargs
):
if
kwargs
:
if
kwargs
:
...
@@ -526,6 +490,16 @@ def memoize_on_first_arg(function):
...
@@ -526,6 +490,16 @@ def memoize_on_first_arg(function):
return
new_wrapper
return
new_wrapper
def
memoize_method
(
method
):
"""
Supports cache deletion via ``method_name.clear_cache(self)``.
.. note::
*clear_cache* support requires Python 2.5 or newer.
"""
return
memoize_on_first_arg
(
method
,
intern
(
"
_memoize_dic_
"
+
method
.
__name__
))
def
memoize_method_with_uncached
(
uncached_args
=
[],
uncached_kwargs
=
set
()):
def
memoize_method_with_uncached
(
uncached_args
=
[],
uncached_kwargs
=
set
()):
"""
Supports cache deletion via ``method_name.clear_cache(self)``.
"""
Supports cache deletion via ``method_name.clear_cache(self)``.
...
@@ -627,8 +601,7 @@ def memoize_method_nested(inner):
...
@@ -627,8 +601,7 @@ def memoize_method_nested(inner):
class
memoize_in
(
object
):
# noqa
class
memoize_in
(
object
):
# noqa
"""
Adds a cache to a function nested inside a method. The cache is attached
"""
Adds a cache to a function nested inside a method. The cache is attached
to *memoize_cache_context* (if it exists) or *self* in the outer (method)
to *object*.
namespace.
Requires Python 2.5 or newer.
Requires Python 2.5 or newer.
"""
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment