Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Frederik Hennig
pystencils
Commits
b1dccb49
Commit
b1dccb49
authored
Mar 25, 2022
by
Frederik Hennig
Committed by
Markus Holzer
Mar 25, 2022
Browse files
Fix: sharedmethodcache didn't preserve docstrings
parent
1f3b1300
Changes
2
Hide whitespace changes
Inline
Side-by-side
pystencils/cache.py
View file @
b1dccb49
import
os
from
collections.abc
import
Hashable
from
functools
import
partial
from
functools
import
partial
,
wraps
from
itertools
import
chain
try
:
...
...
@@ -43,6 +43,7 @@ def sharedmethodcache(cache_id: str):
Of course, for this to be useful, said methods must have the same signature (up to additional kwargs)
and must return the same result when called with the same arguments."""
def
_decorator
(
user_method
):
@
wraps
(
user_method
)
def
_decorated_func
(
self
,
*
args
,
**
kwargs
):
objdict
=
self
.
__dict__
cache
=
objdict
.
setdefault
(
cache_id
,
dict
())
...
...
pystencils_tests/test_sharedmethodcache.py
View file @
b1dccb49
...
...
@@ -60,13 +60,16 @@ class Triad:
@
sharedmethodcache
(
"triad_cache"
)
def
triad
(
self
,
a
,
b
,
c
=
0
):
"""Computes the triad a*b+c."""
self
.
triad_called
+=
1
return
a
*
b
+
c
def
test_tria
b
_memoization
():
def
test_tria
d
_memoization
():
triad
=
Triad
()
assert
triad
.
triad
.
__doc__
==
"Computes the triad a*b+c."
t
=
triad
.
triad
(
12
,
4
,
15
)
assert
triad
.
triad_called
==
1
assert
triad
.
triad_cache
[(
12
,
4
,
15
)]
==
t
...
...
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