-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathutil.py
69 lines (53 loc) · 1.82 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import gin
import logging
import os
import shutil
from solutions.base_solution import BaseSolution
from tasks.base_task import BaseTask
@gin.configurable
def create_task(task_loader, **kwargs):
"""Load and return a task."""
if isinstance(task_loader, BaseTask):
return task_loader
else:
return task_loader(**kwargs)
@gin.configurable
def create_solution(solution_loader, **kwargs):
"""Create a solution."""
if isinstance(solution_loader, BaseSolution):
return solution_loader
else:
return solution_loader(**kwargs)
def save_config(log_dir, config):
"""Create a log directory and save config in it.
Create a log directory and save configurations.
Args:
log_dir: str. Path of the log directory.
config: str. Path to configuration file.
"""
if not os.path.exists(log_dir):
os.makedirs(log_dir)
shutil.copy(config, os.path.join(log_dir, 'config.gin'))
def create_logger(name, log_dir=None, debug=False):
"""Create a logger.
Create a logger that logs to log_dir.
Args:
name: str. Name of the logger.
log_dir: str. Path to log directory.
debug: bool. Whether to set debug level logging.
Returns:
logging.logger.
"""
if log_dir and not os.path.exists(log_dir):
os.makedirs(log_dir)
log_format = '%(asctime)s %(process)d [%(levelname)s] %(message)s'
logging.basicConfig(level=logging.DEBUG if debug else logging.INFO,
format=log_format)
logger = logging.getLogger(name)
if log_dir:
log_file = os.path.join(log_dir, '{}.txt'.format(name))
file_hdl = logging.FileHandler(log_file)
formatter = logging.Formatter(fmt=log_format)
file_hdl.setFormatter(formatter)
logger.addHandler(file_hdl)
return logger