Skip to content

Commit efb2712

Browse files
precommited and added better code description
1 parent 43a5ade commit efb2712

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

examples/neuroml2/neuroml2_spec.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ class neuroml(Base):
116116
validator=instance_of(str), default="http://www.neuroml.org/schema/neuroml2"
117117
)
118118
xmln_loc_2: str = field(
119-
validator=instance_of(str), default="https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"
119+
validator=instance_of(str),
120+
default="https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd",
120121
)
121122

122123
izhikevich2007Cells: List[izhikevich2007Cell] = field(factory=list)
@@ -198,5 +199,6 @@ class neuroml(Base):
198199
d.write(yy)
199200

200201
from modelspec.utils import load_xml
201-
new_neuroml = load_xml('hello_world_neuroml.net.nml')
202+
203+
new_neuroml = load_xml("hello_world_neuroml.net.nml")
202204
print(new_neuroml)

src/modelspec/utils.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ def load_xml(filename: str):
7272
root = tree.getroot() # Get the root element
7373

7474
# 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+
)
7678

7779
removed_namespaces = process_xml_namespace(xml_string)
7880
data = element_to_dict(removed_namespaces)
@@ -99,11 +101,11 @@ def element_to_dict(element):
99101

100102
children_by_tag = {}
101103
for child_element in element:
102-
child_key = child_element.tag + 's'
104+
child_key = child_element.tag + "s"
103105
child_value = element_to_dict(child_element)
104106

105107
# Check if the child element has an 'id' attribute
106-
if 'id' in child_element.attrib:
108+
if "id" in child_element.attrib:
107109
# If the child element has an 'id', add it to the result dictionary directly
108110
result[child_key] = child_value
109111
else:
@@ -121,10 +123,10 @@ def process_xml_namespace(xml_string):
121123
ignored_elements = [
122124
'xmlns="http://www.neuroml.org/schema/neuroml2"',
123125
'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"',
125127
]
126128
for ignored_element in ignored_elements:
127-
xml_string = xml_string.replace(ignored_element, '').strip()
129+
xml_string = xml_string.replace(ignored_element, "").strip()
128130

129131
# Parse the XML string into an ElementTree
130132
root = ET.fromstring(xml_string)
@@ -242,7 +244,10 @@ def build_xml_element(data, parent=None):
242244
for child in children:
243245
child_element = build_xml_element(child)
244246
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+
):
246251
attribute_name = aattr.name
247252
attribute_value = data.__getattribute__(aattr.name)
248253
parent.set(attribute_name, str(attribute_value))
@@ -252,12 +257,11 @@ def build_xml_element(data, parent=None):
252257
if hasattr(data, "xmlns_url"):
253258
parent.set("xmlns:xsi", data.xmlns_url)
254259
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))
256261

257262
return parent
258263

259264

260-
261265
def ascii_encode_dict(data):
262266
ascii_encode = (
263267
lambda x: x.encode("ascii")

0 commit comments

Comments
 (0)