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

Diez B. Roggisch deets at nospam.web.de
Thu May 18 04:33:31 EDT 2006


glomde wrote:

> There are some difference which are quite essential.
> 
> First of all I dont se how you set attributes with that and

Use dicts as childs.

> then I dont see how you can mix python code with the creation.

You can put any python expression in there, and in the Node class you can do
what you 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.

Counterquestion: how do you create this:

<?xml version='1.0' encoding='utf-8'?>
<?python
    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')
?>
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:py="http://purl.org/kid/ns#">
<head>
    <title py:content="title" />
</head>
<body bgcolor="blue" text="yellow">
    <h1 py:content="title" />
    <table cellpadding="4" cellspacing="4">
        <tr py:for="i, entry in enumerate(root)"
          py:if="entry.tag == ns + 'entry'">
            <td py:attrs="bgcolor=('white', 'yellow')[i % 2]">
                <a py:attrs="href=entry.find(ns + 'link').attrib['href']"
                    py:content="entry.findtext(ns + 'title')" />
            </td>
        </tr>
    </table>
</body>
</html>


Diez



More information about the Python-list mailing list