I need help to build a hierarchical nested dict from a QTreeView structure to get something like this:
{"A": {"B": {"H": {}, "I": {"M": {}, "N": {}}}, "D": {}, "E": {}, "F": {}, "G": {"L": {}}, "C": {"J": {}, "K": {}}}}
{
"A": {
"B": {
"H": {},
"I": {
"M": {},
"N": {}
}
},
"D": {},
"E": {},
"F": {},
"G": {
"L": {}
},
"C": {
"J": {},
"K": {}
}
}
}
I am not using columns in this case and the QTreeView represents a directory structure (i actually extracted it from a dict like tho one above and just want to recreate the dict after modifying the Tree)
I already have some thing like this:
def to_dict(self, _structure={}, _parent=''):
sublist[self.name()] = self._children
for child in self._children:
_structure[self.name()] = sublist
child.to_dict(_structure, self.name())
Obviously self._children is a list so it wont work
EDIT: I think i might need something like this:
def to_dict(self, _structure={}, _parent=''):
sublist = {self.name(): {}}
for child in self._children:
if _parent == '':
_structure = sublist
else:
_structure[_parent].update(sublist)
child.to_dict(_structure, self.name())
return _structure
The Problem here is...i need to find the _parent key in the _structure dictionary and as far as i understand it will always be in the lowest level of the dict...do i really need to search the whole _structure dict averytime a want to add a new subdict to the given _parent or is there a better solution to my problem?