473,545 Members | 1,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Appending Elements in Element Tree


In my program I have a lot of statements that append elements, but
sometimes I don't want to append the element so it requres an if
statement to check it, and that requires assigning the returned element
from a function to a name or calling the funtion twice.

e = ET.Element('nam e')
e.append(get_su belement(obj)) # but raises an exception on None

This works, but I have a lot of appends.

sube = get_element(obj )
if sube is None:
e.append(sube)

Now if Elements worked more like lists I could extend an element.

alist = []
alist.extend([]) # returns orginal list

With strings.

s = ''
s += '' # returns original string (or copy)
So is there an identity operation with Element Tree elements that I can
use to avoid putting a lot of if and or try statements around appending
elements?

I could wrap the append and do it this way...

def eappend(e1, e2):
if e2 is None: return e1
e1.append(e2)
return e1

Then do...

e = eappend(e, get_element(obj )) # Append if not None.
But maybe there's a better way?
Ron

Apr 8 '06 #1
1 2284
Ron Adam wrote:

In my program I have a lot of statements that append elements, but
sometimes I don't want to append the element so it requres an if
statement to check it, and that requires assigning the returned element
from a function to a name or calling the funtion twice.

e = ET.Element('nam e')
e.append(get_su belement(obj)) # but raises an exception on None

This works, but I have a lot of appends.

sube = get_element(obj )
if sube is None:
e.append(sube)

Now if Elements worked more like lists I could extend an element.

alist = []
alist.extend([]) # returns orginal list

With strings.

s = ''
s += '' # returns original string (or copy)
So is there an identity operation with Element Tree elements that I can
use to avoid putting a lot of if and or try statements around appending
elements?

I could wrap the append and do it this way...

def eappend(e1, e2):
if e2 is None: return e1
e1.append(e2)
return e1

Then do...

e = eappend(e, get_element(obj )) # Append if not None.
But maybe there's a better way?


The following worked in cases where I needed to append a list of items.

def extend(e1, elist):
for e in elist:
e1.append(e)
return e1
Although I still need to return the list as elements aren't mutable.

element = extend(element, elist)

It would be nice if this was built in so I could do... Hint hint. :-)

element.extend( elist)

Where elist is a list of elements or an element with a number of sub
elements.
I still need to special case individual appends or put the returned item
in a list so I use the extend() function on the returned element.

I guess I need to look at the Element Tree source code.

Cheers,
Ron



Apr 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1321
by: Ruthless | last post by:
Hello. I've got a question. I've got family tree in XML. It looks like this <generation level="2"> <single><person>...</single> <marriage><preson>...</person><person>....</person></marriage> <person> inside single and marriage is the same element,
9
8812
by: Stanimir Stamenkov | last post by:
Using Xerces2 Java I'm trying to validate a CSV data following an XNI example <http://xml.apache.org/xerces2-j/xni-config.html#examples>. The CSV scanner generates XML tree events like: <csv> <row> <col>Andy Clark</col> <col>16 Jan 1973</col> <col>Cincinnati</col>
9
1942
by: Bernd.Moos | last post by:
Given the following XML document: <text> <p> <w>Ronaldo</w> <w>scoredw> <w>the</w> <w>1</w> <c>:</c> <w>1</w>
2
3197
by: RobG | last post by:
Why does Firefox insert #text nodes as children of TR elements? As a work-around for older Safari versions not properly supporting a table row's cells collection, I used the row's childNodes collection as it was pretty much exactly the same thing. However, in Firefox 1.0.7 text nodes are inserted between the TDs. I'm certain that this...
6
5281
by: jmike | last post by:
Hi everyone, I am a total newbie to XML parsing. I've written a couple of toy examples under the instruction of tutorials available on the web. The problem I want to solve is this. I have an XML snippet (in a string) that looks like this: <booga foo="1" bar="2"> <well>hello</well>
5
5549
by: toton | last post by:
Hi, I want to append one vector after another. so like, vector<intv1; ///put some elements to v2. I have a second vector vector<intv2; ///it has some elements. Now I do v1.reserve(v1.size() + v2.size()) ; and then
7
9471
by: Daz | last post by:
Hi everyone! Is it possible to take a line of text like so: <tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr> And append it to a DOM node such as this: var nodeToAppendTo = document.getElementById('tbody');
7
2319
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. Is that so?
2
11232
by: Gentr1 | last post by:
Hi everybody! I am presently working on a Genetic Programming API in python. I have a bit of a problem at the moment... For some specific reasons, I am using nested lists data structure to represent a tree. The first element of a list is always the head of the node, while all others represent the tail of the node. e.g. the nested list , E],...
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.