From e90064466df857ba51bd916890e458e51745bb10 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 17 Apr 2024 21:23:26 +0200 Subject: [PATCH] better import name --- converter.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/converter.py b/converter.py index 0ab10de..825dc11 100644 --- a/converter.py +++ b/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: