Optional algol syntax style
Sokolov Yura
falcon at intercable.ru
Mon Sep 5 05:36:11 EDT 2005
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)
More information about the Python-list
mailing list