-
Notifications
You must be signed in to change notification settings - Fork 85
/
util.py
53 lines (39 loc) · 1.17 KB
/
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
from os import makedirs
from os.path import dirname, abspath, join, exists
import re
def get_root_path():
return dirname(dirname(abspath(__file__)))
def get_data_path():
return join(get_root_path(), "datasets")
def get_log_path():
return join(get_log_path(), "logs")
def create_dir_if_not_exists(folder):
if not exists(folder):
makedirs(folder)
def get_args_info_as_str(config_flags):
rtn = []
d = vars(config_flags)
for k in sorted_nicely(d.keys()):
v = d[k]
if type(v) is dict:
for k2, v2 in v.items():
s = "{0:26} : {1}".format(k + "_" + k2, v2)
rtn.append(s)
else:
s = "{0:26} : {1}".format(k, v)
rtn.append(s)
return "\n".join(rtn)
def sorted_nicely(l, reverse=False):
def tryint(s):
try:
return int(s)
except:
return s
def alphanum_key(s):
if type(s) is not str:
raise ValueError("{} must be a string in l: {}".format(s, l))
return [tryint(c) for c in re.split("([0-9]+)", s)]
rtn = sorted(l, key=alphanum_key)
if reverse:
rtn = reversed(rtn)
return rtn