ElementTree should parse string and file in the same way

Diez B. Roggisch deets at nospam.web.de
Tue Jan 1 07:36:57 EST 2008


Steven D'Aprano schrieb:
> On Tue, 01 Jan 2008 01:53:47 +0000, Peter Pei wrote:
> 
>> You are talking shit. It is never about whether it is hard to write a
>> wrapper. It is about bad design. I should be able to parse a string and
>> a file in exactly same way, and that should be provided as part of the
>> package.
> 
> Oh my, somebody decided to start the new year with all guns blazing.
> 
> Before abusing anyone else, have you considered asking *why* ElementTree 
> does not treat files and strings the same way? I believe the writer of 
> ElementTree, Fredrik Lundh, frequents this newsgroup.
> 
> It may be that Fredrik doesn't agree with you that you should be able to 
> parse a string and a file the same way, in which case there's nothing you 
> can do but work around it. On the other hand, perhaps he just hasn't had 
> a chance to implement that functionality, and would welcome a patch.
> 
> Fredrik, if you're reading this, I'm curious what your reason is. I don't 
> have an opinion on whether you should or shouldn't treat files and 
> strings the same way. Over to you...

I think the decision is pretty clear to everybody who is a code-monkey 
and not a Peter-Pei-School-of-Excellent-And-Decent-Designers-attendant:

when building a XML-document, you start from a Element or Elementtree 
and often do things like


root_element = <some_element>
for child in some_objects:
     root_element.append(XML("""<child attribute="%i"/>""" % 
child.attribute))

Which is such a common usage-pattern that it would be extremely annoying 
to get a document from XML/fromstring and then needing to extract the 
root-element from it.

And codemonkeys know that in python

doc = et.parse(StringIO(string))

is just one import away, which people who attend to 
Peter-Pei-School-of-Excellent-And-Decent-Designers may have not learned 
yet - because they are busy praising themselves and coating each other 
in edible substances before stepping out into the world and having all 
code-monkeys lick off their greatness in awe.

http://www.youtube.com/watch?v=FM7Rpf1x7RU

Diez



More information about the Python-list mailing list