diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ee206bdb9cd74e39704092471e186b94fb688938..a3b3c95044cf458df9d418adc48839242046e4a2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 diff --git a/fsl/utils/memoize.py b/fsl/utils/memoize.py index cbe95a4494f10934d311cd1856ec66bebeade431..b777eed0c945a048858806107487bf214d710984 100644 --- a/fsl/utils/memoize.py +++ b/fsl/utils/memoize.py @@ -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 diff --git a/tests/test_memoize.py b/tests/test_memoize.py index 9b885d0fd7d2c739fddd1d7bc15bb8eb753f5ba0..386674659b4a9af2648dadf445efd1a84a170f37 100644 --- a/tests/test_memoize.py +++ b/tests/test_memoize.py @@ -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():