I have several dictionaries that contain "results". They are all of different lengths and sometimes they include nested dicionaries. Here's an example of what one can look like:
Code: Select all
default dict_with_results_1 = {'result 1' : { 'b' : 2,'c': {'inner c' : 3, 'inner c2' : 3.5} },
'result 2' : { 'e' : 4 , 'f' : 5 , 'g' : 6 },
'result 3' : { 'h' : 7, 'i' : 8 },
'result 4' : { 'j' : 9 } } The screen then displays the key/value pairs of the dictionary as text with some fomatting (that I've mostly cut out here). And if there is a nested dictionary it should be displayed with the same type of formatting, which requires unpacking the nested dict as well.
Code: Select all
label start:
'...'
menu:
'Option 1':
call screen screen_test(dict_with_results_1)
'Option 2':
call screen screen_test(dict_with_results_2) Code: Select all
screen screen_test(d):
frame:
vbox:
for result,x in d.items():
hbox:
text '[result] -- '
vbox:
for k,v in x.items():
hbox:
text '[k] - '
if isinstance(v, dict):
vbox:
for innerK,innerV in v.items():
hbox:
text '[innerK] - '
text '[innerV]'
else:
text '[v]'
The thing is that since all the dictionaries look different I don't know how many levels of nested dictionaries will be neccessary. And I would rather not just have to write the same code over and over again.
In regular python code I would do this by just recalling the same function. See this example from a different part of the code, where "inner_func" gets called over and over until all levels of nesting have been reached:
Code: Select all
def checkDict(old_dict):
new_dict = { }
def inner_func(n,d):
for k, v in d.items():
if isinstance(v,dict):
n[k] = { }
inner_func(n[k],v) #re-run for nested dictionaries
else:
#Stuff I actually want to do the the values of the dict
inner_func(new_dict,old_dict)
return new_dictOr should I try a different approach all together?
(A later goal is the replace all the h/vboxes with grids or vpgrids without making the sizes all bonkers, but that's for another day)