Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
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
FSL
fslpy
Commits
10f40bf7
Commit
10f40bf7
authored
8 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
New memoize function which caches based on argument values,
and allows caller to specify which arguments to use.
parent
a33ff838
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
fsl/utils/memoize.py
+53
-0
53 additions, 0 deletions
fsl/utils/memoize.py
with
53 additions
and
0 deletions
fsl/utils/memoize.py
+
53
−
0
View file @
10f40bf7
...
@@ -21,6 +21,59 @@ import functools
...
@@ -21,6 +21,59 @@ import functools
import
six
import
six
def
memoize
(
args
=
None
,
kwargs
=
None
):
"""
Memoize the given function by the value of the input arguments, allowing
the caller to specify which positional arguments (by index) and keyword
arguments (by name) are used for the comparison.
If no positional or keyword arguments are specified, the function is
memoized on all arguments.
.. note:: This decorator must always be called with brackets, e.g.::
memoize()
def myfunc():
...
:arg args: A list of positional argument indices.
:arg kwargs: A list of keyword argument names.
"""
cache
=
{}
def
decorator
(
func
):
def
wrapper
(
*
a
,
**
kwa
):
key
=
[]
if
args
is
not
None
:
key
+=
[
a
[
i
]
for
i
in
args
]
if
kwargs
is
not
None
:
key
+=
[
kwa
[
k
]
for
k
in
kwargs
]
# This decorator was created without
# any arguments specified - use all
# the arguments as the cache key.
if
len
(
key
)
==
0
:
# Keyword arguments are unordered,
# so we'll try and overcome this
# by sorting the kwarg dict keys.
key
=
list
(
a
)
+
list
([
kwa
[
k
]
for
k
in
sorted
(
kwa
.
keys
)])
key
=
tuple
(
key
)
try
:
result
=
cache
[
key
]
except
KeyError
:
result
=
func
(
*
a
,
**
kwa
)
cache
[
key
]
=
result
return
result
return
wrapper
return
decorator
def
memoizeMD5
(
func
):
def
memoizeMD5
(
func
):
"""
Memoize the given function. Whenever the function is called, an
"""
Memoize the given function. Whenever the function is called, an
md5 digest of its arguments is calculated - if the digest has been
md5 digest of its arguments is calculated - if the digest has been
...
...
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