Skip to content

Commit b7ce03b

Browse files
added better code description
1 parent efb2712 commit b7ce03b

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/modelspec/utils.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,20 @@ def load_xml(filename: str):
7171
tree = ET.parse(infile) # Parse the XML file into an ElementTree object
7272
root = tree.getroot() # Get the root element
7373

74-
# Convert the ElementTree object to a dictionary
75-
xml_string = (
76-
ET.tostring(root).decode().replace("ns0:", "").replace(":ns0", "").strip()
77-
)
74+
# Converts the loaded xml into a string and removes unwanted string values ':ns0' and 'ns0:'
75+
# They prevent the xml from loading correctly
76+
xml_string = ET.tostring(root).decode().replace('ns0:', '').replace(':ns0', '').strip()
7877

78+
# Removes xmlns, xmlns:xsi and xsi:schemaLocation from the xml structure for conversion
7979
removed_namespaces = process_xml_namespace(xml_string)
80+
81+
# Converts the resulting xml stripped of xmlns, xmlns:xsi and xsi:schemaLocation into a dict
8082
data = element_to_dict(removed_namespaces)
83+
84+
# Removes every key having 'id' and replaces it with it's value
8185
removed_id = handle_id(data)
86+
87+
# Values are returned as strings after conversion, this corrects them to their actual values
8288
converted_to_actual_val = convert_values(removed_id)
8389

8490
return convert_values(converted_to_actual_val)
@@ -101,11 +107,11 @@ def element_to_dict(element):
101107

102108
children_by_tag = {}
103109
for child_element in element:
104-
child_key = child_element.tag + "s"
110+
child_key = child_element.tag + 's'
105111
child_value = element_to_dict(child_element)
106112

107113
# Check if the child element has an 'id' attribute
108-
if "id" in child_element.attrib:
114+
if 'id' in child_element.attrib:
109115
# If the child element has an 'id', add it to the result dictionary directly
110116
result[child_key] = child_value
111117
else:
@@ -123,10 +129,12 @@ def process_xml_namespace(xml_string):
123129
ignored_elements = [
124130
'xmlns="http://www.neuroml.org/schema/neuroml2"',
125131
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"',
126-
'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"',
132+
'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"'
127133
]
134+
135+
# Loops through the xml string and removes every instance of the elements in the list named ignored_elements
128136
for ignored_element in ignored_elements:
129-
xml_string = xml_string.replace(ignored_element, "").strip()
137+
xml_string = xml_string.replace(ignored_element, '').strip()
130138

131139
# Parse the XML string into an ElementTree
132140
root = ET.fromstring(xml_string)
@@ -231,6 +239,8 @@ def build_xml_element(data, parent=None):
231239
Returns:
232240
Parent
233241
"""
242+
243+
# If a parent name isn't given, it extracts the name from the class and use that instead
234244
if parent is None:
235245
parent = ET.Element(data.__class__.__name__)
236246

@@ -244,24 +254,23 @@ def build_xml_element(data, parent=None):
244254
for child in children:
245255
child_element = build_xml_element(child)
246256
parent.append(child_element)
247-
elif not any(
248-
hasattr(data, attr_name)
249-
for attr_name in ["xmlns", "xmlns_url", "xmlns_loc", "xmln_loc_2"]
250-
):
257+
elif not any(hasattr(data, attr_name) for attr_name in ["xmlns", "xmlns_url", "xmlns_loc", "xmln_loc_2"]):
251258
attribute_name = aattr.name
252259
attribute_value = data.__getattribute__(aattr.name)
253260
parent.set(attribute_name, str(attribute_value))
254-
261+
262+
# This defines the various namespaces and schemaLocation of the generated xml
255263
if hasattr(data, "xmlns"):
256264
parent.set("xmlns", data.xmlns)
257265
if hasattr(data, "xmlns_url"):
258266
parent.set("xmlns:xsi", data.xmlns_url)
259267
if hasattr(data, "xmlns_loc"):
260-
parent.set("xsi:schemaLocation", str(data.xmlns_loc + "\n" + data.xmln_loc_2))
268+
parent.set("xsi:schemaLocation", str(data.xmlns_loc + '\n' + data.xmln_loc_2))
261269

262270
return parent
263271

264272

273+
265274
def ascii_encode_dict(data):
266275
ascii_encode = (
267276
lambda x: x.encode("ascii")

0 commit comments

Comments
 (0)