Python declarative

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jan 25 21:33:32 EST 2014


On Sat, 25 Jan 2014 09:18:44 +0200, Frank Millman wrote:

> I have realised that we unlikely to come to an agreement on this in the
> near future, as our philosophies are completely different.
> 
> You [Chris Angelo] have stated that your objective is to express as
> much as possible in Python code.
> 
> I have stated that my objective is to express as little as possible in
> Python code.

Interesting perspective.


> We would have to resolve that difference of opinion first, before
> discussing our respective approaches in detail, and that is way beyond
> the scope of this thread.
> 
> As a brief example of my approach, here is how I would write your simple
> 'About' box.
> 
> Here is your version -
> 
> mainwindow = GTK2.Window(0)->add(GTK2.Vbox(0,0)
>     ->add(GTK2.Label("About Gypsum: big long multi-line string"))
>     ->add(GTK2.HbuttonBox()
>         ->add(GTK2.Button("Close"))
>         ->add(GTK2.Button("Foobar"))
>     )
> );

That's not Python code, but it's reasonably concise and readable. (By the 
way, what language is it?) The meaning is fairly obvious, and it's all 
pretty simple. I'd like to see a more human readable GUI designer 
language:

# hypothetical code in a DSL for creating GUI elements
create new window mainwindow
with mainwindow
    add vbox at 0,0
    add label "About Gypsum: big long multi-line string"
    add hbuttonbox
    add button "Close"
    add button "Foobar"

but what you've got there is okay. Seven lines of readable code.


> Here is my version -
> 
> <form name="ChrisAbout_form">
>   <data_objects/>
>   <input_params/>
>   <output_params/>
>   <rules/>
>   <frame>
>     <body>
>       <block/>  <!-- an html div -->
>       <panel/>  <!-- an html table -->
>       <row/>
>       <col/>
>       <text value="This is line one of a multi-line string"/> <row/>
>       <col/>
>       <text value="This is line two of a multi-line string"/> <row/>
>       <col/>
>       <text value="This is line three of a multi-line string"/>
>     </body>
>     <button_row>  <!-- an html div -->
>       <button btn_id="btn_close" btn_label="Close" len="60"
>           btn_enabled="true" btn_validate="false" btn_default="true">
>         <end_form state="completed"/>
>       </button>
>       <button btn_id="btn_foo" btn_label="Foobar" len="60"
>           btn_enabled="true" btn_validate="false" btn_default="false">
>         <pyfunc name="path1.path2.foobar"/>
>       </button>
>     </button_row>
>     <frame_methods>
>       <method name="on_req_close">  <!-- user clicked [X] or pressed
> Shift_F4 -->
>         <end_form state="completed"/>
>       </method>
>       <method name="on_req_cancel">  <!-- user pressed Escape -->
>         <end_form state="completed"/>
>       </method>
>     </frame_methods>
>   </frame>
> </form>

Thirty. Seven. Lines. Of. XML.

You've got to be kidding me. How can you *possibly* prefer that? 

First rule of XML: it is not human-writable. It's barely human-readable. 
It's a *document* interchange language which happens to use mostly human-
readable elements, which is not the same thing at all. It's designed to 
be machine written. Using XML for *data* is abusive to both the XML 
design goals and to the poor schmuck who has to read your config file. 
Using XML *as a programming language* is not just abuse, it's getting 
into grievous bodily harm territory.

Here's a simple programming expression, familiar to most people, common 
to hundreds of programming languages:

3+4*5

Here it is written as XML:

<add><int>3</int><mult><int>4</int><int>5</int></mult></add>


Source:
http://www.ibm.com/developerworks/xml/library/x-sbxml/index.html

More here:

http://www.codinghorror.com/blog/2008/05/xml-the-angle-bracket-tax.html
http://myarch.com/why-xml-is-bad-for-humans/


If you expect a human being to routinely *read*, let alone *write*, XML 
in preference to some real programming language, that is a horrible, 
horrible thing. Using XML as an internal, machine-generated, format not 
intended for humans is not too bad. Anything else is abusive.



-- 
Steven



More information about the Python-list mailing list