|
a |
|
b/utils/io.py |
|
|
1 |
''' |
|
|
2 |
@Author: your name |
|
|
3 |
@Date: 2020-01-06 17:08:52 |
|
|
4 |
@LastEditTime : 2020-01-07 13:25:31 |
|
|
5 |
@LastEditors : Please set LastEditors |
|
|
6 |
@Description: In User Settings Edit |
|
|
7 |
@FilePath: /KGCN_Keras-master/utils/io.py |
|
|
8 |
''' |
|
|
9 |
# -*- coding: utf-8 -*- |
|
|
10 |
|
|
|
11 |
import os |
|
|
12 |
import json |
|
|
13 |
import pickle |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
def pickle_load(filename: str): |
|
|
17 |
try: |
|
|
18 |
with open(filename, 'rb') as f: |
|
|
19 |
obj = pickle.load(f) |
|
|
20 |
print(f'Logging Info - Loaded: {filename}') |
|
|
21 |
except EOFError: |
|
|
22 |
print(f'Logging Error - Cannot load: {filename}') |
|
|
23 |
obj = None |
|
|
24 |
|
|
|
25 |
return obj |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
def pickle_dump(filename: str, obj): |
|
|
29 |
with open(filename, 'wb') as f: |
|
|
30 |
pickle.dump(obj, f) |
|
|
31 |
print(f'Logging Info - Saved: {filename}') |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
def write_log(filename: str, log, mode='w'): |
|
|
35 |
with open(filename, mode) as writers: |
|
|
36 |
writers.write('\n') |
|
|
37 |
json.dump(log, writers, indent=4, ensure_ascii=False) |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
def format_filename(_dir: str, filename_template: str, **kwargs): |
|
|
41 |
"""Obtain the filename of data base on the provided template and parameters""" |
|
|
42 |
filename = os.path.join(_dir, filename_template.format(**kwargs)) |
|
|
43 |
return filename |