Python declarative

  • Thread starter Sergio Tortosa Benedito
  • Start date
S

sertorbe

Hi again , I'm sorry I dind't reply earlier I still hav enot added myself to the list (I'm doing this trhough google groups).

Thanks Asaf, but I don't like having the code and the GUI separated.

First,I'm not aiming to not programmers (or people qho don't want to be one) because they may be able to write the layout but not the actual program'sfunction so there's no reason to do that.

Second, I wouldn't like to have code and GUI separated since it's a hassle when try to change something like the name.

XML and JSON are pretty cool but they serve for loading info which can varyto a fixed engine not for when that info does not change, and that's even more true when you are speaking about an interpreted language.

I've been reasearching a bit myself and this is what I got: a way of havinga nice syntax in python itself (no external files, no new language...) with metaclasses, so the same example which was used in Lua (and I think in original's python syntax) would look like this with metaclasses and classes:

class myWindow(Window):
title="Hello World"
class myButton(Button):
label="Hello World"

It's just like what I was looking for, with the added benefit that functions can be defined right there. It has it's own problems (like having to access variables from the global scope), but I think they are either solvable or minor.

Sergio
 
F

Frank Millman

[...]

I have realised that we unlikely to come to an agreement on this in the near
future, as our philosophies are completely different.

You 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.

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"))
)
);

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>

Currently I do not have a widget for a multi-line label, so I just show
three separate lines. If I ever needed one, it would not take long to
create.

I took two screenshots, but I don't know the best way to upload and share
them. I found a site called tinypic, which works, but the screen is
cluttered with a lot of other stuff. Still, it shows what I want.

First, here is the screen as rendered in Internet Explorer (it works in
other browsers as well - even on my smart phone, though I have not done any
optimising for mobile devices yet) -

http://tinypic.com/r/ip15xx/5



Second, here is the screen designer, showing a portion of the screen
definition - one of the buttons. As you can see, the designer does not have
to worry about XML at all -


http://tinypic.com/r/1j7sdh/5



I am not claiming that I am 'right', or that this is a perfect solution. It
is just 'where I am' right now, after quite a long period of evolution, and
it is achieving a large portion of what I am aiming for, so I will press on
with my current approach for now.



Hopefully it will not take *too* long before I am in a position to release
something, and then I will be very interested in any feedback.



Thanks for the interesting discussion.



Frank
 
C

Chris Angelico

I have realised that we unlikely to come to an agreement on this in the near
future, as our philosophies are completely different.

You 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.

Ah but the question is *why* you want to minimize code. I write
everything in code because it minimizes unnecessary coding - yes,
that's right, I maximize code to minimize code :) - because all that
code that isn't written also isn't debugged, isn't maintained, and
isn't placing unnecessary restrictions on anything. What's the
advantage of doing it in XML?

Your GUI builder is all very well, but it could as easily write Python
code as XML, so it doesn't give a strong incentive for XML. And
personally, I think it's more likely to be a waste of effort, too, but
that's more because a good GUI builder takes a *ton* of effort to
build. That's time that has to be spent up-front, before you have
experience with the system, before you _can get_ experience with the
system - and, see above, it's code that has to be debugged and
maintained.

Every framework has to justify itself. Just "it lets people not write
code" isn't enough unless not-writing-code can justify the costs.

ChrisA
 
M

Matěj Cepl

[This message has also been posted to gmane.comp.python.general.]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I have stated that my objective is to express as little as
possible in Python code.

Yes, and I believe that it is very wrong. But anyway, if you are
so passionate about GUI-via-XML, what’s wrong with Glade
(http://www.pygtk.org/pygtk2reference/class-gladexml.html)? You
have editors for that XML etc.

Matěj

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iD8DBQFS457O4J/vJdlkhKwRAlA7AJ9nTl4v+FoiNZb3NLaSsIZMd2HO5wCeNYwe
EVLDNqlw3YaQtloF1RGWP8Y=
=AloI
-----END PGP SIGNATURE-----
 
S

Steven D'Aprano

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.
 
M

Mark Lawrence

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.

If I worked as a consultant I'd much prefer the XML version as I'd be
able to charge much more on the grounds that I'd done much more, hoping
that the people paying didn't bother with design reviews or the like :)
 
C

Chris Angelico

If I worked as a consultant I'd much prefer the XML version as I'd be able
to charge much more on the grounds that I'd done much more, hoping that the
people paying didn't bother with design reviews or the like :)

And that's very true. If someone wants something infinitely
customizeable, the best solution might be an empty file with a shebang
at the top - but that's hardly something you can charge oodles for.
This is where soft-coding comes from.

http://thedailywtf.com/Articles/Soft_Coding.aspx

The business logic examples given there are best served by hard code.
Suppose you need to calculate this-value plus that-value times
other-value; is there any difference between writing that in Python
(ugh, that's hard code!) and putting the formula in a config file,
where it'll get parsed and evaluated (oh, that's a config file, that's
fine)? As you'll see in my other post, there's a formula evaluator
routine there; it takes a string of code and compiles it:

(wealth_plat*1000+wealth_gold*100+wealth_silver*10+wealth_copper)/100

It's straight code, and it's embedded. If ever the rules change, I can
edit the code. For instance, here's the D&D Fourth Edition version of
that:

(wealth_plat*10000+wealth_gold*100+wealth_silver*10+wealth_copper)/100

Should the value of a platinum coin be moved out into a config file?
Maybe. MAYBE. Should the value of a silver piece be? Definitely not!
There's no use-case for silver pieces being worth anything other than
ten copper. If and when there is one, the code can simply be edited.
There is no value in soft-coding this formula.

Code isn't something to be afraid of. It's just text files like any
other. After all, Python code is a config file for /usr/bin/python, so
if you want to change what Python does, just edit its config file!

ChrisA
 
R

Rustom Mody

Code isn't something to be afraid of. It's just text files like any
other. After all, Python code is a config file for /usr/bin/python, so
if you want to change what Python does, just edit its config file!

Windows stores configuration in the registry -- by fiat
Linux (posix) stores configuration in /etc + ~/.files -- by convention

Which do you think is preferable?
 
C

Chris Angelico

Windows stores configuration in the registry -- by fiat
Linux (posix) stores configuration in /etc + ~/.files -- by convention

Which do you think is preferable?

Not exclusively, in either case. Many many things are config files of
various sorts. The terms of the GPL specifically state that a GPL'd
language does not enforce that code written in it is GPL'd, because
it's just (to the GPL code) data files.

Plenty of Windows programs store config files outside the registry.

ChrisA
 
F

Frank Millman

Steven D'Aprano said:
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.

[...]

I did not really want to continue this, but I have to respond to that.

There is a sub-context within which I made my statement.

Here is a quote from one of my earlier posts -
Each form definition is stored as gzip'd XML in a database, and linked to
the
menu system. There is just one python program that responds to the
selection
of a menu option, retrieves the form from the database, unpacks it and
runs it.

I do not expect anyone to read or edit the XML - it is just a storage
format. I am sure it could be done in JSON or YAML as well.

One objective is to make it easy for non-programmers to modify forms and
create new ones. I showed a screenshot earlier that illustrated a 'button'
definition.

Frank
 
R

Rustom Mody

Not exclusively, in either case. Many many things are config files of
various sorts. The terms of the GPL specifically state that a GPL'd
language does not enforce that code written in it is GPL'd, because
it's just (to the GPL code) data files.

Ok so you are being careful and hedging your bets!!
[And Ive no idea what the gpl has to do with this]

If you see the 'Principle of Least Power here:
http://www.w3.org/DesignIssues/Principles.html
there is a spectrum of power from narrow data format to universal data
format to Turing complete programming language. And a strong case is
made for minimizing the 'power' in any application.

By decreeing things about the registry, windows solves many problems
that create unnecessary headaches for developers, packagers, uses with
the laissez-faire approach of 'put whatever you like in /etc.' This
follows from the principle: "Anything goes" applied to /etc means messes
go in. Its harder to push messes into a dictat-ed registry

Steven's link
http://www.codinghorror.com/blog/2008/05/xml-the-angle-bracket-tax.html
linked to
http://nothing-more.blogspot.in/2004/10/where-xml-goes-astray.html
explains what the real problem is:

Xml, originally a document format, is nowadays used as a data-format.
This conduces to humongous messing, first for the xml-library writers, and
thence to the users of those libraries because library messes inevitably
leak past abstraction barriers to cause user-programmer headaches.

tl;dr
Frank's principle: "Express little as possible in <programming language>"
is correct.
"And therefore XML is the solution"
is bad logic
[Unless <programming language> == "java" !]
 
F

Frank Millman

Rustom Mody said:
Xml, originally a document format, is nowadays used as a data-format.
This conduces to humongous messing, first for the xml-library writers, and
thence to the users of those libraries because library messes inevitably
leak past abstraction barriers to cause user-programmer headaches.

tl;dr
Frank's principle: "Express little as possible in <programming language>"
is correct.
"And therefore XML is the solution"
is bad logic
[Unless <programming language> == "java" !]

If that is the case, what is 'good logic'? JSON or YAML?

It does not make much difference which format I use. However, I will say
that I found it a useful discipline to create an xml schema to describe my
form definition, for two reasons.

Firstly, I was hand-crafting my form definitions initially, and as I added
features it became unwieldy. Forcing myself to create the schema highlighted
a lot of anomalies and I ended up with a much cleaner structure as a result.

Secondly, it has picked up a lot of errors in the resulting documents which
would otherwise have generated hard-to-find runtime exceptions.

Frank
 
S

Steven D'Aprano

Code isn't something to be afraid of.

Not according to the Laundry series by Charles Stross. The protagonist of
the series was "recruited" to the Laundry after the computer program he
was working on almost summoned Nyarlathotep the Crawling Chaos Elder God
to Wolverhampton.
 
S

Steven D'Aprano

I do not expect anyone to read or edit the XML - it is just a storage
format. I am sure it could be done in JSON or YAML as well.

But that's not what you originally said. You stated:

"here is how I would write your simple 'About' box"

and compared your XML to actual code written by Chris.

As I said in my previous post, if the XML is intended as a purely
internal document, written by and read by your application, it's not so
bad. (But then XML is intended for document *exchange*, i.e. from one
application to another. If your XML is just used *internally*, with no
interchange needed, why not use a more efficient internal format?

XML's strength is that it is a well-known standard that allows
application A to interchange documents with application B. But it's
weaknesses include, it is neither efficient like a custom-designed binary
format, not human-editable.

It seems to me that if I were in your position, I would have the GUI
designer generate source code in some language directly, rather than XML.
Ah, wait! An idea strikes... if your GUI designer generates XML, you
could then have a plug-in system to convert the XML to source code in
whatever languages the plug-in supports. So that's a possible good use
for XML as an intermediate language.

One objective is to make it easy for non-programmers to modify forms and
create new ones. I showed a screenshot earlier that illustrated a
'button' definition.

The idea of drag-and-drop GUI designers is hardly new. I was using one
back in 1986 or '88, Apple's Hypercard. Now that was a user-friendly
programming language/environment/toolkit.
 
R

Rustom Mody

Rustom Mody" said:
Xml, originally a document format, is nowadays used as a data-format.
This conduces to humongous messing, first for the xml-library writers, and
thence to the users of those libraries because library messes inevitably
leak past abstraction barriers to cause user-programmer headaches.
tl;dr
Frank's principle: "Express little as possible in <programming language>"
is correct.
"And therefore XML is the solution"
is bad logic
[Unless <programming language> == "java" !]
If that is the case, what is 'good logic'? JSON or YAML?
It does not make much difference which format I use. However, I will say
that I found it a useful discipline to create an xml schema to describe my
form definition, for two reasons.
Firstly, I was hand-crafting my form definitions initially, and as I added
features it became unwieldy. Forcing myself to create the schema highlighted
a lot of anomalies and I ended up with a much cleaner structure as a result.
Secondly, it has picked up a lot of errors in the resulting documents which
would otherwise have generated hard-to-find runtime exceptions.

There are json/yaml 'schema'* validators if you want eg
https://github.com/alecthomas/voluptuous
http://rx.codesimply.com/

* if you want to call them that!
 
A

Asaf Las

On 26/01/2014 02:33, Steven D'Aprano wrote:

If I worked as a consultant I'd much prefer the XML version as I'd be
able to charge much more on the grounds that I'd done much more, hoping
that the people paying didn't bother with design reviews or the like :)

My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence

Google's android uses XML for GUI widget definitions.
/Asaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top