forked from hahnmichael/miniTopSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameter.py
130 lines (108 loc) · 4.39 KB
/
parameter.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
'''
Modul zum Laden einer Parameter Datenbank namens parameters.db
PARAMETER ConfigParser Objekt der Parameter
COMMENT ConfigParser Objekt der Kommentare/Erklärungen
CONDITION ConfigParser Objekt der Bedinungen
init() lädt die Datenbank namens parameters.db
read(Datei) lädt Paramter aus der Datei
bei nicht übereinstimmen des Typs gibt es einen Error und das Programm wird beendet
toDict() gibt ein Dictionary mit den keys und den werten aus PARAMETER zurück
@author:Michael Hahn e1125097@student.tuwien.ac.at
'''
import configparser
import sys
#hilfs Config objekte
PARAMETER = configparser.ConfigParser()
CONDITION = configparser.ConfigParser()
COMMENT = configparser.ConfigParser()
PAR_IN = configparser.ConfigParser()
#Typabfrage des Parameters Rückgabewert ist der Typ als String
#Vorher float dann bool, weil wenn zahl mit 0 enthält würde bool ausschlagen
def checktypeparameter(section,key):
#versucht ob der angegebene string ein float ist
try:
PARAMETER.getfloat(section,key)
typ='float'
except ValueError:
#wenn es kein float ist wird überprüft ob es eine boolsche Variable ist
try:
PARAMETER.getboolean(section,key)
typ='bool'
#ansonst ist es ein string
except ValueError:
typ='string'
return typ
def checktypepar_in(section,key):
try:
PAR_IN.getfloat(section,key)
typ='float'
except ValueError:
try:
PAR_IN.getboolean(section,key)
typ='bool'
except ValueError:
typ='string'
return typ
def init():
#parameters.db einlesen
PAR_DEFAULT = configparser.ConfigParser()
PAR_DEFAULT.read('parameters.db')
#Schleife über alle sections
for section in PAR_DEFAULT:
#erstellen der Sections in den config objekten
PARAMETER[section]={}
CONDITION[section]={}
COMMENT[section]={}
#schleife über alle Parameter in der Section
for key in PAR_DEFAULT[section]:
#Auslesen des Tupels aus dem strin und zuteilung zu den Config objekten
tupel=eval(PAR_DEFAULT.get(section,key),{},{})
PARAMETER[section][key]=str(tupel[0])
CONDITION[section][key]=str(tupel[1])
#Überprüfung ob die Bedingung richtig ist
try:
CONDITION.getboolean(section,key)
except ValueError:
if(CONDITION[section][key].lower() == 'none'):
CONDITION[section][key]='True'
else:
error='Type of condition '+key+' is not correct'
sys.exit(error)
COMMENT[section][key]=str(tupel[2])
def read(Datei):
#Datei einlesen
PAR_IN.read(Datei)
#Iteration über alle Sections und Keys
for section in PARAMETER:
for key in PARAMETER[section]:
#Abfrage ob die Eingelesene section und der key vorhanden sind und abfrage ob die Bedingung erfüllt ist
if (section in PAR_IN) and (key in PAR_IN[section]) and CONDITION.getboolean(section,key):
#Abfrage ob die Typen übereinstimmen
if checktypepar_in(section,key) is checktypeparameter(section,key):
PARAMETER[section][key]=PAR_IN[section][key]
else:
#Beendigung des Programms wenn der Typ nich übereinstimmt
error='Type of '+key+' does not match with type in parameters.db'
sys.exit(error)
PAR_IN.clear()
# Load the variables into the 'globals' namespace
varDict = toDict()
for key in varDict:
globals()[key] = varDict[key]
# String parameters start and end with a single quotes (e.g: 'Cosine')
# To correctly use string parameters, we strip them out.
def unquote_string_parameter(s):
return s.strip('\'')
def toDict():
dictionary={}
for section in PARAMETER:
for key in PARAMETER[section]:
parType = checktypeparameter(section, key)
if parType == 'float':
dictionary[key.upper()]=PARAMETER.getfloat(section,key)
elif parType == 'bool':
dictionary[key.upper()]=PARAMETER.getboolean(section,key)
else:
dictionary[key.upper()]=unquote_string_parameter(PARAMETER.get(section,key))
return dictionary