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