[535f03]: / tests / test.flatten.py

Download this file

28 lines (21 with data), 655 Bytes

 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
from osim.env import ProstheticsEnv
import numpy as np
import unittest
# Source: https://stackoverflow.com/questions/39135433/how-to-flatten-nested-python-dictionaries
def flatten(d):
res = [] # Result list
if isinstance(d, dict):
for key, val in sorted(d.items()):
res.extend(flatten(val))
elif isinstance(d, list):
res = d
else:
res = [d]
return res
d = { '123': { 'key3': 3, 'key2': 11, 'key1': 1 },
'124': { 'key1': 6, 'key2': 56, 'key3': 6 },
'125': { 'key1': 7, 'key2': 44, 'key3': 9 },
}
env = ProstheticsEnv()
print(flatten(env.reset()))
print(flatten(d))