Skip to content
Snippets Groups Projects
Commit d4a1d9f1 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Merge branch 'enh/skipUnchanged_invalidate' into 'master'

Enh/skip unchanged invalidate

See merge request fsl/fslpy!50
parents e9870d73 68f7f069
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,10 @@ Added ...@@ -18,6 +18,10 @@ Added
function which submits a cluster job via ``fsl_sub`` (by Michiel Cottaar). function which submits a cluster job via ``fsl_sub`` (by Michiel Cottaar).
* Assertions (in the :mod:`.assertions` module) can be disabled with the * Assertions (in the :mod:`.assertions` module) can be disabled with the
new :func:`.assertions.disabled` context manager. new :func:`.assertions.disabled` context manager.
* New :mod:`fsl.utils.parse_data` module containing various neuroimaging
data constructors for use with ``argparse``.
* The :func:`.memoize.skipUnchanged` decorator has an ``invalidate`` function
which allows its cache to be cleared.
Changed Changed
......
...@@ -205,6 +205,18 @@ def skipUnchanged(func): ...@@ -205,6 +205,18 @@ def skipUnchanged(func):
*not* called. If the given value is different from the cached value (or *not* called. If the given value is different from the cached value (or
there is no value), the decorated function is called. there is no value), the decorated function is called.
The ``invalidate`` method may be called on a ``skipUnchanged``-decorated
function to clear the internal cache. For example::
@skipUnchanged
def setval(name, value):
# ...
# ...
setval.invalidate()
.. note:: This decorator ignores the return value of the decorated .. note:: This decorator ignores the return value of the decorated
function. function.
...@@ -216,6 +228,10 @@ def skipUnchanged(func): ...@@ -216,6 +228,10 @@ def skipUnchanged(func):
cache = {} cache = {}
# TODO merge skipUnchanged and Memoize somehow
def invalidate():
cache.clear()
def wrapper(name, value, *args, **kwargs): def wrapper(name, value, *args, **kwargs):
oldVal = cache.get(name, None) oldVal = cache.get(name, None)
...@@ -243,6 +259,8 @@ def skipUnchanged(func): ...@@ -243,6 +259,8 @@ def skipUnchanged(func):
return True return True
wrapper.invalidate = invalidate
return wrapper return wrapper
......
...@@ -242,7 +242,29 @@ def test_skipUnchanged(): ...@@ -242,7 +242,29 @@ def test_skipUnchanged():
wrapped('key4', np.zeros((1, 4))) wrapped('key4', np.zeros((1, 4)))
assert timesCalled['key4'] == 2 assert timesCalled['key4'] == 2
timesCalled['key1'] = 0
timesCalled['key2'] = 0
timesCalled['key3'] = 0
wrapped('key1', 1)
wrapped('key2', 2)
wrapped('key3', 3)
assert timesCalled['key1'] == 1
assert timesCalled['key2'] == 1
assert timesCalled['key3'] == 1
wrapped('key1', 1)
wrapped('key2', 2)
wrapped('key3', 3)
assert timesCalled['key1'] == 1
assert timesCalled['key2'] == 1
assert timesCalled['key3'] == 1
wrapped.invalidate()
wrapped('key1', 1)
wrapped('key2', 2)
wrapped('key3', 3)
assert timesCalled['key1'] == 2
assert timesCalled['key2'] == 2
assert timesCalled['key3'] == 2
def test_Instanceify(): def test_Instanceify():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment