Re: [xml] faster replacement libxml.py for python 2.2



Daniel Veillard kirjutas P, 05.01.2003 kell 01:30:
On Sat, Jan 04, 2003 at 05:08:06PM +0500, Hannu Krosing wrote:

I have modified libxml.py (the handmade part of libxml2.py) to make 
use of some new features of python 2.2 , namely

1) replaced __gettattr__() with property()

  Okay, I integrated both using
    import sys
    if float(sys.version[0:3]) < 2.2:
        def __getattr__(self, attr):
            ... existing libxml.py code ...
    else:
        ... your property based definitions ...

  see the enclosed result.

2) added an __iter__() method that returns an iterator over subtree of
node

  That I couldn't make to work over 1.5 and 2.1 :

It will not work on 1.5 as it just does not support iterators.

It _may_ work with 2.1 with some "from __future__ import xxx2 stuff

veillard:~/XML/python -> python1.5 tst.py
Traceback (innermost last):
  File "tst.py", line 17, in ?
    nodecount(rec)
  File "tst.py", line 10, in nodecount
    for node in doc:
  File "libxml2.py", line 307, in __getattr__
    raise AttributeError,attr
AttributeError: __getitem__
veillard:~/XML/python ->

  It seems that defining __getitem__ along to __getattr__ for the
old code would allow to provide that facility on the older framework
and I would really prefer that. Maybe reuse the iterator is possible
it should just be a bit of glue it seems.

The result of replacing __gettattr__() with property() is a 2x speedup
when iterating over tree using node.next and node.children

An additional 12% speedup can be gained by not having a separate xmlCore
class (it is used only by xmlNode) but including the hand-made part
directly in definition of class xmlNode.

    Well, that's harder due to the autogeneration of xmlNode,

I was thinking about generating a chunk of code that would be inserted
into the xmlNode in the phase that merges the two (handmade and
generated) parts.


-- 
Hannu Krosing <hannu tm ee>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]