-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.py
More file actions
34 lines (29 loc) · 1.1 KB
/
constants.py
File metadata and controls
34 lines (29 loc) · 1.1 KB
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
import json
from types import SimpleNamespace
def iterativeNamespaceFromDict(data):
def iterate(holder, key, subdata):
if type(subdata) == type({}):
holder[key] = SimpleNamespace()
for dk, dv in subdata.items():
iterate(holder[key].__dict__, dk, dv)
elif type(subdata) == type([]):
holder[key] = list(range(len(subdata)))
for dk, dv in enumerate(subdata):
iterate(holder[key], dk, dv)
else:
holder[key] = subdata
package = {0: None}
iterate(package, 0, data)
return package[0]
GLOBAL_CONSTANTS = SimpleNamespace()
def loadConstants(file="constants.json"):
f = open('constants.json')
data = json.load(f)
f.close()
global GLOBAL_CONSTANTS
#GLOBAL_CONSTANTS = iterativeNameSpaceFromDict(data) this rewrites the reference, meaning other files importing it don't update...
GLOBAL_CONSTANTS.__dict__.clear()
simple = iterativeNamespaceFromDict(data)
for key, value in simple.__dict__.items():
GLOBAL_CONSTANTS.__dict__[key] = value
loadConstants()