-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
200 lines (167 loc) · 5.67 KB
/
utility.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import sys
import os
import json
import re
from time import time, sleep
from datetime import datetime
from dataclasses import dataclass
import importlib
import pkgutil
# shortening
join = os.path.join
isdir = os.path.isdir
isfile = os.path.isfile
listdir = os.listdir
def subdirs(folder: str, join=True, prefix=None, suffix=None, sort=True):
''' Get lists of subdirs for a target folder.
Args:
folder: (str), Path of target folder.
join: (bool), Return full path of subfolders (True) or folder names (False). The default is True.
prefix: (str), Specify prefix.
suffix: (str), Specify suffix.
sort: (bool), Whether to sort the results. The default is True.
Returns:
(list) subdirs list.
'''
if join:
l = os.path.join
else:
l = lambda x, y: y
res = [l(folder, i) for i in os.listdir(folder) if os.path.isdir(os.path.join(folder, i))
and (prefix is None or i.startswith(prefix))
and (suffix is None or i.endswith(suffix))]
if sort:
res.sort()
return res
subfolders = subdirs
def subfiles(folder, join=True, prefix=None, suffix=None, sort=True):
''' Get lists of sub-files for a target folder.
Args:
folder: (str), Path of target folder.
join: (bool), Return full path of sub-files (True) or file names (False). The default is True.
prefix: (str), Specify prefix.
suffix: (str), Specify suffix.
sort: (bool), Whether to sort the results. The default is True.
Returns:
(list) subdirs list.
'''
if join:
l = os.path.join
else:
l = lambda x, y: y
res = [l(folder, i) for i in os.listdir(folder) if os.path.isfile(os.path.join(folder, i))
and (prefix is None or i.startswith(prefix))
and (suffix is None or i.endswith(suffix))]
if sort:
res.sort()
return res
def maybe_mkdir_p(directory):
''' If a path does not exist, create it level by level.
Args:
directory: (str), Target path.
Raises:
FileExistsError: This can sometimes happen when two jobs try to create the same directory at the same time.
'''
directory = os.path.abspath(directory)
# splits = directory.split("/")[1:]
splits = re.split(r'[/\\]', directory)[1:]
for i in range(0, len(splits)):
if not os.path.isdir(os.path.join("/", *splits[:i+1])):
try:
os.mkdir(os.path.join("/", *splits[:i+1]))
except FileExistsError:
# this can sometimes happen when two jobs try to create the same directory at the same time,
# especially on network drives.
print("WARNING: Folder %s already existed and does not need to be created" % directory)
def load_json(file):
'''
Args:
file:
Returns:
'''
with open(file, 'r') as f:
a = json.load(f)
return a
def save_json(obj, file, indent=4, sort_keys=True):
'''
Args:
obj:
file:
indent:
sort_keys:
Returns:
'''
with open(file, 'w') as f:
json.dump(obj, f, sort_keys=sort_keys, indent=indent)
@dataclass
class globalVal():
log_file = ''
project_path = ''
model_path = ''
output_path = ''
device = ''
first_rank = True
def init_log_file(path:str, prefix='mrLog'):
"""
init log file, call this function at the begining fo the programme. After this \
you can call print_to_log_file to write text into the log file.
Args:
path: (str), folder to write log
prefix: (str), prefix of the log file name
Returns:
"""
maybe_mkdir_p(path)
fileName = datetime.now().strftime(prefix+'_'+'%Y_%m_%d_%H_%M_%S')
globalVal.log_file = join(path, fileName + '.txt')
with open(globalVal.log_file, 'w') as f:
f.write("Starting... \n")
def print_to_log_file(*args, also_print_to_console=True, add_timestamp=True):
"""
print to log file.
Args:
*args: things to print
also_print_to_console: (bool)
add_timestamp: (bool), add time stamp at beginning of the line
Returns:
"""
if not globalVal.first_rank:
return
timestamp = time()
dt_object = datetime.fromtimestamp(timestamp)
if add_timestamp:
args = ("%s:" % dt_object, *args)
assert globalVal.log_file is not None, "error: print to log file before init. (from utility.py)"
successful = False
max_attempts = 5
ctr = 0
while not successful and ctr < max_attempts:
try:
with open(globalVal.log_file, 'a+') as f:
for a in args:
f.write(str(a))
f.write(" ")
f.write("\n")
successful = True
except IOError:
print("%s: failed to log: " % datetime.fromtimestamp(timestamp), sys.exc_info())
sleep(0.5)
ctr += 1
if also_print_to_console:
print(*args)
def recursive_find_class(folder, class_name, current_module):
tr = None
for importer, modname, ispkg in pkgutil.iter_modules([folder[0]]):
# print(modname, ispkg)
if not ispkg:
m = importlib.import_module(current_module + "." + modname)
if hasattr(m, class_name):
tr = getattr(m, class_name)
break
if tr is None:
for importer, modname, ispkg in pkgutil.iter_modules([folder[0]]):
if ispkg:
next_current_module = current_module + "." + modname
tr = recursive_find_class([join(folder[0], modname)], class_name, current_module=next_current_module)
if tr is not None:
break
return tr