-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathconfig_util.py
61 lines (48 loc) · 1.55 KB
/
config_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
import cPickle as pickle
import uuid
from utils import local_file
import codecs
import json
CONF_FILE = local_file('configs.pkl')
defaults = {
'too_small': 7,
'small_contour_thresh': 2, # times small std dev
'viterbi_post_process': True,
'break_threshold': 2.0,
'hang_off_amount': .4,
'segmenter': 'experimental',
'line_breaker': 'line_cut',
'detect_special_notation': False,
'feature_transform': None, # pca pickle or whatever
}
def save_config(config, path):
'''Save a single configuration'''
return json.dump(config, codecs.open(path, 'w', 'utf-8'))
def load_config(path):
'''Load a single configuration'''
return json.load(codecs.open(path, 'r', 'utf-8'))
def rand_id():
return str(uuid.uuid4())
def add_new_config(conf):
all_confs = load_configs()
if not _is_duplicate_conf(all_confs, conf):
all_confs[rand_id()] = conf
save_configs(all_confs)
return
def _is_duplicate_conf(all_confs, conf):
keys = defaults.keys()
is_duplicate = False
for oconf in all_confs:
if is_duplicate: break
for key in keys:
if conf[key] != oconf[key]:
break
else:
is_duplicate = True
return is_duplicate
def save_configs(confs):
'''Save collection of configurations'''
pickle.dump(confs, open(local_file(CONF_FILE), 'wb'))
def load_configs():
'''Load collection of configurations'''
return pickle.load(open(CONF_FILE, 'rb'))