-
Notifications
You must be signed in to change notification settings - Fork 5
/
pickle_data.py
48 lines (36 loc) · 1.14 KB
/
pickle_data.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
42
43
44
45
46
47
48
"""
Functions for pickling and unpickling data
"""
import os
import pickle
from absl import logging
def save_pickle(filename, data, overwrite=False, debug=True):
"""
Pickle some data so we don't have to rerun everything
Usage:
savePickle("data.pickle", (data1, data2))
"""
if not overwrite and os.path.exists(filename):
logging.debug("Skipping, %s exists", filename)
else:
logging.debug("Pickling %s", filename)
if os.path.exists(filename):
os.rename(filename, filename+".bak")
try:
with open(filename, 'wb') as f:
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
except Exception as error:
logging.error("Error saving %s: %s", filename, error)
def load_pickle(filename, debug=True):
"""
Load some data from a pickle file
Usage:
if os.path.exists("data.pickle"):
data1, data2 = loadPickle("data.pickle")
"""
if os.path.exists(filename):
logging.debug("Loading %s", filename)
with open(filename, 'rb') as f:
data = pickle.load(f)
return data
return None