homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: use 'text=...' to define the text attribute of and xml.etree.ElementTree.Element
Type: enhancement Stage:
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: paaguti, patrick.vrijlandt, terry.reedy
Priority: normal Keywords:

Created on 2012年01月16日 07:21 by paaguti, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Messages (6)
msg151336 - (view) Author: Pedro Andres Aranda Gutierrez (paaguti) Date: 2012年01月16日 07:21
I have extended the xml.etree.ElementTree.Element class and pass the text attribute in the arguments. This creates much more compact code:
import xml.etree.ElementTree as xml
 
 
class Element(xml.Element):
 def __init__(self,tag,attrib={},**attrs):
 super(xml.Element,self).__init__()
 self.tag = tag
 self.attrib = attrib
 self.attrib.update(attrs)
 self.text = self.attrib.pop('text',None)
 self.tail = self.attrib.pop('tail',None)
 self._children = []
if __name__ == '__main__':
 from sys import stdout
 test = Element('Hello',)
 test2 = Element('World',{'humour':'excelent'},text = 'How do you do', tail="Fine")
 test.append(test2) 
 xml.ElementTree(test).write(stdout,encoding="utf-8",xml_declaration="yes",method="xml")
msg151350 - (view) Author: patrick vrijlandt (patrick.vrijlandt) Date: 2012年01月16日 11:14
I agree the Element syntax is sometimes awkward.
But how would you represent text or tail attributes within this enhanced element?
<animal name="cat" tail="yes"> comes to mind ...
msg151376 - (view) Author: Pedro Andres Aranda Gutierrez (paaguti) Date: 2012年01月16日 16:13
Touché :-)
I was just frustrated because my XMLs never have tail or text as
attributes and I wanted to have more compact code...
On Mon, Jan 16, 2012 at 12:14 PM, patrick vrijlandt
<report@bugs.python.org> wrote:
>
> patrick vrijlandt <patrick.vrijlandt@gmail.com> added the comment:
>
> I agree the Element syntax is sometimes awkward.
>
> But how would you represent text or tail attributes within this enhanced element?
> <animal name="cat" tail="yes"> comes to mind ...
>
> ----------
> nosy: +patrick.vrijlandt
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue13796>
> _______________________________________
msg151458 - (view) Author: patrick vrijlandt (patrick.vrijlandt) Date: 2012年01月17日 15:11
Hi,
Did you look at lxml (http://lxml.de)?
from lxml.builder import E
from lxml import etree
tree = etree.ElementTree(
 E.Hello(
 "Good morning!",
 E.World("How do you do", humour = "excellent"),
 "Fine",
 E.Goodbye(),
 ),
 )
print(etree.tostring(tree, pretty_print=True).decode())
# output, even more prettified
<Hello>
 Good morning!
 <World humour="excellent">
 How do you do
 </World>
 Fine
 <Goodbye/>
</Hello>
By the way, your Element enhancement is buggy, because all newly create
elements will share the same attrib dictionary (if attrib is not given).
Notice that Donald Duck will be sad; by the time we print even Hello is sad.
import xml.etree.ElementTree as etree
class Element(etree.Element):
 def __init__(self, tag, attrib={}, **extra):
 super().__init__(tag)
 self.tag = tag
 self.attrib = attrib
 self.attrib.update(extra)
 self.text = self.attrib.pop('text', None)
 self.tail = self.attrib.pop('tail', None)
 self._children = []
if __name__ == '__main__':
 test = Element('Hello',)
 test2 = Element('World',{'humour':'excelent'},text = 'How do you do',
tail="Fine")
 test3 = Element('Goodbye', humour='sad')
 test4 = Element('Donaldduck')
 test.append(test2)
 test.append(test3)
 test.append(test4)
 tree = etree.ElementTree(test)
 print(etree.tostring(test, encoding="utf-8", method="xml"))
<Hello humour="sad">
 <World humour="excelent">How do you do</World>Fine
 <Goodbye humour="sad" />
 <Donaldduck humour="sad" />
</Hello>'
The correct idiom would be:
 def __init__(self, tag, attrib=None, **extra):
 if attrib is None:
 attrib = {}
 super().__init__(tag)
Cheers,
Patrick
2012年1月16日 Pedro Andres Aranda Gutierrez <report@bugs.python.org>
>
> Pedro Andres Aranda Gutierrez <paaguti@gmail.com> added the comment:
>
> Touché :-)
> I was just frustrated because my XMLs never have tail or text as
> attributes and I wanted to have more compact code...
>
> On Mon, Jan 16, 2012 at 12:14 PM, patrick vrijlandt
> <report@bugs.python.org> wrote:
> >
> > patrick vrijlandt <patrick.vrijlandt@gmail.com> added the comment:
> >
> > I agree the Element syntax is sometimes awkward.
> >
> > But how would you represent text or tail attributes within this enhanced
> element?
> > <animal name="cat" tail="yes"> comes to mind ...
> >
> > ----------
> > nosy: +patrick.vrijlandt
> >
> > _______________________________________
> > Python tracker <report@bugs.python.org>
> > <http://bugs.python.org/issue13796>
> > _______________________________________
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue13796>
> _______________________________________
>
msg151534 - (view) Author: Pedro Andres Aranda Gutierrez (paaguti) Date: 2012年01月18日 12:04
Thanks a lot again :-)
We have a saying here: you'll never go to sleep without having learnt
something new :-)
On Tue, Jan 17, 2012 at 4:11 PM, patrick vrijlandt
<report@bugs.python.org> wrote:
>
> patrick vrijlandt <patrick.vrijlandt@gmail.com> added the comment:
>
> Hi,
>
> Did you look at lxml (http://lxml.de)?
>
> from lxml.builder import E
> from lxml import etree
>
> tree = etree.ElementTree(
>  E.Hello(
>    "Good morning!",
>    E.World("How do you do", humour = "excellent"),
>    "Fine",
>    E.Goodbye(),
>    ),
>  )
>
> print(etree.tostring(tree, pretty_print=True).decode())
>
> # output, even more prettified
>
> <Hello>
>  Good morning!
>  <World humour="excellent">
>     How do you do
>  </World>
>     Fine
>  <Goodbye/>
> </Hello>
>
> By the way, your Element enhancement is buggy, because all newly create
> elements will share the same attrib dictionary (if attrib is not given).
> Notice that Donald Duck will be sad; by the time we print even Hello is sad.
>
> import xml.etree.ElementTree as etree
>
> class Element(etree.Element):
>  def __init__(self, tag, attrib={}, **extra):
>    super().__init__(tag)
>    self.tag = tag
>    self.attrib = attrib
>    self.attrib.update(extra)
>    self.text = self.attrib.pop('text', None)
>    self.tail = self.attrib.pop('tail', None)
>    self._children = []
>
> if __name__ == '__main__':
>
>  test = Element('Hello',)
>  test2 = Element('World',{'humour':'excelent'},text = 'How do you do',
> tail="Fine")
>  test3 = Element('Goodbye', humour='sad')
>  test4 = Element('Donaldduck')
>  test.append(test2)
>  test.append(test3)
>  test.append(test4)
>  tree = etree.ElementTree(test)
>  print(etree.tostring(test, encoding="utf-8", method="xml"))
>
> <Hello humour="sad">
>  <World humour="excelent">How do you do</World>Fine
>  <Goodbye humour="sad" />
>  <Donaldduck humour="sad" />
> </Hello>'
>
> The correct idiom would be:
>
>  def __init__(self, tag, attrib=None, **extra):
>    if attrib is None:
>      attrib = {}
>    super().__init__(tag)
>
> Cheers,
>
> Patrick
>
> 2012年1月16日 Pedro Andres Aranda Gutierrez <report@bugs.python.org>
>
>>
>> Pedro Andres Aranda Gutierrez <paaguti@gmail.com> added the comment:
>>
>> Touché :-)
>> I was just frustrated because my XMLs never have tail or text as
>> attributes and I wanted to have more compact code...
>>
>> On Mon, Jan 16, 2012 at 12:14 PM, patrick vrijlandt
>> <report@bugs.python.org> wrote:
>> >
>> > patrick vrijlandt <patrick.vrijlandt@gmail.com> added the comment:
>> >
>> > I agree the Element syntax is sometimes awkward.
>> >
>> > But how would you represent text or tail attributes within this enhanced
>> element?
>> > <animal name="cat" tail="yes"> comes to mind ...
>> >
>> > ----------
>> > nosy: +patrick.vrijlandt
>> >
>> > _______________________________________
>> > Python tracker <report@bugs.python.org>
>> > <http://bugs.python.org/issue13796>
>> > _______________________________________
>>
>> ----------
>>
>> _______________________________________
>> Python tracker <report@bugs.python.org>
>> <http://bugs.python.org/issue13796>
>> _______________________________________
>>
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue13796>
> _______________________________________
msg151712 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012年01月21日 02:29
Pedro and Patrick: if you are going to respond by email rather than by typing in the browser box, PLEASE delete the quoted text. Or on occasion, quote a line or two if really necessary, as when responding to a previous message other than the last one. Each message is assumed to be a response to the one immediately preceding, and that message is visible just above yours. This is not like a mail list where previous messages may have come and gone.
In any case, etree.ElementTree is documented as being a stable version F. Lundh's package and I am pretty sure that we are not going to change that by adding things on our own. So continue using your customized subclass.
"See http://effbot.org/zone/element-index.htm for tutorials and links to other docs. Fredrik Lundh’s page is also the location of the development version of the xml.etree.ElementTree.
Changed in version 3.2: The ElementTree API is updated to 1.3. For more information, see Introducing ElementTree 1.3."
History
Date User Action Args
2022年04月11日 14:57:25adminsetgithub: 58005
2012年01月21日 02:29:14terry.reedysetstatus: open -> closed

nosy: + terry.reedy
messages: + msg151712

resolution: not a bug
2012年01月18日 12:04:20paagutisetmessages: + msg151534
2012年01月17日 15:11:25patrick.vrijlandtsetmessages: + msg151458
2012年01月16日 16:13:45paagutisetmessages: + msg151376
2012年01月16日 11:14:10patrick.vrijlandtsetnosy: + patrick.vrijlandt
messages: + msg151350
2012年01月16日 07:21:26paaguticreate

AltStyle によって変換されたページ (->オリジナル) /