Pythonic way of web-programming
ianb at colorstudy.com
Sun Apr 20 22:23:32 CEST 2003
On Sun, 2003-04-20 at 04:21, David Abrahams wrote:
> I don't care much about XML programming (haven't had a use for it),
> but at PyConDC recently I got together with a couple of guys from the
> Twisted project who do care, and designed a nice little meta-language
> in Python for doing this sort of template processing. Here's a little
> example of it in action:
> template = body[
> table(id="outer", width="100%", height="100%", border="0")[
> td(id="output", width="75%", valign="top", model="latestOutput")[
> div(pattern="listItem", view="html")[
> td(id="room-contents", valign="bottom")[
> "Stuff you have"
> div(model="playerInventory", view="List")[
> # Note programmatic elements
> if_(not arg1)
This could almost be plain Python, if Python allowed code blocks. The
use of 's isn't really necessary, and actually the expressions could
maybe be eval'd as well...
td(id='room-contents', valign='bottom', contents=[
strong(contents=['Stuff you have']),
div(model='playerInventory', view="List", contents=[
<[div(style=['color: red', 'color: blue', None],
<> is the only nice nesting braces I could think of (`` wouldn't really
do, since it doesn't nest, not to mention it looks much to much like
''). Of course, closing that nested structure is kind of crazy.
And actually <> wouldn't work, because of if <a > 10> -- it's not
entirely ambiguous, but it certainly *looks* ambiguous. And << and >>
are also used, even if it's only for one of the more stupid operators.
It's all pipe-dreams, but code blocks sure would be nice. I don't think
the argument that they allow New Control Structures is correct --
iterators and the traditional control structures actually cover
everything I've ever thought of. Though now that I think about it, is
the thing above a new control structure? A sort of inside-out control
structure, but also asort of lazy evaluation, like a callback.
I've played around with Python objects that allow lazily evaluated
expressions, by overloading all the comparison operators (though you
can't overload and, or, not, or is). In some ways it pleases me, though
it also feels fragile -- and it's not really compatible with normal
Python, i.e., you couldn't call a normal function with a lazy argument
(though you could call a method on a lazy object easily enough).
More information about the Python-list