classification
Title: instantiating and populating xml.dom.minidom.Element is cumbersome
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Alexandre.Zani, BreamoreBoy, avono, exarkun
Priority: normal Keywords: easy, patch

Created on 2009-01-05 19:08 by exarkun, last changed 2012-06-05 13:47 by avono.

Files
File name Uploaded Description Edit
easier_element_init.patch Alexandre.Zani, 2012-06-02 05:00 Add new optional arguments to Element constructor and tests review
Messages (7)
msg79183 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2009-01-05 19:08
In order to create an element with an attribute and a child, this is
necessary:

    e = Element("foo")
    e.setAttribute("bar", "baz")
    e.appendChild(quux)

It would be preferable if Element.__init__ accepted two additional
parameters to shorten this:

    e = Element("foo", attributes={"bar": "baz"}, children=[quux])

It may also be preferable to have a third new parameter, attributesNS,
to parallel the Element.setAttributeNS method.  This would accept a dict
mapping namespaceURI and qualifiedName to an attribute value.
msg108192 - (view) Author: Mark Lawrence (BreamoreBoy) Date: 2010-06-19 18:11
As you Jean-Paul Calderone seem to know what you're talking about could you provide a patch to get this issue going?  If not I might even have a go myself, even if I do get shot down in flames.
msg111724 - (view) Author: Mark Lawrence (BreamoreBoy) Date: 2010-07-27 20:19
I think this could be sneaked into 3.2 if needed, but is it more work than the benefits actually deliver in the real world?
msg161577 - (view) Author: Alexander O (avono) Date: 2012-05-25 12:07
stupid question but why shouldn't this be possible ? 

class Element(_Element):
	def __init__(self,name,**kwargs):
		_Element.__init__(self,name)
                attributes = kwargs.get("attributes")
                children = kwargs.get("children")
		for key,value in attributes.iteritems():
			self.setAttribute(key,value)
		for child in children:
			self.appendChild(child)
msg162080 - (view) Author: Alexandre Zani (Alexandre.Zani) Date: 2012-06-01 17:11
I would tend to disagree with the use of **kwargs. It means the argument list would be implicit, not documented by the code and not checked at runtime. We could end up with anything in there. I'll try to propose a patch tonight that implements Jean-Paul's solution.
msg162123 - (view) Author: Alexandre Zani (Alexandre.Zani) Date: 2012-06-02 05:00
Here is my patch for it.
msg162341 - (view) Author: Alexander O (avono) Date: 2012-06-05 13:47
On Jun 2, 2012 7:00 AM, "Alexandre Zani" <report@bugs.python.org> wrote:

>
> Alexandre Zani <alexandre.zani@gmail.com> added the comment:
>
> Here is my patch for it.
>
> ----------
> keywords: +patch
> Added file: http://bugs.python.org/file25796/easier_element_init.patch
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue4849>
> _______________________________________
>
History
Date User Action Args
2012-06-05 13:47:43avonosetmessages: + msg162341
2012-06-02 05:00:46Alexandre.Zanisetfiles: + easier_element_init.patch
keywords: + patch
messages: + msg162123
2012-06-01 17:11:33Alexandre.Zanisetmessages: + msg162080
2012-05-25 12:07:43avonosetnosy: + avono
messages: + msg161577
2012-05-21 06:23:58eric.araujosetkeywords: + easy
versions: + Python 3.3, - Python 3.2
2012-05-21 03:25:40Alexandre.Zanisetnosy: + Alexandre.Zani
2010-07-27 20:19:10BreamoreBoysettype: behavior -> enhancement
stage: needs patch
messages: + msg111724
versions: + Python 3.2
2010-06-19 18:11:02BreamoreBoysetnosy: + BreamoreBoy
messages: + msg108192
2009-01-05 19:08:25exarkuncreate