-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.py
71 lines (56 loc) · 1.73 KB
/
Config.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
#/usr/bin/python
# -*- coding: utf-8 -*-
"""
Natural Language Processing homework assignment.
Valentin Lemière - Guillaume Desquesnes
"""
import codecs
import json
import sys
class Config:
"""
The configuration of the application.
Hold the value of the different parameters
"""
# Associate python type to full name.
__types = { "unicode": "string", "str": "string", "int": "integer", "bool": "boolean", "float": "float" }
# Default values of the config parameters.
__shared_state = {
"nostats": False,
"corpus_path" : u"",
"termino_path" : u"",
"save_file" : u"res.txt",
"size_min" : 4,
"score_min" : 75,
"score_min_termino" : 10,
"freq_termino": 0.35,
"freq_collocation": 6.0,
"bootstrap_iteration": 3,
"bootstrap_freq_filter": 4,
"boostrap_nb": 200,
"macro_average": True
}
def __init__(self):
"""
Constructor. Share the same state that other instance.
"""
self.__dict__ = self.__shared_state
def load(self, filename):
"""
Load a configuration from a file.
@param filename Name of the config file.
"""
try:
with codecs.open(filename, 'r',encoding="UTF-8") as f:
data = json.loads(f.read())
for attr,val in self.__dict__.items():
if not attr in data:
sys.stderr.write('Missing %s field. Using default value: %s\n'%(attr, str(self.__dict__[attr])))
elif type(data[attr]) != type(val):
sys.stderr.write('%s should be of type %s. Using default value: %s\n'%(attr, self.__types[type(val).__name__], str(self.__dict__[attr])))
else:
self.__dict__[attr] = data[attr]
except IOError:
sys.exit("%s does not exist or can't be read."%(filename))
except ValueError:
sys.exit("Encoding problem, encode the config file in UTF-8.")