473,586 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cElementTree clear semantics


Hi,
I am trying to understand how cElementTree's clear works: I have a
(relatively) large XML file, that I do not wish to load into memory.
So, naturally, I tried something like this:

from cElementTree import iterparse
for event, elem in iterparse("data .xml"):
if elem.tag == "schnappi":
count += 1
elem.clear()

.... which resulted in caching of all elements in memory except for
those named <schnappi> (i.e. the process' memory footprint grew more
and more). Then I though about clear()'ing all elements that I did not
really need:

from cElementTree import iterparse
for event, elem in iterparse("data .xml"):
if elem.tag == "schnappi":
count += 1
elem.clear()

.... which gave a suitably small memory footprint, *BUT* since
<schnappi> has a number of subelements, and I subscribe to
'end'-events, the <schnappi> element is returned after all of its
subelements have been read and clear()'ed. So, I see indeed a
<schnappi> element, but calling its getiterator() gives me completely
empty subelements, which is not what I wanted :(

Finally, I thought about keeping track of when to clear and when not
to by subscribing to start and end elements (so that I would collect
the entire <schnappi>-subtree in memory and only than release it):

from cElementTree import iterparse
clear_flag = True
for event, elem in iterparse("data .xml", ("start", "end")):
if event == "start" and elem.tag == "schnappi":
# start collecting elements
clear_flag = False
if event == "end" and elem.tag == "schnappi":
clear_flag = True
# do something with elem
# unless we are collecting elements, clear()
if clear_flag:
elem.clear()

This gave me the desired behaviour, but:

* It looks *very* ugly
* It's twice as slow as version which sees 'end'-events only.

Now, there *has* to be a better way. What am I missing?

Thanks in advance,

ivr
--
"...but it's HDTV -- it's got a better resolution than the real world."
-- Fry, "When aliens attack"
Sep 25 '05 #1
27 1956
D H
Igor V. Rafienko wrote:
This gave me the desired behaviour, but:

* It looks *very* ugly
* It's twice as slow as version which sees 'end'-events only.

Now, there *has* to be a better way. What am I missing?


Try emailing the author for support.
Sep 25 '05 #2
D H wrote:
Igor V. Rafienko wrote:
This gave me the desired behaviour, but:

* It looks *very* ugly
* It's twice as slow as version which sees 'end'-events only.

Now, there *has* to be a better way. What am I missing?


Try emailing the author for support.


I don't think that's needed. He is one of the most active members
of c.l.py, and you should know that yourself.

Reinhold
Sep 25 '05 #3
D H
Reinhold Birkenfeld wrote:
D H wrote:
Igor V. Rafienko wrote:
This gave me the desired behaviour, but:

* It looks *very* ugly
* It's twice as slow as version which sees 'end'-events only.

Now, there *has* to be a better way. What am I missing?


Try emailing the author for support.

I don't think that's needed. He is one of the most active members
of c.l.py, and you should know that yourself.


I would recommend emailing the author of a library when you have a
question about that library. You should know that yourself as well.
Sep 25 '05 #4
D H wrote:
Reinhold Birkenfeld wrote:
D H wrote:
Igor V. Rafienko wrote:

This gave me the desired behaviour, but:

* It looks *very* ugly
* It's twice as slow as version which sees 'end'-events only.

Now, there *has* to be a better way. What am I missing?
Try emailing the author for support.

I don't think that's needed. He is one of the most active members
of c.l.py, and you should know that yourself.


I would recommend emailing the author of a library when you have a
question about that library. You should know that yourself as well.


Well, if I had e.g. a question about Boo, I would of course first ask
here because I know the expert writes here.

Reinhold
Sep 25 '05 #5
D H
Reinhold Birkenfeld wrote:

Well, if I had e.g. a question about Boo, I would of course first ask
here because I know the expert writes here.

Reinhold
Reinhold Birkenfeld also wrote: If I had wanted to say "you have opinions? fuck off!", I would have said
"you have opinions? fuck off!".

Take your own advice asshole.
Sep 25 '05 #6
D H
D H wrote:
Reinhold Birkenfeld wrote:

Well, if I had e.g. a question about Boo, I would of course first ask
here because I know the expert writes here.

Reinhold

Reinhold Birkenfeld also wrote:
> If I had wanted to say "you have opinions? fuck off!", I would have said
>"you have opinions? fuck off!".

Take your own advice asshole.


Sep 25 '05 #7
D H wrote:
Reinhold Birkenfeld wrote:

Well, if I had e.g. a question about Boo, I would of course first ask
here because I know the expert writes here.

Reinhold


Reinhold Birkenfeld also wrote:
> If I had wanted to say "you have opinions? fuck off!", I would have said
>"you have opinions? fuck off!".

Take your own advice asshole.


QED. Irony tags for sale.

Reinhold
Sep 25 '05 #8
D H wrote:
D H wrote:
Reinhold Birkenfeld wrote:

Well, if I had e.g. a question about Boo, I would of course first ask
here because I know the expert writes here.

Reinhold

Reinhold Birkenfeld also wrote:
> If I had wanted to say "you have opinions? fuck off!", I would have said
>"you have opinions? fuck off!".

Take your own advice asshole.


And what's that about?

Reinhold
Sep 25 '05 #9
D H
Reinhold Birkenfeld wrote:
D H wrote:
D H wrote:
Reinhold Birkenfeld wrote:
Well, if I had e.g. a question about Boo, I would of course first ask
here because I know the expert writes here.

Reinhold
Reinhold Birkenfeld also wrote:
> If I had wanted to say "you have opinions? fuck off!", I would have said
>"you have opinions? fuck off!".
Take your own advice asshole.

And what's that about?


I think it means you should fuck off, asshole.
Sep 25 '05 #10

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

Similar topics

1
1764
by: Kent Johnson | last post by:
Is it possible to subclass cElementTree.Element? I tried >>> import cElementTree as et >>> class Elt(et.Element): ... pass ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: Error when calling the metaclass bases cannot create 'builtin_function_or_method' instances
10
7060
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to clear(), in DOT.NET the capacity() is reduced to 0.
3
1879
by: Diez B. Roggisch | last post by:
Hi, I've got to deal with a pretty huge XML-document, and to do so I use the cElementTree.iterparse functionality. Working great. Only trouble: The guys creating that chunk of XML - well, lets just say they are "encodingly challanged", so they don't produce utf-8, but only cp1252 instead, together with some weird name (Windows-1252) for...
4
10270
by: Russell Warren | last post by:
I'm guessing no, since it skips down through any Lock semantics, but I'm wondering what the best way to clear a Queue is then. Esentially I want to do a "get all" and ignore what pops out, but I don't want to loop through a .get until empty because that could potentially end up racing another thread that is more-or-less blindly filling it...
0
926
by: Mark | last post by:
I have an elementtree created with cElementTree. I then use ElementInclude to resolve some xinclude elements. But then I want to move those included elements to be children of the root root.append(included_child) but I get an error message TypeError: 'append() argument 1 must be Element, not instance'
0
1266
by: Mark | last post by:
-------- Original Message -------- Subject: Using cElementTree and elementtree.ElementInclude Date: Mon, 23 Oct 2006 09:40:24 -0500 From: Mark E. Smith <mark.e.smith@arnold.af.mil> Organization: AEDC To: python-list@python.org
1
1351
by: Piet van Oostrum | last post by:
I have just installed Python 2.5 on Mac OS X 10.4.8 on an iBook (PPC) from the dmg. Now I tried to install cElementTree -1.0.5-20 from source (no egg available in cheeseshop) and got the following compilation error: gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -Wno-long-double -no-cpp-precomp...
2
2193
by: mukappa | last post by:
I found an earlier post about subclassing cElementTree.Element which can't be done because it is a factory method. I am trying to subclass XMLTreeBuilder with success using the python implementation, but not with cElementTree. $ python Python 2.3.4 (#1, Feb 22 2005, 04:09:37) on linux2
1
2164
by: Barry | last post by:
I recently tried switching from ElementTree to cElementTree. My application parses a collection of large XML files and creates indexes based on certain attributes. This entire collection is saved as an instance of my Database class. Using ElementTree and cPickle has allowed me to save these instances and use them later. Using...
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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
8202
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
8338
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...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5390
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.