-
Notifications
You must be signed in to change notification settings - Fork 47
/
CommonUtil.py
50 lines (37 loc) · 1.09 KB
/
CommonUtil.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
from __future__ import absolute_import, division, print_function
import datetime
import os
import shutil
class Logger(object):
def __init__(self):
self.file = None
self.buffer = ''
def __del__(self):
if self.file is not None:
self.file.close()
def set_log_file(self, filename):
assert self.file is None
self.file = open(filename, 'wt')
if self.buffer is not None:
self.file.write(self.buffer)
self.buffer = None
def write(self, *args):
now = datetime.datetime.now()
dtstr = now.strftime('%Y-%m-%d %H:%M:%S')
t_msg = '[%s]' % dtstr + ' %s' % ' '.join(map(str, args))
print(t_msg)
if self.file is not None:
self.file.write(t_msg + '\n')
else:
self.buffer += t_msg
def flush(self):
if self.file is not None:
self.file.flush()
logger = Logger()
def safe_rm_mkdir(dir):
if os.path.exists(dir):
shutil.rmtree(dir)
os.mkdir(dir)
def safe_mkdir(dir):
if not os.path.exists(dir):
os.mkdir(dir)