back

epy.py - implementation of ePython

Introduction

epy.py is a Python implementation of ePython(embedded Python) like Perl's ePerl and Ruby's eRuby. I referenced to 30 Lines Implementation of eRuby, written by creator of Erubis)(fastest eRuby implementation) and pyTenjin(fastest python template system).

Feature

Download

http://wids.net/archive/epy/

Syntax

<% (python code) %>
do python code
<%= (python code) %>
do python code and print return value
<%# (python code) %>
code comment. It's exists in python code but not print.
<%=r (python code) %>
raw mode. (See FAQ Section)
<% %>
1% of incompatible. This is like endfor, endif, and end at other template systems, and outdent one level to python code. I brought this idea form Python Server Pages.

Example

Basic

Python 2.5 (release25-maint, Jul 20 2008, 20:47:25)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import epy
>>> epython = epy.ePython('Hello, <%= progname.replace("p", "P") %>')
>>> env = dict(progname='epython')
>>> print epython.render(env)
Hello, ePython
>>> print epython.pysrc
_buf = []
_buf.append(u'Hello, ')
_buf.append(_esc(unicode(progname.replace("p", "P"))))

__result = ''.join(_buf)

for loop

epython.src

<% for language in languages: %>
 <p>embedded <%= language %> is e<%= language.title() %>.</p>
 <% %>

epython.render(dict(languages=['perl', 'ruby', 'python'])

 <p>embedded perl is ePerl.</p>
 <p>embedded ruby is eRuby.</p>
 <p>embedded python is ePython.</p>

epytnon.pysrc

_buf = []
for language in languages:
 _buf.append(u' embedded ')
 _buf.append(_esc(unicode(language)))
 _buf.append(u' is e')
 _buf.append(_esc(unicode(language.title())))
 _buf.append(u'.\n')

__result = ''.join(_buf)

if .. elif .. else

epython.src

<p>Acronym dictionary - <%= ac %>:<br />
<% if ac == 'DoCoMo': %>
 Do Communication over the Mobile
<% elif ac == 'IBM': %>
 International Business Machines
<% else: %>
 A Clever Representation Of Names You Manufacture
 <% %></p>

epython.render(dict(ac='PCMCIA'))

<p>Acronym dictionary - PCMCIA:<br />
 A Clever Representation Of Names You Manufacture
</p>

epython.pysrc

_buf = []
_buf.append(u'<p>Acronym dictionary - ')
_buf.append(_esc(unicode(ac)))
_buf.append(u':<br />\n')
if ac == 'DoCoMo':
 _buf.append(u' Do Communication over the Mobile\n')
elif ac == 'IBM':
 _buf.append(u' International Business Machines\n')
else:
 _buf.append(u' A Clever Representation Of Names You Manufacture\n')
_buf.append(u'</p>\n')

__result = ''.join(_buf)

FAQ

"Syntax Surface" 99% compatible ?

Actually, epy.py's ePython implementation does not care for indent.

<% for language in languages: %>
 <p>embedded <%= language %> is e<%= language.title() %>.</p>
 <% %>

(Attention for indent !)

<% for language in languages: %>
<p>embedded <%= language %> is e<%= language.title() %>.</p>
<% %>

Both these code are perfectory work. So, "Syntax Surface" compatible.

I want to generate HTML. Please auto quote returning values.

Use epy.ePythonHTML. The render method of this class quote returning values, and use 'raw mode' to parts of ePython code you don't hope quoting.

Requirements

Changelog

License

MIT License.