Unittest - Add a memoize function for caching of function results
This commit is contained in:
@@ -165,6 +165,20 @@ def release_port(port):
|
|||||||
pass
|
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:
|
try:
|
||||||
from shutil import which
|
from shutil import which
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|||||||
Reference in New Issue
Block a user