[lxml-dev] Bug in objectify node[:].index

Christian Zagrodnick cz at gocept.com
Wed May 23 08:50:39 CEST 2007


Hi,

following little script fails at the last assert:

----------------------------
import lxml.objectify


tree = lxml.objectify.fromstring(
"""\
    <root>
      <a name="car">foo</a>
      <a name="car">bar</a>
      <a name="tree">foo</a>
      <a name="tree">bar</a>
    </root>
""")

trees = tree.findall('//a[@name="tree"]')
print trees

foo_tree = trees[0]
assert foo_tree.get('name') == 'tree'

parent = foo_tree.getparent()
assert parent.tag == 'root'


node_list = parent[foo_tree.tag]
import pdb; pdb.set_trace()
foo_index = node_list[:].index(foo_tree)
assert foo_index == 3, foo_index  # FAILS: foo_index is 0

----------------------------

So, fo_index == 0. Which is <a name="car">foo</a>. Apparently the 
.index only looks at the text or something?!

Anyway, all I *actually* want is to remove the nodes found by the 
xpath. The way you'd think it would be 'normal' doesn't work 
unfortunately:


(Pdb) p parent.index(foo_tree)
2
(Pdb) del parent[2]
*** TypeError: deleting items not supported by root element

This is obviously because of the sort fo strange list/attribute 
handling (i.e. parent is parent[0])

But then there is parent.remove(...) which works.


Apart from the bug above objectify feels kind of strange sometimes... :/

But then at other times its really nice again :)

-- 
Christian Zagrodnick

gocept gmbh & co. kg  ·  forsterstrasse 29 · 06112 halle/saale
www.gocept.com · fon. +49 345 12298894 · fax. +49 345 12298891





More information about the lxml-dev mailing list