Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Spack
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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
eic_tools
Spack
Commits
92f398a8
Commit
92f398a8
authored
9 years ago
by
Todd Gamblin
Browse files
Options
Downloads
Patches
Plain Diff
Better `@memoized` decorator.
parent
da98b076
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
lib/spack/llnl/util/lang.py
+25
-8
25 additions, 8 deletions
lib/spack/llnl/util/lang.py
with
25 additions
and
8 deletions
lib/spack/llnl/util/lang.py
+
25
−
8
View file @
92f398a8
...
@@ -26,6 +26,7 @@
...
@@ -26,6 +26,7 @@
import
re
import
re
import
sys
import
sys
import
functools
import
functools
import
collections
import
inspect
import
inspect
# Ignore emacs backups when listing modules
# Ignore emacs backups when listing modules
...
@@ -170,16 +171,32 @@ def has_method(cls, name):
...
@@ -170,16 +171,32 @@ def has_method(cls, name):
return
False
return
False
def
memoized
(
obj
):
class
memoized
(
obj
ect
):
"""
Decorator that caches the results of a function, storing them
"""
Decorator that caches the results of a function, storing them
in an attribute of that function.
"""
in an attribute of that function.
"""
cache
=
obj
.
cache
=
{}
def
__init__
(
self
,
func
):
@functools.wraps
(
obj
)
self
.
func
=
func
def
memoizer
(
*
args
,
**
kwargs
):
self
.
cache
=
{}
if
args
not
in
cache
:
cache
[
args
]
=
obj
(
*
args
,
**
kwargs
)
return
cache
[
args
]
def
__call__
(
self
,
*
args
):
return
memoizer
if
not
isinstance
(
args
,
collections
.
Hashable
):
# Not hashable, so just call the function.
return
self
.
func
(
*
args
)
if
args
not
in
self
.
cache
:
self
.
cache
[
args
]
=
self
.
func
(
*
args
)
return
self
.
cache
[
args
]
def
__get__
(
self
,
obj
,
objtype
):
"""
Support instance methods.
"""
return
functools
.
partial
(
self
.
__call__
,
obj
)
def
clear
(
self
):
"""
Expunge cache so that self.func will be called again.
"""
self
.
cache
.
clear
()
def
list_modules
(
directory
,
**
kwargs
):
def
list_modules
(
directory
,
**
kwargs
):
...
...
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