We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b5b81ac commit 6d5cc14Copy full SHA for 6d5cc14
1 file changed
adaptive/utils.py
@@ -1,5 +1,6 @@
1
# -*- coding: utf-8 -*-
2
from contextlib import contextmanager
3
+from functools import wraps
4
from itertools import product
5
import time
6
@@ -24,3 +25,16 @@ def restore(*learners):
24
25
finally:
26
for state, learner in zip(states, learners):
27
learner.__setstate__(state)
28
+
29
30
+def cache_latest(f):
31
+ """Cache the latest return value of the function and add it
32
+ as 'self._cache[f.__name__]'."""
33
+ @wraps(f)
34
+ def wrapper(*args, **kwargs):
35
+ self = args[0]
36
+ if not hasattr(self, '_cache'):
37
+ self._cache = {}
38
+ self._cache[f.__name__] = f(*args, **kwargs)
39
+ return self._cache[f.__name__]
40
+ return wrapper
0 commit comments