ANN: XML builder for Python

Walter Dörwald walter at livinglogic.de
Thu Jul 3 17:46:39 EDT 2008


Stefan Behnel wrote:
> Hi,
> 
> Walter Dörwald wrote:
>> XIST has been using with blocks since version 3.0.
>>
>> Take a look at:
>> http://www.livinglogic.de/Python/xist/Examples.html
>>
>>
>> from __future__ import with_statement
>>
>> from ll.xist import xsc
>> from ll.xist.ns import html, xml, meta
>>
>> with xsc.Frag() as node:
>>    +xml.XML()
>>    +html.DocTypeXHTML10transitional()
>>    with html.html():
>>       with html.head():
>>          +meta.contenttype()
>>          +html.title("Example page")
>>       with html.body():
>>          +html.h1("Welcome to the example page")
>>          with html.p():
>>             +xsc.Text("This example page has a link to the ")
>>             +html.a("Python home page", href="http://www.python.org/")
>>             +xsc.Text(".")
>>
>> print node.conv().bytes(encoding="us-ascii")
> 
> Interesting. Is the "+" actually required? Are there other operators that make
> sense here? I do not see what "~" or "-" could mean.

Of course the node constructor could append the node to the currently 
active element. However there might be cases where you want to do 
something else with the newly created node, so always appending the node 
is IMHO the wrong thing.

 > Are there other operators that make
 > sense here? I do not see what "~" or "-" could mean.
 >
 > Or is it just a technical constraint?

You need *one* operator/method that appends a node to the currently 
active block without opening another new block. This operator should be 
short to type and should have the right connotations. I find that unary 
+ is perfect for that.

 > I'm asking because I consider adding such a syntax to lxml as a separate
 > module. And I'd prefer copying an existing syntax over a (badly) home 
grown one.

"Existing syntax" might be a little exaggeration, I know of no other 
Python package that uses __pos__ for something similar. (But then again, 
I know of no other Python package that uses with block for generating 
XML ;)).

Servus,
    Walter




More information about the Python-list mailing list