By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,630 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

Optional algol syntax style

P: n/a
I think, allowing to specify blocks with algol style (for-end, if-end,
etc) will allow to write easy php-like templates
and would attract new web developers to python (as php and ruby do).
It can be straight compilled into Python bytecode cause there is
one-to-one transformation.
So this:
# -*- syntax-style: algol -*-
class asd:
def __init__(self,a):
if a:
for i in xrange(100):
pass
else:
while false:
pass
else:
pass
end
end
else:
self.a=None
end
end
end
is equivalent for:
class asd:
def __init__(self,a):
if a:
for i in xrange(100):
pass
else:
while false:
pass
else:
pass
else:
self.a=None
but could be written as:

# -*- syntax-style: algol -*-
class asd: def __init__(self,a): if a: for i in xrange(100): pass; else:
while false: pass; else: pass; end; end; end; end;

which is easier to embed into http template (php and ruby shows it).

And, maybe, there can web syntax - equivalent for php and ruby template:
<%# -*- syntax: web -*-%>
<%# -*- command: print -*- %>
<%
def print_all(l):%>
<table><%
for k in l:%>
<tr><td>#{k.name}</td><td>#{k.surname}</td></tr>
<%end%>
</table>
<%end
# -*- command: yield -*-
def yield_all(l):%>
<table><%
for k in l:%>
<tr><td>#{k.name}</td><td>#{k.surname}</td></tr>
<%end%>
</table>
<%end
# -*- command: S.write -*-
def write_all(S,l):%>
<table><%
for k in l:%>
<tr><td>#{k.name}</td><td>#{k.surname}</td></tr>
<%end%>
</table>
<%end%>

will be translated as it were written:
def print_all(l):
print (r"<table>")
for k in l:
print (r"<tr><td>"+`k.name`+ r"</td><td>"+`k.surname`+ r"</td></tr>")
# or print (r"<tr><td>"+str(k.name)+ r"</td><td>"+str(k.surname)+
r"</td></tr>")
print (r"</table>")
def yield_all(l):
yield (r"<table>")
for k in l:
yield (r"<tr><td>"+`k.name`+ r"</td><td>"+`k.surname`+ r"</td></tr>")
yield (r"</table>")
def write_all(S.l):
S.write (r"<table>")
for k in l:
S.write (r"<tr><td>"+`k.name`+ r"</td><td>"+`k.surname`+
r"</td></tr>")
S.write (r"</table>")

I offer to include it into iterpretter, so we can leave another
compilation stage, as it exists with popular
template libraries, which compile their template files into bytecode (or
python source)

Sep 5 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Sokolov Yura wrote:
I think, allowing to specify blocks with algol style (for-end, if-end,
etc) will allow to write easy php-like templates
and would attract new web developers to python (as php and ruby do).
It can be straight compilled into Python bytecode cause there is
one-to-one transformation.
[snip details]


Standard answer to this kind of question:

You can already do that in Python. It's spelled #end.

/MiO
Sep 5 '05 #2

P: n/a
You didn't quite get the OP's intention, I guess.

The OP wanted Python to be a bit more freeform by adding "end" tags.
That might be an improvement for web scripting, but I haven't seen the
solutions of the existing frameworks and won't dare to compare.

Sep 5 '05 #3

P: n/a
"Christoph Rackwitz" <ch****************@gmail.com> writes:
You didn't quite get the OP's intention, I guess.

The OP wanted Python to be a bit more freeform by adding "end" tags.
That might be an improvement for web scripting, but I haven't seen the
solutions of the existing frameworks and won't dare to compare.


Existing frameworks (the one's I've looked at anyway) tend to replace
indents with end tags of some kind. Doing anything else tends to get
ugly unless the language you are templating for treats whitespace the
same way Python does.

I'd argue that you're better off using a real templating system than
trying to mangle Python into becoming a more acceptable templating
system. For templates, you tend to want to access multiple different
names spaces that are only vaguelly related to the Python contextual
name spaces. For example, you may want CGI (or whatever variables)
available directly in the template, rather than as
CGI["foobar"].value. However, you don't want them showing up in front
of your templat variables; you want to be able to say "add CGI
variables foo, bar and foobar to the current name space" in a short
manner.

Templating systems (well, the better ones, anyway) let you manipulate
those name spaces in ways you can't do in python. Cheeta and
ClearSilver come to mind as systems that will let you do this.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Sep 7 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.