-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
91 lines (77 loc) · 2.5 KB
/
tools.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
from configparser import RawConfigParser
def config_from_dict(d: dict, prefix: str):
i = len(prefix)
r = dict()
for key, value in d.items():
if key.find(prefix) == 0:
name = key[i:]
r[name] = value
return r
def create_numeric_columns(cf: dict):
cf['numeric_columns'] = []
for unit in cf['numeric_units']:
suffix = '_' + unit
i = len(suffix)
for column in cf['columns']:
if column[-i:] == suffix:
cf['numeric_columns'].append(column)
def create_range_values(cf: dict):
r = dict()
for line in cf['range_values']:
column, values = line.split(':')
min_, max_ = values.split(',')
r[column] = float(min_), float(max_)
cf['range_values'] = r
def get_names(d: dict) -> (list, dict):
names = list(d.keys())
alias = dict()
for key in d:
alias[key] = key
values = d[key].strip().split()
for value in values:
alias[value] = key
return names, alias
def create_names(conf: RawConfigParser, cf: dict):
r = dict()
for column in cf['columns']:
if column[-5:] != '_name':
continue
r[column] = ([], dict())
for section in conf.sections():
if section[-5:] != '_name':
continue
column = section
keys, alias = get_names(dict(conf.items(column)))
r[column] = (keys, alias)
if r:
cf['names'] = r
def read_conf(conf_file):
def to_str(key):
if (s := cf.get(key)) and (s := s.strip()):
cf[key] = s
def to_list(key):
if (s := cf.get(key)) and (s := s.strip()) and (r := s.split()):
cf[key] = r
return True
conf = RawConfigParser()
# https://stackoverflow.com/questions/19359556/configparser-reads-capital-keys-and-make-them-lower-case
conf.optionxform = str
conf.read(conf_file)
cf = dict(conf.items('main'))
# Untuk downloader.py & to_category.py
to_str('filter')
if cf.get('filter', '').find('stock > 0') > -1:
cf['is_ready_stock'] = True
# Untuk to_category.py
cf['prompt_template'] = cf['prompt_template'].strip()
cf['columns'] = cf['columns'].strip().split()
to_str('role')
# Untuk repair.py
to_list('not_null_columns')
to_list('numeric_units') and create_numeric_columns(cf)
to_list('range_values') and create_range_values(cf)
create_names(conf, cf)
# Untuk check.py
to_list('count_columns')
to_list('min_max_columns')
return cf