[Twisted-Python] copy domish element

Ralph Meijer twisted at ralphm.ik.nu
Tue Jun 9 02:47:36 EDT 2009

On Mon, 2009-06-08 at 18:03 +0100, Phil Mayers wrote:
> Jack Bates wrote:
> > i have a domish element - when i try the following,
> > 
> >>>> message = element
> >>>> del message['from']
> >>>> element['from']
> > 
> > - i get a key error
> Well, yes. That's how python works ;o)
> > 
> > to avoid this key error, i think i want to copy the element, so i can
> > drop the 'from' attribute from one copy without affecting the other?
> > 
> > but i didn't find anything to make a copy in the domish element api
> I haven't tried it, but have you tried the python standard copy protocol:
> import copy
> message = copy.deepcopy(element)

Deep copies of Element instances is probably not what you want here. See
issue #724.

I suspect from your example that you want to respond to an incoming XML
Stanza. In general, it is better to construct a new stanza from the old
one, keeping the addressing but in reverse. There is a utility function
for this: twisted.words.protocols.jabber.xmlstream.toResponse. For error
responses, you want to look at t.w.p.j.error.StanzaError and its
toResponse method.



More information about the Twisted-Python mailing list