Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
FSL
fslpy
Commits
10f40bf7
Commit
10f40bf7
authored
Jun 02, 2016
by
Paul McCarthy
Browse files
New memoize function which caches based on argument values,
and allows caller to specify which arguments to use.
parent
a33ff838
Changes
1
Hide whitespace changes
Inline
Side-by-side
fsl/utils/memoize.py
View file @
10f40bf7
...
...
@@ -21,6 +21,59 @@ import functools
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
):
"""Memoize the given function. Whenever the function is called, an
md5 digest of its arguments is calculated - if the digest has been
...
...
Write
Preview
Supports
Markdown
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