Skip to content

Commit

Permalink
Fix: Writing xml as a formatted file
Browse files Browse the repository at this point in the history
  • Loading branch information
Grabt234 committed Apr 7, 2024
1 parent 4053638 commit 2cea8b5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
13 changes: 10 additions & 3 deletions config_generators/fersxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,19 @@ def define_dtd_file(self, name):
self.dtd = doctype_declaration = "<!DOCTYPE simulation SYSTEM \""+name+"\" >"

def write_to_file(self, file_path):
# Write the XML tree to a file with the DOCTYPE declaration

# Parse the XML string
import xml.dom.minidom
import xml.etree.ElementTree as ET

xml_string = ET.tostring(self.simulation, encoding='utf-8', xml_declaration=True)
dom = xml.dom.minidom.parseString(xml_string)
pretty_xml = dom.toprettyxml(indent=" ")

with open(file_path, "wb") as f:
f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".encode('utf-8'))
f.write(self.dtd.encode('utf-8'))
tree = ET.ElementTree(self.simulation)
tree.write(f)
f.write(pretty_xml.encode('utf-8'))

# only contains single reflector
class SimpleStaticIsotropicTargetPlatform:
Expand Down
67 changes: 50 additions & 17 deletions config_generators/tutorial_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -64,7 +64,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -136,7 +136,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -169,13 +169,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Define signals which may be used by platforms\n",
"SimulationConfiguration.define_pulse(\"pulse1\",file_name,\"1\", \"1\",\"1000000\")\n"
"SimulationConfiguration.define_pulse(\"pulse1\",file_name,\"1\", \"1\",\"1000000\")"
]
},
{
Expand All @@ -198,7 +197,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -231,7 +230,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -248,7 +247,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -258,9 +257,25 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "TypeError",
"evalue": "a bytes-like object is required, not 'ElementTree'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mSimulationConfiguration\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwrite_to_file\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtmp.xml\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/SenseScape/FERS/config_generators/fersxml.py:466\u001b[0m, in \u001b[0;36mSimulationConfiguration.write_to_file\u001b[0;34m(self, file_path)\u001b[0m\n\u001b[1;32m 461\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mwrite_to_file\u001b[39m(\u001b[38;5;28mself\u001b[39m, file_path):\n\u001b[1;32m 462\u001b[0m \n\u001b[1;32m 463\u001b[0m \u001b[38;5;66;03m# Parse the XML string\u001b[39;00m\n\u001b[1;32m 464\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mxml\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdom\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mminidom\u001b[39;00m\n\u001b[0;32m--> 466\u001b[0m dom \u001b[38;5;241m=\u001b[39m \u001b[43mxml\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdom\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mminidom\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mparseString\u001b[49m\u001b[43m(\u001b[49m\u001b[43mET\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mElementTree\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msimulation\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 468\u001b[0m \u001b[38;5;66;03m# Pretty-print the XML document\u001b[39;00m\n\u001b[1;32m 469\u001b[0m pretty_xml \u001b[38;5;241m=\u001b[39m dom\u001b[38;5;241m.\u001b[39mtoprettyxml(indent\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"File \u001b[0;32m/usr/lib/python3.10/xml/dom/minidom.py:2000\u001b[0m, in \u001b[0;36mparseString\u001b[0;34m(string, parser)\u001b[0m\n\u001b[1;32m 1998\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m parser \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 1999\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mxml\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdom\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m expatbuilder\n\u001b[0;32m-> 2000\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mexpatbuilder\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mparseString\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstring\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2001\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 2002\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mxml\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdom\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m pulldom\n",
"File \u001b[0;32m/usr/lib/python3.10/xml/dom/expatbuilder.py:925\u001b[0m, in \u001b[0;36mparseString\u001b[0;34m(string, namespaces)\u001b[0m\n\u001b[1;32m 923\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 924\u001b[0m builder \u001b[38;5;241m=\u001b[39m ExpatBuilder()\n\u001b[0;32m--> 925\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mbuilder\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mparseString\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstring\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m/usr/lib/python3.10/xml/dom/expatbuilder.py:223\u001b[0m, in \u001b[0;36mExpatBuilder.parseString\u001b[0;34m(self, string)\u001b[0m\n\u001b[1;32m 221\u001b[0m parser \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgetParser()\n\u001b[1;32m 222\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 223\u001b[0m \u001b[43mparser\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mParse\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstring\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 224\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_setup_subset(string)\n\u001b[1;32m 225\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m ParseEscape:\n",
"\u001b[0;31mTypeError\u001b[0m: a bytes-like object is required, not 'ElementTree'"
]
}
],
"source": [
"SimulationConfiguration.write_to_file(\"tmp.xml\")"
]
Expand All @@ -269,14 +284,28 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a couple of convenience functions."
"### Run a Simulation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a couple of convenience functions."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def lin2db(lin):\n",
" '''\n",
Expand Down Expand Up @@ -308,14 +337,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Noise density: -173.98 [dBm/Hz]\n"
"ename": "NameError",
"evalue": "name 'BOLTZMANN' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[12], line 11\u001b[0m\n\u001b[1;32m 9\u001b[0m tx_power \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m \u001b[38;5;66;03m# watt\u001b[39;00m\n\u001b[1;32m 10\u001b[0m noise_temp \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m290\u001b[39m\n\u001b[0;32m---> 11\u001b[0m noise_density \u001b[38;5;241m=\u001b[39m \u001b[43mBOLTZMANN\u001b[49m \u001b[38;5;241m*\u001b[39m noise_temp \u001b[38;5;66;03m# [W/Hz]\u001b[39;00m\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mNoise density:\u001b[39m\u001b[38;5;124m'\u001b[39m, np\u001b[38;5;241m.\u001b[39mround(lin2db(noise_density) \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m30\u001b[39m, \u001b[38;5;241m2\u001b[39m), \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m[dBm/Hz]\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 15\u001b[0m prf \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1000\u001b[39m \u001b[38;5;66;03m# Hz\u001b[39;00m\n",
"\u001b[0;31mNameError\u001b[0m: name 'BOLTZMANN' is not defined"
]
}
],
Expand Down

0 comments on commit 2cea8b5

Please sign in to comment.