From e74271e1b54899039380a990905922fddc671a42 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Tue, 1 May 2018 18:07:36 +0100 Subject: [PATCH] ENH: memoize.skipUnchanged has an invalidate "method" --- fsl/utils/memoize.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/fsl/utils/memoize.py b/fsl/utils/memoize.py index cbe95a449..b777eed0c 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 -- GitLab