Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pycodegen
pystencils
Commits
b1dccb49
Commit
b1dccb49
authored
3 years ago
by
Frederik Hennig
Committed by
Markus Holzer
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix: sharedmethodcache didn't preserve docstrings
parent
1f3b1300
No related branches found
No related tags found
1 merge request
!287
Fix: sharedmethodcache didn't preserve docstrings
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pystencils/cache.py
+2
-1
2 additions, 1 deletion
pystencils/cache.py
pystencils_tests/test_sharedmethodcache.py
+4
-1
4 additions, 1 deletion
pystencils_tests/test_sharedmethodcache.py
with
6 additions
and
2 deletions
pystencils/cache.py
+
2
−
1
View file @
b1dccb49
import
os
import
os
from
collections.abc
import
Hashable
from
collections.abc
import
Hashable
from
functools
import
partial
from
functools
import
partial
,
wraps
from
itertools
import
chain
from
itertools
import
chain
try
:
try
:
...
@@ -43,6 +43,7 @@ def sharedmethodcache(cache_id: str):
...
@@ -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)
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.
"""
and must return the same result when called with the same arguments.
"""
def
_decorator
(
user_method
):
def
_decorator
(
user_method
):
@wraps
(
user_method
)
def
_decorated_func
(
self
,
*
args
,
**
kwargs
):
def
_decorated_func
(
self
,
*
args
,
**
kwargs
):
objdict
=
self
.
__dict__
objdict
=
self
.
__dict__
cache
=
objdict
.
setdefault
(
cache_id
,
dict
())
cache
=
objdict
.
setdefault
(
cache_id
,
dict
())
...
...
This diff is collapsed.
Click to expand it.
pystencils_tests/test_sharedmethodcache.py
+
4
−
1
View file @
b1dccb49
...
@@ -60,13 +60,16 @@ class Triad:
...
@@ -60,13 +60,16 @@ class Triad:
@sharedmethodcache
(
"
triad_cache
"
)
@sharedmethodcache
(
"
triad_cache
"
)
def
triad
(
self
,
a
,
b
,
c
=
0
):
def
triad
(
self
,
a
,
b
,
c
=
0
):
"""
Computes the triad a*b+c.
"""
self
.
triad_called
+=
1
self
.
triad_called
+=
1
return
a
*
b
+
c
return
a
*
b
+
c
def
test_tria
b
_memoization
():
def
test_tria
d
_memoization
():
triad
=
Triad
()
triad
=
Triad
()
assert
triad
.
triad
.
__doc__
==
"
Computes the triad a*b+c.
"
t
=
triad
.
triad
(
12
,
4
,
15
)
t
=
triad
.
triad
(
12
,
4
,
15
)
assert
triad
.
triad_called
==
1
assert
triad
.
triad_called
==
1
assert
triad
.
triad_cache
[(
12
,
4
,
15
)]
==
t
assert
triad
.
triad_cache
[(
12
,
4
,
15
)]
==
t
...
...
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