-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcommon_cache.py
36 lines (31 loc) · 954 Bytes
/
common_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def select_with_scores(texts, scores, score_predicate, select):
indices = [i for i in range(len(texts)) if score_predicate(scores[i])]
if indices == []:
return texts[0], scores[0]
text = select([texts[i] for i in indices], indices)
return text, [scores[i] for i in indices if text == texts[i]][0]
def create_score_predicate(f=lambda x: x):
def fetch(x):
score = f(x)
return score is None or score > 0
return fetch
def score_first(x):
return x[0]
def create_cached_func(f):
cache = {}
stats = {'hit': 0, 'miss': 0}
def fetch(x):
INITIAL = object()
y = cache.get(x, INITIAL)
if y == INITIAL:
stats['miss'] += 1
y = f(x)
cache[x] = y
else:
stats['hit'] += 1
return y
def reset_cache():
cache.clear()
stats['hit'] = 0
stats['miss'] = 0
return fetch, stats, reset_cache