Proposal for new operators to python that add syntactic sugar for hierarcical data.

glomde tbrkic at yahoo.com
Thu May 18 05:52:15 EDT 2006


> You can put any python expression in there, and in the Node class you can do
> what you want.

But the main advantage is any python code not only expressions. In
addition to that
you get nice flow style. Can set variables which you later use as want.

> Seems that you are after a templating-system. Well, there are plenty of them
> available, including yours. A preprocessor is nothing but a template system
> - see TurboGears-utilized KID (http://kid.lesscode.org/) for example, it
> generates python files. And way more readable, because you are working in
> the domain of your application (HTML) instead of some crude syntax that is
> neither fish or flesh.

What I propose is for all data that is hierarcical. Not only HTML. I
dont use it for creating html. Templating systems have the drawback IMO
that the more code you get the more undreadable it gets. If the code
part is small they are very nice.

The other part is that indentation, syntax highlighting and so on works
not so well when working with such code.


I would translate your example to the below. Which I think is much more
readable, since you see the python code flow much easier. I realised
that you realy should do Element("html") and not html("html") when
adding nodes in my syntax and using elementtree.

from urllib import urlopen
from elementtree.ElementTree import parse, tostring
feed = 'http://naeblis.cx/rtomayko/weblog/index.atom'
root = parse(urlopen(feed)).getroot()
ns = '{http://purl.org/atom/ns#}'
title = root.findtext(ns + 'title')

root = ET.Element("html")
*+* root:
    *+* Element("head"):
         *+* Element("title")
         *+* Element("body"):
             *=* bgcolor = "blue"
             *=* text = "yellow"
             *+* Element("table"):
                 *=* cellpadding="4"
                 *=* cellspacing="4"
                 for i, entry in enumerate(root):
                     *+* Element("tr"):
                     if entry.tag==ns + 'entry':
                         *+* Element("td"):
                                 *=* bgcolor = ('white', 'yellow')[i %
2]
                                 *+* Element("a"):
                                     *=* href = entry.find(ns +
'link').attrib['href']
                                     *=* text = "entry.findtext(ns +
'title')"


With the above you can use your favorite editor to do the coding and
get indentation, syntax highlightning and so on. Which I find can be
quite a burden with a templating system especially the more code that
is in there.

It is also easier to debug in comparison when using a templating system.




More information about the Python-list mailing list