-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
65 lines (55 loc) · 1.66 KB
/
helper.py
File metadata and controls
65 lines (55 loc) · 1.66 KB
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
"""
Helper functions.
"""
import os
import subprocess
import json
import argparse
### IO
def check_dir(d):
if not os.path.exists(d):
print("Directory {} does not exist. Exit.".format(d))
exit(1)
def check_files(files):
for f in files:
if f is not None and not os.path.exists(f):
print("File {} does not exist. Exit.".format(f))
exit(1)
def ensure_dir(d, verbose=True):
if not os.path.exists(d):
if verbose:
print("Directory {} do not exist; creating...".format(d))
os.makedirs(d)
def save_config(config, path, verbose=True):
with open(path, 'w') as outfile:
json.dump(config, outfile, indent=2)
if verbose:
print("Config saved to file {}".format(path))
return config
def load_config(path, verbose=True):
with open(path) as f:
config = json.load(f)
if verbose:
print("Config loaded from file {}".format(path))
return config
def print_config(config):
info = "Running with the following configs:\n"
for k,v in config.items():
info += "\t{} : {}\n".format(k, str(v))
print("\n" + info + "\n")
return
class FileLogger(object):
"""
A file logger that opens the file periodically and write to it.
"""
def __init__(self, filename, header=None):
self.filename = filename
if os.path.exists(filename):
# remove the old file
os.remove(filename)
if header is not None:
with open(filename, 'w') as out:
print(header, file=out)
def log(self, message):
with open(self.filename, 'a') as out:
print(message, file=out)