-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
35 lines (31 loc) · 1.08 KB
/
utils.py
File metadata and controls
35 lines (31 loc) · 1.08 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
import os
import yaml
from collections import OrderedDict
from datetime import datetime
# class Config():
def get_timestamp():
return datetime.now().strftime('%y%m%d_%H%M%S')
def from_yaml(filepath):
if os.path.isabs(filepath):
with open(filepath, 'r', encoding='utf-8') as file:
cfg = yaml.safe_load(file.read())
else:
filepath = os.path.abspath(os.path.expanduser(filepath))
with open(filepath, 'r', encoding='utf-8') as file:
cfg = yaml.safe_load(file.read())
dict_cfgs = cfg
return Attr_Dict(cfg), dict_cfgs
class Attr_Dict(object):
def __init__(self, data):
for name, value in data.items():
setattr(self, name, self._wrap(value))
def _wrap(self, value):
if isinstance(value, (tuple, list, set, frozenset)):
return type(value)([self._wrap(v) for v in value])
else:
return Attr_Dict(value) if isinstance(value, dict) else value
def convert_to_config(opt, config):
opt = vars(opt)
for key in opt:
if key not in config:
config[key] = opt[key]