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
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
Andreas Klöckner
pytools
Commits
05168035
Unverified
Commit
05168035
authored
4 years ago
by
Andreas Klöckner
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #67 from alexfikl/memoize-method-immutable
memoize_method for frozen classes
parents
077b6e2c
626334ca
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Pipeline
#139202
passed
4 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pytools/__init__.py
+24
-13
24 additions, 13 deletions
pytools/__init__.py
test/test_pytools.py
+42
-0
42 additions, 0 deletions
test/test_pytools.py
with
66 additions
and
13 deletions
pytools/__init__.py
+
24
−
13
View file @
05168035
...
...
@@ -675,8 +675,9 @@ def memoize_on_first_arg(function, cache_dict_name=None):
"""
if
cache_dict_name
is
None
:
cache_dict_name
=
intern
(
"
_memoize_dic_
"
+
function
.
__module__
+
function
.
__name__
)
cache_dict_name
=
intern
(
f
"
_memoize_dic_
{
function
.
__module__
}{
function
.
__name__
}
"
)
def
wrapper
(
obj
,
*
args
,
**
kwargs
):
if
kwargs
:
...
...
@@ -688,7 +689,7 @@ def memoize_on_first_arg(function, cache_dict_name=None):
return
getattr
(
obj
,
cache_dict_name
)[
key
]
except
AttributeError
:
result
=
function
(
obj
,
*
args
,
**
kwargs
)
setattr
(
obj
,
cache_dict_name
,
{
key
:
result
})
object
.
__
setattr
__
(
obj
,
cache_dict_name
,
{
key
:
result
})
return
result
except
KeyError
:
result
=
function
(
obj
,
*
args
,
**
kwargs
)
...
...
@@ -696,7 +697,7 @@ def memoize_on_first_arg(function, cache_dict_name=None):
return
result
def
clear_cache
(
obj
):
delattr
(
obj
,
cache_dict_name
)
object
.
__
delattr
__
(
obj
,
cache_dict_name
)
from
functools
import
update_wrapper
new_wrapper
=
update_wrapper
(
wrapper
,
function
)
...
...
@@ -707,9 +708,15 @@ def memoize_on_first_arg(function, cache_dict_name=None):
def
memoize_method
(
method
:
F
)
->
F
:
"""
Supports cache deletion via ``method_name.clear_cache(self)``.
.. versionchanged:: 2021.2
Can memoize methods on classes that do not allow setting attributes
(e.g. by overwritting ``__setattr__``).
"""
return
memoize_on_first_arg
(
method
,
intern
(
"
_memoize_dic_
"
+
method
.
__name__
))
return
memoize_on_first_arg
(
method
,
intern
(
f
"
_memoize_dic_
{
method
.
__name__
}
"
))
class
keyed_memoize_on_first_arg
:
# noqa: N801
...
...
@@ -729,8 +736,7 @@ class keyed_memoize_on_first_arg: # noqa: N801
self
.
cache_dict_name
=
cache_dict_name
def
_default_cache_dict_name
(
self
,
function
):
return
intern
(
"
_memoize_dic_
"
+
function
.
__module__
+
function
.
__name__
)
return
intern
(
f
"
_memoize_dic_
{
function
.
__module__
}{
function
.
__name__
}
"
)
def
__call__
(
self
,
function
):
cache_dict_name
=
self
.
cache_dict_name
...
...
@@ -746,7 +752,7 @@ class keyed_memoize_on_first_arg: # noqa: N801
return
getattr
(
obj
,
cache_dict_name
)[
cache_key
]
except
AttributeError
:
result
=
function
(
obj
,
*
args
,
**
kwargs
)
setattr
(
obj
,
cache_dict_name
,
{
cache_key
:
result
})
object
.
__
setattr
__
(
obj
,
cache_dict_name
,
{
cache_key
:
result
})
return
result
except
KeyError
:
result
=
function
(
obj
,
*
args
,
**
kwargs
)
...
...
@@ -754,7 +760,7 @@ class keyed_memoize_on_first_arg: # noqa: N801
return
result
def
clear_cache
(
obj
):
delattr
(
obj
,
cache_dict_name
)
object
.
__
delattr
__
(
obj
,
cache_dict_name
)
from
functools
import
update_wrapper
new_wrapper
=
update_wrapper
(
wrapper
,
function
)
...
...
@@ -770,9 +776,14 @@ class keyed_memoize_method(keyed_memoize_on_first_arg): # noqa: N801
which computes and returns the cache key.
.. versionadded :: 2020.3
.. versionchanged:: 2021.2
Can memoize methods on classes that do not allow setting attributes
(e.g. by overwritting ``__setattr__``).
"""
def
_default_cache_dict_name
(
self
,
function
):
return
intern
(
"
_memoize_dic_
"
+
function
.
__name__
)
return
intern
(
f
"
_memoize_dic_
{
function
.
__name__
}
"
)
def
memoize_method_with_uncached
(
uncached_args
=
None
,
uncached_kwargs
=
None
):
...
...
@@ -797,7 +808,7 @@ def memoize_method_with_uncached(uncached_args=None, uncached_kwargs=None):
uncached_kwargs
=
list
(
uncached_kwargs
)
def
parametrized_decorator
(
method
):
cache_dict_name
=
intern
(
"
_memoize_dic_
"
+
method
.
__name__
)
cache_dict_name
=
intern
(
f
"
_memoize_dic_
{
method
.
__name__
}
"
)
def
wrapper
(
self
,
*
args
,
**
kwargs
):
cache_args
=
list
(
args
)
...
...
@@ -823,7 +834,7 @@ def memoize_method_with_uncached(uncached_args=None, uncached_kwargs=None):
return
getattr
(
self
,
cache_dict_name
)[
key
]
except
AttributeError
:
result
=
method
(
self
,
*
args
,
**
kwargs
)
setattr
(
self
,
cache_dict_name
,
{
key
:
result
})
object
.
__
setattr
__
(
self
,
cache_dict_name
,
{
key
:
result
})
return
result
except
KeyError
:
result
=
method
(
self
,
*
args
,
**
kwargs
)
...
...
@@ -831,7 +842,7 @@ def memoize_method_with_uncached(uncached_args=None, uncached_kwargs=None):
return
result
def
clear_cache
(
self
):
delattr
(
self
,
cache_dict_name
)
object
.
__
delattr
__
(
self
,
cache_dict_name
)
if
sys
.
version_info
>=
(
2
,
5
):
from
functools
import
update_wrapper
...
...
This diff is collapsed.
Click to expand it.
test/test_pytools.py
+
42
−
0
View file @
05168035
...
...
@@ -131,6 +131,48 @@ def test_memoize_keyfunc():
assert
count
[
0
]
==
2
def
test_memoize_frozen
():
from
dataclasses
import
dataclass
from
pytools
import
memoize_method
# {{{ check frozen dataclass
@dataclass
(
frozen
=
True
)
class
FrozenDataclass
:
value
:
int
@memoize_method
def
double_value
(
self
):
return
2
*
self
.
value
c
=
FrozenDataclass
(
10
)
assert
c
.
double_value
()
==
20
c
.
double_value
.
clear_cache
(
c
)
# pylint: disable=no-member
# }}}
# {{{ check class with no setattr
class
FrozenClass
:
value
:
int
def
__init__
(
self
,
value
):
object
.
__setattr__
(
self
,
"
value
"
,
value
)
def
__setattr__
(
self
,
key
,
value
):
raise
AttributeError
(
f
"
cannot set attribute
{
key
}
"
)
@memoize_method
def
double_value
(
self
):
return
2
*
self
.
value
c
=
FrozenClass
(
10
)
assert
c
.
double_value
()
==
20
c
.
double_value
.
clear_cache
(
c
)
# pylint: disable=no-member
# }}}
@pytest.mark.parametrize
(
"
dims
"
,
[
2
,
3
])
def
test_spatial_btree
(
dims
,
do_plot
=
False
):
pytest
.
importorskip
(
"
numpy
"
)
...
...
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