|
a |
|
b/common/common.py |
|
|
1 |
import os |
|
|
2 |
import _pickle as pickle |
|
|
3 |
import h5py |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
def create_folder(path): |
|
|
7 |
if not os.path.exists(path): |
|
|
8 |
os.makedirs(path) |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
def save_obj(obj, name): |
|
|
12 |
with open(name + '.pkl', 'wb') as f: |
|
|
13 |
pickle.dump(obj, f) |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
def load_obj(name): |
|
|
17 |
with open(name + '.pkl', 'rb') as f: |
|
|
18 |
return pickle.load(f) |
|
|
19 |
|
|
|
20 |
|
|
|
21 |
class H5Recorder(object): |
|
|
22 |
def __init__(self, path): |
|
|
23 |
self.path = path |
|
|
24 |
|
|
|
25 |
def open(self, read=False): |
|
|
26 |
if read: |
|
|
27 |
self.hf = h5py.File(self.path, 'r') |
|
|
28 |
else: |
|
|
29 |
self.hf = h5py.File(self.path, 'w') |
|
|
30 |
|
|
|
31 |
def write(self, key, value): |
|
|
32 |
self.hf.create_dataset(key, data=value) |
|
|
33 |
|
|
|
34 |
def read(self, key): |
|
|
35 |
return self.hf.get(key) |
|
|
36 |
|
|
|
37 |
def close(self): |
|
|
38 |
self.hf.close() |