[lxml-dev] No extend method on elements?
I know ElementTree doesn't support it, but is there any chance of getting an extend method on Elements? It's an awfully useful list function, and my first try for replacement was: [ element.append( new ) for new in otherelement ] However, it looks like for large element lists, it's far faster to use slice assignment: element[ len( element ) : len( element ) ] = otherelement which was not the most intuitive way to do things for me. It'd be nice if -0 : -0 was a real slice... Is this really the best way, or am I missing something obvious? -- John Krukoff <jkrukoff@ltgc.com> Land Title Guarantee Company
Fredrik Lundh wrote:
John Krukoff wrote:
I know ElementTree doesn't support it, but is there any chance of getting an extend method on Elements?
ET 1.3 has an extend() method.
That's good to know. Then I guess lxml 1.1 should have one, too.
element[ len( element ) : len( element ) ] = otherelement
shorter:
element[len(element):] = otherelement
That's the "obvious" way of implementing it. So here's a quick and small patch against the trunk that adds the function to etree. Something like this will make it into 1.1. Stefan Index: src/lxml/etree.pyx =================================================================== --- src/lxml/etree.pyx (Revision 31661) +++ src/lxml/etree.pyx (Arbeitskopie) @@ -725,6 +725,11 @@ # parent element has moved; change them too.. moveNodeToDocument(element, self._doc) + def extend(self, elements): + """Extends the current children by the elements in the iterable. + """ + self[python.PY_SSIZE_T_MAX:python.PY_SSIZE_T_MAX] = elements + def clear(self): """Resets an element. This function removes all subelements, clears all attributes and sets the text and tail
participants (3)
-
Fredrik Lundh -
John Krukoff -
Stefan Behnel