-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
32 lines (27 loc) · 811 Bytes
/
Copy pathcache.py
File metadata and controls
32 lines (27 loc) · 811 Bytes
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
import pickle
from os.path import exists, splitext
def get_cache(filename, function):
cache_file = splitext(filename)[0] + ".p"
if exists(cache_file):
print "cache: reading cache"
try:
with file(cache_file) as f:
return pickle.load(f)
except:
print "cache: error. unable to read file"
print "cache: calling processing func"
assert function is not None
data = function(filename)
try:
with file(cache_file, "w") as f:
print "cache: dumping data"
pickle.dump(data, f)
except:
print "cache: error. unable to write data"
return data
def test():
data = "asd"
testmethod = lambda x: x + "qwe"
print get_cache(data, testmethod)
if __name__ == '__main__':
test()