-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommons.py
41 lines (33 loc) · 975 Bytes
/
commons.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
37
38
39
40
41
import json
import os
import time
from functools import wraps
def file_cache(filename):
def decorator(func):
@wraps(func)
def cached_function(*args, **kwargs):
cache = read(filename)
if cache:
print "using cache"
return cache
else:
result = func(*args, **kwargs)
common.save(result, filename)
return result
return cached_function
return decorator
def touch(filename, times=None):
print "filename: ", filename
with open(filename, 'a'):
os.utime(filename, times)
def save(data, filename):
with open(filename, 'w') as f:
json.dump(data, f, sort_keys=True,
indent=4,
separators=(',', ': '))
def read(filename):
try:
with open(filename, 'r') as f:
return json.load(f)
except (IOError, ValueError):
return None