-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfaas_utils.py
52 lines (40 loc) · 1.37 KB
/
dfaas_utils.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
import sys
from pathlib import Path
import orjson
def dict_to_json(data, file_path):
file_path = to_pathlib(file_path)
try:
with open(file_path, "wb") as file:
enc = orjson.dumps(
data, option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_SORT_KEYS
)
file.write(enc)
file.write(b"\n")
except IOError as e:
print(
f"Failed to write dict to json file to {file_path.as_posix()!r}: {e}",
file=sys.stderr,
)
sys.exit(1)
def json_to_dict(file_path):
file_path = to_pathlib(file_path)
try:
with open(file_path, "r") as file:
return orjson.loads(file.read())
except IOError as e:
print(
f"Failed to read json file from {file_path.as_posix()!r}: {e}",
file=sys.stderr,
)
sys.exit(1)
def to_pathlib(file_path):
# Make sure to have a Path object, because we want the absolute path.
if isinstance(file_path, str):
file_path = Path(file_path)
return file_path.absolute()
def parse_result_file(result_path):
result_path = to_pathlib(result_path)
# The "result.json" file is not a valid JSON file. Each line is an isolated
# JSON object, the result of one training iteration.
with result_path.open() as result:
return [orjson.loads(line) for line in result]