-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
35 lines (33 loc) · 903 Bytes
/
utils.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
import cPickle
import hashlib
import os
def filecontents(fname):
f=open(fname)
content = f.read()
f.close()
return content
def save_pickled(fname,content):
f = open(fname,"w")
cPickle.Pickler(f).dump(content)
f.close()
def unpickled_content(fname):
f=open(fname)
content=cPickle.Unpickler(f).load()
f.close()
return content
"""
If file fname exists unpickle the contents and return it. If it doesnt exist,
call fn with the arguments and store a pickled representation of the return value in fname. Also return the result of call to fn
"""
def read_cached(fname, fn, *args):
if not os.path.isfile(fname):
result = fn(*args)
save_pickled(fname, result)
return result
else:
return unpickled_content(fname)
"""
Returns the hexdigest of the content
"""
def get_hash(content):
return hashlib.sha1(content).hexdigest()