-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchap3_code_37.py
More file actions
41 lines (28 loc) · 1.06 KB
/
chap3_code_37.py
File metadata and controls
41 lines (28 loc) · 1.06 KB
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
37
38
39
40
41
import time
import hashlib
import pickle
cache = {}
def is_obsolete(entry, duration):
return time.time() - entry['time'] > duration
def compute_key(function, args, kw):
key = pickle.dumps((function.__name__, args, kw))
# returns the pickle representation of an object as a byte object
# instead of writing it on a file
# creates a key from the "frozen" key generated in the last step
return hashlib.sha1(key).hexdigest()
def memoize(duration=10):
def _memoize(function):
def __memoize(*args, **kw):
key = compute_key(function, args, kw)
# do we have the value on cache?
if key in cache and not is_obsolete(cache[key], duration):
print('we already have the value')
return cache[key]['value']
# if we didn't
print('calculating...')
result = function(*args, **kw)
# storing the result
cache[key] = {'value': result, 'time': time.time()}
return result
return __memoize
return _memoize