Alex Klizhentas | 1 May 21:11

Re: Custom Elements question

Thanks for the comments,

The idea behind this is to allow the XML tree to notify observers when it's contents are changed: the node is added, removed or moved.

That's why I'm going to override the ElementBase members so that they will notify observers on the certain actions performed.

Everything works fine, except this usefult SubElement function that did not work as expected, now you've clarified the things,

Thanks
Alex

2008/5/1 Stefan Behnel <stefan_ml <at> behnel.de>:
Hi,

Alex Klizhentas wrote:
> I've extended the ElementBase object using the approach described in the
> tutorial, but SubElement does not work as desired:
>
> class NodeBase(etree.ElementBase):
>      def append(self,child):
>  print "aaa"
>  return etree.ElementBase.append(self,child)
>
> etree.SubElement(root,"child") #no "aaa" printed

That's because SubElement() does not call .append().


> OK, but when taking your code to the module:
>
> def SubElement(parent, tag, attrib={}, **extra):
>     attrib = attrib.copy()
>     attrib.update(extra)
>     element = parent.makeelement(tag, attrib)
>     parent.append(element)
>     return element
>
> SubElement(root,"child") # "aaa" is here!

As expected, as you call .append() explicitly here.


> and overriding
>     def makeelement(self, tag, attrib):
>         return Node(tag, attrib)
>
> in the NodeBase just does not help,

SubElement() does not call .makeelement() either. It's implemented in plain C.
Could you explain a bit why you want to do this and how your .append() differs
from the normal append code?

Stefan



--
Regards,
Alex
_______________________________________________
lxml-dev mailing list
lxml-dev <at> codespeak.net
http://codespeak.net/mailman/listinfo/lxml-dev

Gmane