I have a dictionary of dictionaries. Within these subdictionaries, I have two keys - ui_section
and section_order
- that determine if the value
of this subdictionary is shown in a specfic portion of the UI and if so, what order it appears. My dictionary looks like this:
MASTER_DICT = {
'key1': {'ui_section':[1,2],'section_order':1, 'value': 'key1'},
'key2': {'ui_section':[1],'section_order':2, 'value': 'key2'},
'key3': {'ui_section':[1,2],'section_order':3, 'value': 'key3'},
'key4': {'ui_section':[1],'section_order':4, 'value': 'key4'},
'key5': {'ui_section':[1],'section_order':5, 'value': 'key5'},
'key6': {'ui_section':[1],'section_order':6, 'value': 'key6'},
'key7': {'ui_section':[1],'section_order':7, 'value': 'key7'},
'key8': {'ui_section':[1],'section_order':8, 'value': 'key8'},
'key9': {'ui_section':[1],'section_order':9, 'value': 'key9'},
}
The ui_section
is a list of possible sections the key can appear in. I determine this via the following code:
def show_section_ui(master_dict, section=None):
if section:
ui_sections = []
# Find the keys that are part of this section
for k in master_dict.keys():
try:
if section in master_dict[k]['ui_section']:
ui_sections.append(master_dict[k])
except AttributeError:
pass
# Order the keys by sort order
ui_sections.sort(key=lambda x: x['section_order'])
return ui_sections
else:
return None
This portion of the code works. The output below shows that order is correct for both sections 1 and 2.
>>> pprint.pprint(show_section_ui(MASTER_DICT, 1))
[{'section_order': 1, 'ui_section': [1,2], 'value': 'key1'},
{'section_order': 2, 'ui_section': [1], 'value': 'key2'},
{'section_order': 3, 'ui_section': [1,2], 'value': 'key3'},
{'section_order': 4, 'ui_section': [1], 'value': 'key4'},
{'section_order': 5, 'ui_section': [1], 'value': 'key5'},
{'section_order': 6, 'ui_section': [1], 'value': 'key6'},
{'section_order': 7, 'ui_section': [1], 'value': 'key7'},
{'section_order': 8, 'ui_section': [1], 'value': 'key8'},
{'section_order': 9, 'ui_section': [1], 'value': 'key9'}]
>>> pprint.pprint(show_section_ui(MASTER_DICT, 2))
[{'section_order': 1, 'ui_section': [1,2], 'value': 'key1'},
{'section_order': 3, 'ui_section': [1,2], 'value': 'key3'}]
My problem is that the section_order
needs to have sort orders per ui_section
. For example, in the above outputs, in section 2, I'd like key3 to be first. My initial thought was to make section_order
a list as well. But, I'm not sure how to adjust this line to properly account for the list (and to select the correct index to sort by then)
ui_sections.sort(key=lambda x: x['section_order'])
My intention was to do something like this:
MASTER_DICT = {
'key1': {'ui_section':[1,2],'section_order':[1,2], 'value': 'key1'},
'key2': {'ui_section':[1],'section_order':[2], 'value': 'key2'},
'key3': {'ui_section':[1,2],'section_order':[3,1], 'value': 'key3'},
}
Getting me output like this:
>>> pprint.pprint(show_section_ui(MASTER_DICT, 2))
[{'section_order': [3,1], 'ui_section': [1,2], 'value': 'key3'},
{'section_order': [1,2], 'ui_section': [1,2], 'value': 'key1'}]
How can I sort by the ui_section
and the appropriate index within the key?