-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.py
55 lines (37 loc) · 1.15 KB
/
utils.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
import os
import csv
import yaml
import time
import numpy as np
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
def read_config(path):
return AttrDict(yaml.load(open(path, 'r')))
def read_datas(filename, trans_to_num=False):
lines = open(filename, 'r').readlines()
lines = list(map(lambda x: x.split(), lines))
if trans_to_num:
lines = [list(map(int, line)) for line in lines]
return lines
def save_datas(data, filename, trans_to_str=False):
if trans_to_str:
data = [list(map(str, line)) for line in data]
lines = list(map(lambda x: " ".join(x), data))
with open(filename, 'w') as f:
f.write("\n".join(lines))
def logging(file):
def write_log(s):
print(s, end='')
with open(file, 'a') as f:
f.write(s)
return write_log
def logging_csv(file):
def write_csv(s):
with open(file, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(s)
return write_csv
def format_time(t):
return time.strftime("%Y-%m-%d-%H:%M:%S", t)