better import name
This commit is contained in:
parent
9d9cacb83d
commit
e90064466d
1 changed files with 15 additions and 13 deletions
28
converter.py
28
converter.py
|
@ -2,7 +2,9 @@ from argparse import ArgumentParser
|
|||
from csv import reader as csv_reader
|
||||
from pprint import pformat
|
||||
from xml.dom.minidom import parseString as xml_parser
|
||||
from xml.etree import ElementTree
|
||||
from xml.etree.ElementTree import Element as XMLElement
|
||||
from xml.etree.ElementTree import SubElement as XMLSubElement
|
||||
from xml.etree.ElementTree import tostring as element_stringifier
|
||||
|
||||
|
||||
class System:
|
||||
|
@ -49,28 +51,28 @@ class System:
|
|||
def __repr__(self):
|
||||
return pformat(self.__dict__, compact=True, width=120, sort_dicts=False)
|
||||
|
||||
def to_xml(self) -> ElementTree.Element:
|
||||
def to_xml(self) -> XMLElement:
|
||||
"""Convert the current system into an XML element"""
|
||||
system = ElementTree.Element("lsystem")
|
||||
system = XMLElement("lsystem")
|
||||
|
||||
base = ElementTree.SubElement(system, "name")
|
||||
base = XMLSubElement(system, "name")
|
||||
base.text = self.name
|
||||
|
||||
base = ElementTree.SubElement(system, "base")
|
||||
base = XMLSubElement(system, "base")
|
||||
base.text = "".join(self.base)
|
||||
|
||||
axiom = ElementTree.SubElement(system, "axiom")
|
||||
axiom = XMLSubElement(system, "axiom")
|
||||
axiom.text = self.axiom
|
||||
|
||||
substitutions = ElementTree.SubElement(system, "substitutions")
|
||||
substitutions = XMLSubElement(system, "substitutions")
|
||||
for member, substitution in self.substitutions:
|
||||
sub_element = ElementTree.SubElement(substitutions, "substitution")
|
||||
sub_element = XMLSubElement(substitutions, "substitution")
|
||||
sub_element.set("member", member)
|
||||
sub_element.text = substitution
|
||||
|
||||
interpretations = ElementTree.SubElement(system, "interpretations")
|
||||
interpretations = XMLSubElement(system, "interpretations")
|
||||
for member, interpretation in self.interpretations:
|
||||
inter_element = ElementTree.SubElement(interpretations, "interpretation")
|
||||
inter_element = XMLSubElement(interpretations, "interpretation")
|
||||
inter_element.set("member", member)
|
||||
inter_element.text = interpretation
|
||||
|
||||
|
@ -96,10 +98,10 @@ def data_reader(path: str, delimiter: str = ","):
|
|||
return res
|
||||
|
||||
|
||||
def lsystems_xml(systems: list[System]) -> ElementTree.Element:
|
||||
def lsystems_xml(systems: list[System]) -> XMLElement:
|
||||
"""Convert list of L-system structure into XML"""
|
||||
|
||||
root = ElementTree.Element("lsystems")
|
||||
root = XMLElement("lsystems")
|
||||
|
||||
for system in systems:
|
||||
root.append(system.to_xml())
|
||||
|
@ -125,7 +127,7 @@ if __name__ == "__main__":
|
|||
xml = lsystems_xml(lsystems)
|
||||
|
||||
# Output XML
|
||||
dom = xml_parser(ElementTree.tostring(xml))
|
||||
dom = xml_parser(element_stringifier(xml))
|
||||
pretty_xml = dom.toprettyxml(encoding="UTF-8").decode()
|
||||
if args.output:
|
||||
with open(args.output, "w") as output_file:
|
||||
|
|
Reference in a new issue