Skip to content

Commit 1d24991

Browse files
modified the element_to_dict to get a better dictionary representation
1 parent c971569 commit 1d24991

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/modelspec/utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,21 @@ def element_to_dict(element):
9494
if attrs:
9595
result.update(attrs)
9696

97+
children_by_tag = {}
9798
for child_element in element:
98-
child_key = child_element.tag
99+
child_key = child_element.tag + "s"
99100
child_value = element_to_dict(child_element)
100101

101-
if child_key in result:
102-
if not isinstance(result[child_key], list):
103-
result[child_key] = [result[child_key]]
104-
result[child_key].append(child_value)
105-
else:
102+
# Check if the child element has an 'id' attribute
103+
if "id" in child_element.attrib:
104+
# If the child element has an 'id', add it to the result dictionary directly
106105
result[child_key] = child_value
106+
else:
107+
# If the child element does not have an 'id', represent it as a list
108+
children_by_tag.setdefault(child_key, []).append(child_value)
109+
110+
# Append the lists to the result dictionary
111+
result.update(children_by_tag)
107112

108113
return result
109114

0 commit comments

Comments
 (0)