xml application advice

W

William Purcell

I am writing a application to calculate pressure drop for a piping
network. Namely a building sprinkler system. This will be a
command line program at first with the system described in xml (at
least that is how I think I want to do it).

An important part of this calculation is finding the 'hydraulically
most remote' sprinkler. This is something that I could specify with
an attribute for now and later think about how to automate it. I
need to walk through the dom tree until I find a node of type
"sprinkler" that has an attribute of hydraulically_most_remote with
a value of True.

After I find this I need to break the itterator/for loop and then
start walking backwards keeping a running total of the pressure drop
until I reach a node that has multiple pipesections and then walk to
the end of each branch and calculate the pressure drop, and then add
them to the branch that contained the hydraulically most remote
sprinkler, and then move on, repeating this until I walk all the way
back to the inflow node.

I am having trouble finding a decent python/xml resource on the web.
I have ordered Python & XML by Jones and Drake, but I am anxious to
get something started. The only decent online resource that I can
seem to find is

http://pyxml.sourceforge.net/topics/howto/xml-howto.html

which doesn't seem to be a very comprehensive how-to.

Do demonstrate just about everything I know about xml and python I
attached t.py and ex.xml.

Another thing that is confusing is dir(walker) does not show walker
having an attribute currentNode and dir(walker.currentNode) does not
show walker.currentNode having an attribute tagName.

Bill
 
D

Diez B. Roggisch

William said:
I am writing a application to calculate pressure drop for a piping
network. Namely a building sprinkler system. This will be a
command line program at first with the system described in xml (at
least that is how I think I want to do it).

An important part of this calculation is finding the 'hydraulically
most remote' sprinkler. This is something that I could specify with
an attribute for now and later think about how to automate it. I
need to walk through the dom tree until I find a node of type
"sprinkler" that has an attribute of hydraulically_most_remote with
a value of True.

After I find this I need to break the itterator/for loop and then
start walking backwards keeping a running total of the pressure drop
until I reach a node that has multiple pipesections and then walk to
the end of each branch and calculate the pressure drop, and then add
them to the branch that contained the hydraulically most remote
sprinkler, and then move on, repeating this until I walk all the way
back to the inflow node.

I am having trouble finding a decent python/xml resource on the web.
I have ordered Python & XML by Jones and Drake, but I am anxious to
get something started. The only decent online resource that I can
seem to find is

http://pyxml.sourceforge.net/topics/howto/xml-howto.html

which doesn't seem to be a very comprehensive how-to.

Do demonstrate just about everything I know about xml and python I
attached t.py and ex.xml.

Another thing that is confusing is dir(walker) does not show walker
having an attribute currentNode and dir(walker.currentNode) does not
show walker.currentNode having an attribute tagName.

Use lxml2 and xpath.

http://codespeak.net/lxml/
http://codespeak.net/lxml/xpathxslt.html

See the below piece of code to get you started:

import lxml.etree as et

xml = """<?xml version="1.0"?>
<project name="test">
<inflow static="60" residual="20">
<pipesection diameter="1.05" length="10">
<node id="H1" k="5.6" type="sprinkler">
<pipesection diameter="1.05" length="4">
<node id="1" type="T">
<pipesection diameter="1.05" length="6">
<node id="H2" hydraulically_most_remote="True">
</node>
</pipesection>
<pipesection diameter="1.05" length="5">
<node id="H3">
</node>
</pipesection>
</node>
</pipesection>
</node>
</pipesection>
</inflow>
</project>"""



project = et.fromstring(xml)


hydraulically_most_remote =
project.xpath("//node[@hydraulically_most_remote='True']")[0]
print hydraulically_most_remote.attrib["id"]

# find node with multiple pipesections that's upwards

def find_mp_node(node):
pipesections = node.xpath("pipesection")
if len(pipesections) > 1:
return node
parent = node.getparent()
if parent is not None:
return find_mp_node(parent)

print find_mp_node(hydraulically_most_remote).attrib["id"]
 
P

Peter Otten

William said:
I am writing a application to calculate pressure drop for a piping
network. Namely a building sprinkler system. This will be a
command line program at first with the system described in xml (at
least that is how I think I want to do it).

An important part of this calculation is finding the 'hydraulically
most remote' sprinkler. This is something that I could specify with
an attribute for now and later think about how to automate it. I
need to walk through the dom tree until I find a node of type
"sprinkler" that has an attribute of hydraulically_most_remote with
a value of True.

After I find this I need to break the itterator/for loop and then
start walking backwards keeping a running total of the pressure drop
until I reach a node that has multiple pipesections and then walk to
the end of each branch and calculate the pressure drop, and then add
them to the branch that contained the hydraulically most remote
sprinkler, and then move on, repeating this until I walk all the way
back to the inflow node.

I am having trouble finding a decent python/xml resource on the web.
I have ordered Python & XML by Jones and Drake, but I am anxious to
get something started. The only decent online resource that I can
seem to find is

http://pyxml.sourceforge.net/topics/howto/xml-howto.html

which doesn't seem to be a very comprehensive how-to.

Do demonstrate just about everything I know about xml and python I
attached t.py and ex.xml.

Another thing that is confusing is dir(walker) does not show walker
having an attribute currentNode and dir(walker.currentNode) does not
show walker.currentNode having an attribute tagName.

I'd probably start with a few python classes representing the sprinkler
system. The exact layout may change a few times until you have found one
that makes your questions clear and the calculations as easy as possible.

You can then add a read_model_from_file() function converting the xml into
your model using ElementTree or its close relative lxml.

My guess is that it'll be a lot more fun this way...

Peter
 
W

William Purcell


This looks promising. I will start playing around with it and see
what I can come up with. Thanks for the example.

Peter said:
I'd probably start with a few python classes representing the
sprinkler
system. The exact layout may change a few times until you have
found one
that makes your questions clear and the calculations as easy as
possible.

You can then add a read_model_from_file() function converting the
xml into
your model using ElementTree or its close relative lxml.

My guess is that it'll be a lot more fun this way...

This was my initial plan, but I have never messed with xml and
didn't know if it was what I wanted. I have messed around with
plistlib on a mac. If I remember correctly the reader in plistlib
returns a dict so I thought I would be getting a dict from an xml
reader (but maybe xml and plist aren't as close as I thought).
Reading xml seems more complicated than I initially expected, but
probably rightfully so.

But I digress.

I will take your advice and start with some classes and then work on
getting the data to my classes.
 
J

Jorgen Grahn

I am writing a application to calculate pressure drop for a piping
network. Namely a building sprinkler system. This will be a
command line program at first with the system described in xml (at
least that is how I think I want to do it).

How about (re)using the dot graph language from Graphviz? It's a file
format for describing directed graphs, which I suppose a sprinkler
system is. It might fit; it might not.
An important part of this calculation is finding the 'hydraulically
most remote' sprinkler. This is something that I could specify with
an attribute for now and later think about how to automate it. I
need to walk through the dom tree until I find a node of type
"sprinkler" that has an attribute of hydraulically_most_remote with
a value of True.

After I find this I need to break the itterator/for loop and then
start walking backwards keeping a running total of the pressure drop
until I reach a node that has multiple pipesections and then walk to
the end of each branch and calculate the pressure drop, and then add
them to the branch that contained the hydraulically most remote
sprinkler, and then move on, repeating this until I walk all the way
back to the inflow node.

I am having trouble finding a decent python/xml resource on the web.
I have ordered Python & XML by Jones and Drake, but I am anxious to
get something started.

If what you're interested in is to get real work done, why decide to
make XML a showstopper?

I see two tasks: (a) transforming a text file description of a sprinkler
system into a Python data structure, and (b) implementing algorithms
to find out important stuff about such a data structure.

You do not need (a) before you can do (b). You can even have Python as
your input format, and eval() the file. Crude and insecure, but it
works, at almost zero cost.

/Jorgen
 
W

William Purcell

Scott said:
William said:
I am writing a application to calculate pressure drop for a piping
network. Namely a building sprinkler system. This will be a
command line program at first with the system described in xml....

If you are going to be doing a lot of tree walking, try etree.
Simple example:

import xml.etree.ElementTree as ET # or wherever you get ElementTree

def find_remote_and_path(node, path):
for child in node:
for result in walks(child, path + [node]): yield result
if node.tag == 'node' and node.get('hydraulically_most_remote'
) == 'True':
yield node, path


tree = ET.parse('ex.xml')
for node, path in find_remote_and_path(tree.getroot(), []):
for t in path:
print ' ', t.tag, t.get('id', '-')
print node.tag, ', '.join(sorted('%s=%r' % pair
for pair in node.items()))


--Scott David Daniels
(e-mail address removed)

Scott, Thanks for the reply.

I am having a little trouble finding where to import `walks` from.

Bill
 
D

Diez B. Roggisch

If what you're interested in is to get real work done, why decide to
make XML a showstopper?

I see two tasks: (a) transforming a text file description of a sprinkler
system into a Python data structure, and (b) implementing algorithms
to find out important stuff about such a data structure.

You do not need (a) before you can do (b). You can even have Python as
your input format, and eval() the file. Crude and insecure, but it
works, at almost zero cost.

While I certainly agree that XML often means more trouble than it's worth, I
don't concur in this concrete case. If the OP has a tree-structure, XPath
is a *very* powerful way to operate on that - and as he seems to have
questions like "get the one node with the attribute X, then the first one
above that having more than one child of kind Y", it can get in handy.

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top