@@ -72,7 +72,9 @@ def load_xml(filename: str):
72
72
root = tree .getroot () # Get the root element
73
73
74
74
# Convert the ElementTree object to a dictionary
75
- xml_string = ET .tostring (root ).decode ().replace ('ns0:' , '' ).replace (':ns0' , '' ).strip ()
75
+ xml_string = (
76
+ ET .tostring (root ).decode ().replace ("ns0:" , "" ).replace (":ns0" , "" ).strip ()
77
+ )
76
78
77
79
removed_namespaces = process_xml_namespace (xml_string )
78
80
data = element_to_dict (removed_namespaces )
@@ -99,11 +101,11 @@ def element_to_dict(element):
99
101
100
102
children_by_tag = {}
101
103
for child_element in element :
102
- child_key = child_element .tag + 's'
104
+ child_key = child_element .tag + "s"
103
105
child_value = element_to_dict (child_element )
104
106
105
107
# Check if the child element has an 'id' attribute
106
- if 'id' in child_element .attrib :
108
+ if "id" in child_element .attrib :
107
109
# If the child element has an 'id', add it to the result dictionary directly
108
110
result [child_key ] = child_value
109
111
else :
@@ -121,10 +123,10 @@ def process_xml_namespace(xml_string):
121
123
ignored_elements = [
122
124
'xmlns="http://www.neuroml.org/schema/neuroml2"' ,
123
125
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' ,
124
- 'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"'
126
+ 'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"' ,
125
127
]
126
128
for ignored_element in ignored_elements :
127
- xml_string = xml_string .replace (ignored_element , '' ).strip ()
129
+ xml_string = xml_string .replace (ignored_element , "" ).strip ()
128
130
129
131
# Parse the XML string into an ElementTree
130
132
root = ET .fromstring (xml_string )
@@ -242,7 +244,10 @@ def build_xml_element(data, parent=None):
242
244
for child in children :
243
245
child_element = build_xml_element (child )
244
246
parent .append (child_element )
245
- elif not any (hasattr (data , attr_name ) for attr_name in ["xmlns" , "xmlns_url" , "xmlns_loc" , "xmln_loc_2" ]):
247
+ elif not any (
248
+ hasattr (data , attr_name )
249
+ for attr_name in ["xmlns" , "xmlns_url" , "xmlns_loc" , "xmln_loc_2" ]
250
+ ):
246
251
attribute_name = aattr .name
247
252
attribute_value = data .__getattribute__ (aattr .name )
248
253
parent .set (attribute_name , str (attribute_value ))
@@ -252,12 +257,11 @@ def build_xml_element(data, parent=None):
252
257
if hasattr (data , "xmlns_url" ):
253
258
parent .set ("xmlns:xsi" , data .xmlns_url )
254
259
if hasattr (data , "xmlns_loc" ):
255
- parent .set ("xsi:schemaLocation" , str (data .xmlns_loc + ' \n ' + data .xmln_loc_2 ))
260
+ parent .set ("xsi:schemaLocation" , str (data .xmlns_loc + " \n " + data .xmln_loc_2 ))
256
261
257
262
return parent
258
263
259
264
260
-
261
265
def ascii_encode_dict (data ):
262
266
ascii_encode = (
263
267
lambda x : x .encode ("ascii" )
0 commit comments