mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
Unittest - Add a memoize function for caching of function results
This commit is contained in:
parent
00ee5b2289
commit
d69533add1
1 changed files with 14 additions and 0 deletions
|
@ -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:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue