forked from Gab0/japonicus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTOMLutils.py
54 lines (45 loc) · 1.44 KB
/
TOMLutils.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
#!/bin/python
import re
def preprocessTOMLFile(filepath):
f = open(filepath)
f = f.read()
f = f.split('\n')
return f
def TOMLToParameters(TOMLLines):
Parameters = {}
subkey = None
parseTuple = lambda s: (float(x) for x in s[1:-1].split(','))
for Line in TOMLLines:
if Line.startswith('#'):
continue
sub = re.findall("\[\w+\]", Line)
if sub:
subkey = sub[0][1:-1]
if subkey not in Parameters.keys():
Parameters[subkey] = {}
elif '=' in Line:
target = Parameters[subkey] if subkey else Parameters
L = Line.split(' = ') if ' = ' in Line else Line.split('=')
target[L[0]] = parseTuple(L[1]) if '(' in L[1] else float(L[1])
else:
subkey = None
return Parameters
def parametersToTOML(Settings):
text = []
toParameter = lambda name, value: "%s = %f" % (name, value)
# print("{{ %s }}" % Settings[Strat])
def iterate(base):
Settingskeys = base.keys()
Settingskeys = sorted(
list(Settingskeys), key= lambda x: type(base[x]) == dict, reverse=False
)
for W in Settingskeys:
Q = base[W]
if type(Q) == dict:
text.append("[%s]" % W)
iterate(Q)
text.append('')
else:
text.append("%s = %s" % (W, Q))
iterate(Settings)
return '\n'.join(text)