-
Notifications
You must be signed in to change notification settings - Fork 0
/
pickle_util.py
88 lines (69 loc) · 2.35 KB
/
pickle_util.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 25 15:16:11 2018
@author: TiffMin
"""
import _pickle as cPickle
import os, sys
#import hashlib
class PickleUtil:
def __init__(self, file_path):
self.path = file_path
self.read_output = None
self.dumped_output = None
self.read_success = 0
self.written = 0
def exists(self):
return os.path.exists(self.path)
def is_empty(self):
if self.exists():
try:
cPickle.load(open(self.path, 'rb'))
#Need to close?
print(str(self.path) + "all good")
return False
except EOFError:
print(str(self.path) + "not corrupt but empty")
return True
except IOError:
print(str(self.path) + "not empty but can't be opened(corrupt)")
return False
else:
print(str(self.path) + "does not exist")
return False
def is_corrupt(self):
if self.exists():
try:
cPickle.load(open(self.path, 'rb'))
#Need to close?
print(str(self.path) + "all good")
return False
except EOFError:
print(str(self.path) + "not corrupt but empty")
return False
#UnpicklingError and IOError
except:
print(str(self.path) + "not empty but can't be opened(corrupt)")
return True
else:
print(str(self.path) + "does not exist")
return False
#load to work in all environments
def safe_load(self):
if self.exists() and not(self.is_empty()) and not(self.is_corrupt()):
rfile = open(self.path, 'rb')
self.read_output = cPickle.load(rfile)
rfile.close()
self.read_success = 1
else:
self.read_output = None
return self.read_output
#safely pickle data
def safe_save(self, arg, protocol=2):
wfile = open(self.path, 'wb')
self.dumped_output = cPickle.dump(arg, wfile, protocol)
wfile.close()
self.written = 1
return self.dumped_output
#Protocol?