From d69533add10e51ba86b0c0a58eb4dc98be07cfda Mon Sep 17 00:00:00 2001 From: Renato Alves Date: Fri, 18 Jul 2014 17:22:17 +0100 Subject: [PATCH] Unittest - Add a memoize function for caching of function results --- test/basetest/utils.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/basetest/utils.py b/test/basetest/utils.py index f8d85b59e..b47230226 100644 --- a/test/basetest/utils.py +++ b/test/basetest/utils.py @@ -165,6 +165,20 @@ def release_port(port): pass +def memoize(obj): + """Keep an in-memory cache of function results given it's inputs + """ + cache = obj.cache = {} + + @functools.wraps(obj) + def memoizer(*args, **kwargs): + key = str(args) + str(kwargs) + if key not in cache: + cache[key] = obj(*args, **kwargs) + return cache[key] + return memoizer + + try: from shutil import which except ImportError: