I have a particularly nasty stats object from a system that I need to retrieve data from (two of the many stats entries shown for brevity).
'https://localhost/mgmt/tm/sys/performance/all-stats/TMM%20Memory%20Used': {'nestedStats': {'entries': {'Average': {'description': '5'},
'Current': {'description': '5'},
'Max(since 2019_11_12T02:47:10Z)': {'description': '5'},
'Memory Used': {'description': 'TMM '
'Memory '
'Used'}}}},
'https://localhost/mgmt/tm/sys/performance/all-stats/Utilization': {'nestedStats': {'entries': {'Average': {'description': '9'},
'Current': {'description': '10'},
'Max(since 2019_11_12T02:47:10Z)': {'description': '53'},
'System CPU Usage': {'description': 'Utilization'}}}}}
Currently I use the .get method multiple times in the nested stacks, but I was listening to the author of the glom module on Talk Python this weekend and thought that might be a far cleaner solution for me. And it is, as this code makes it so that I have all the data in a loop without crazy layers of get methods (first example pictured above that I'm working on tonight). The outer key is the long URL, the inner key is the avg/current/max/desc.
stats = b.tm.sys.performances.all_stats.load()
for k, v in stats.entries.items():
print('\n')
spec = f'entries.{k}.nestedStats.entries'
v_stats = glom(stats, spec)
for k, v, in v_stats.items():
spec = f'{k}.description'
stat_vals = glom(v_stats, spec)
print(f'{k}: {stat_vals}')
Which results the data I need:
Average: 5
Current: 5
Max(since 2019_11_12T02:47:10Z): 5
Memory Used: TMM Memory Used
That said, I don't really have control of the data at this point, I'm just printing it. I don't think I'm grokking the power of glom just yet and was curious if someone could point me to an example that'll help my understanding? End goal is to flatten all this data into a single list of 4 item dictionaries.