Skip to content

Commit 6d5cc14

Browse files
committed
add 'cache_loss' decorator
1 parent b5b81ac commit 6d5cc14

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

adaptive/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from contextlib import contextmanager
3+
from functools import wraps
34
from itertools import product
45
import time
56

@@ -24,3 +25,16 @@ def restore(*learners):
2425
finally:
2526
for state, learner in zip(states, learners):
2627
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

Comments
 (0)