Message151534
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>
> _______________________________________ |
|
Date |
User |
Action |
Args |
2012-01-18 12:04:21 | paaguti | set | recipients:
+ paaguti, patrick.vrijlandt |
2012-01-18 12:04:20 | paaguti | link | issue13796 messages |
2012-01-18 12:04:19 | paaguti | create | |
|