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.

Author patrick.vrijlandt
Recipients paaguti, patrick.vrijlandt
Date 2012年01月17日.15:11:24
SpamBayes Score 2.0787672e-08
Marked as misclassified No
Message-id <CAHRxO8irhc-vMHx5oXQo+w+sSo6J+Kao2O0YeTGJNv9nER0eOQ@mail.gmail.com>
In-reply-to <CAO48Bk-ewBnvWp5Y_YCwiBgcjnUVZOWOQRCfYCpt__YpvZ-vcg@mail.gmail.com>
Content
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>
> _______________________________________
>
History
Date User Action Args
2012年01月17日 15:11:25patrick.vrijlandtsetrecipients: + patrick.vrijlandt, paaguti
2012年01月17日 15:11:25patrick.vrijlandtlinkissue13796 messages
2012年01月17日 15:11:24patrick.vrijlandtcreate

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