Issue 13796: use 'text=...' to define the text attribute of and xml.etree.ElementTree.Element (original) (raw)
Issue13796
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? 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? > 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 Good morning! How do you do Fine 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")) How do you doFine ' 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? > > 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 > > > Good morning! > > How do you do > > Fine > > > > 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")) > > > How do you doFine > > > ' > > 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? >> > 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) * ![]() |
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:25 | admin | set | github: 58005 |
| 2012-01-21 02:29:14 | terry.reedy | set | status: open -> closednosy: + terry.reedymessages: + resolution: not a bug |
| 2012-01-18 12:04:20 | paaguti | set | messages: + |
| 2012-01-17 15:11:25 | patrick.vrijlandt | set | messages: + |
| 2012-01-16 16:13:45 | paaguti | set | messages: + |
| 2012-01-16 11:14:10 | patrick.vrijlandt | set | nosy: + patrick.vrijlandtmessages: + |
| 2012-01-16 07:21:26 | paaguti | create |
