Commit d4a1d9f1 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

Merge branch 'enh/skipUnchanged_invalidate' into 'master'

Enh/skip unchanged invalidate

See merge request fsl/fslpy!50
parents e9870d73 68f7f069
Pipeline #1916 passed with stages
......@@ -18,6 +18,10 @@ Added
function which submits a cluster job via ``fsl_sub`` (by Michiel Cottaar).
* Assertions (in the :mod:`.assertions` module) can be disabled with the
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
......
......@@ -205,6 +205,18 @@ def skipUnchanged(func):
*not* called. If the given value is different from the cached value (or
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
function.
......@@ -216,6 +228,10 @@ def skipUnchanged(func):
cache = {}
# TODO merge skipUnchanged and Memoize somehow
def invalidate():
cache.clear()
def wrapper(name, value, *args, **kwargs):
oldVal = cache.get(name, None)
......@@ -243,6 +259,8 @@ def skipUnchanged(func):
return True
wrapper.invalidate = invalidate
return wrapper
......
......@@ -242,7 +242,29 @@ def test_skipUnchanged():
wrapped('key4', np.zeros((1, 4)))
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():
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment