[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Looking for very light weight template library (not framework

python

3/7/2008 1:56:00 AM

New to Python and looking for a template library that allows Python
expressions embedded in strings to be evaluated in place. In other words
something more powerful than the basic "%(variable)s" or "$variable"
(Template) capabilities.

I know that some of the web frameworks support this type of template
capability but I don't need a web framework, just a library that
supports embedded expression evaluation.

Use case:

myOutput = """
The total cost is {{invoice.total}}.

This order will be shipped to {{invoice.contact}} at the following
address:

{{invoice.address}}

This order was generated at {{some date/time expression}}
"""

Any suggestions appreciated.

Malcolm
7 Answers

Erik Max Francis

3/7/2008 2:44:00 AM

0

Malcolm Greene wrote:

> New to Python and looking for a template library that allows Python
> expressions embedded in strings to be evaluated in place. In other words
> something more powerful than the basic "%(variable)s" or "$variable"
> (Template) capabilities.
>
> I know that some of the web frameworks support this type of template
> capability but I don't need a web framework, just a library that
> supports embedded expression evaluation.
...
> Any suggestions appreciated.

EmPy may work:

http://www.alcyone.com/soft...

Your template would look something like:

myOutput = """
The total cost is @invoice.total.

This order will be shipped to @invoice.contact at the following
address:

@invoice.address

This order was generated at @time.ctime()
"""

This could be instrumented with something as simple as:

>>> import em, time
>>> myOutput = """....
.... The total cost is @invoice.total.
....
.... This order will be shipped to @invoice.contact at the following
.... address:
....
.... @invoice.address
....
.... This order was generated at @time.ctime()
.... """
>>>
>>> class Invoice: pass
....
>>> invoice = Invoice()
>>> invoice.total = "$123.45"
>>> invoice.contact = "Jack McCoy"
>>> invoice.address = "1 Police Plaza\nNew York City, NY"
>>> print em.expand(myOutput, globals())

The total cost is $123.45.

This order will be shipped to Jack McCoy at the following
address:

1 Police Plaza
New York City, NY

This order was generated at Thu Mar 6 18:41:58 2008

--
Erik Max Francis && max@alcyone.com && http://www.alcyon...
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
There's a reason why we / Keep chasing morning
-- Sandra St. Victor

Carl Banks

3/7/2008 3:40:00 AM

0

On Mar 6, 8:56 pm, "Malcolm Greene" <pyt...@bdurham.com> wrote:
> New to Python and looking for a template library that allows Python
> expressions embedded in strings to be evaluated in place. In other words
> something more powerful than the basic "%(variable)s" or "$variable"
> (Template) capabilities.
>
> I know that some of the web frameworks support this type of template
> capability but I don't need a web framework, just a library that
> supports embedded expression evaluation.
>
> Use case:
>
> myOutput = """>
> The total cost is {{invoice.total}}.
>
> This order will be shipped to {{invoice.contact}} at the following
> address:
>
> {{invoice.address}}
>
> This order was generated at {{some date/time expression}}
> """
>
> Any suggestions appreciated.


You could do something like this with a single function. For
instance:


import re

def apply_template(text,env):
def eval_in_env(m):
return str(eval(m.group(1),env))
return re.sub(r"{{(.*?)}}",eval_in_env,text)


Where env is a dict with the variables you want to evaluate (locals()
could be a good choice for this).

Standard warning about usage of eval: don't use this with untrusted
input or Bad Things can happen.


Carl Banks

Jeff McNeil

3/7/2008 3:41:00 AM

0

Cheetah (http://www.cheetahtem...) and Mako
(http://www.makotemp...) come to mind. Isn't there some long
running joke about new Python programmers creating their own template
language or something, too? =)

I know you said you don't want a web framework, but I've always been a
fan of Django templates in that they don't really allow you to do too
much inside the template itself. That seems to help keep a lot of
ex-PHP/JSP/ASP programmers I know honest.


On 3/6/08, Erik Max Francis <max@alcyone.com> wrote:
> Malcolm Greene wrote:
>
> > New to Python and looking for a template library that allows Python
> > expressions embedded in strings to be evaluated in place. In other words
> > something more powerful than the basic "%(variable)s" or "$variable"
> > (Template) capabilities.
> >
> > I know that some of the web frameworks support this type of template
> > capability but I don't need a web framework, just a library that
> > supports embedded expression evaluation.
>
> ...
> > Any suggestions appreciated.
>
> EmPy may work:
>
> http://www.alcyone.com/soft...
>
> Your template would look something like:
>
>
> myOutput = """>
> The total cost is @invoice.total.
>
> This order will be shipped to @invoice.contact at the following
> address:
>
> @invoice.address
>
>
> This order was generated at @time.ctime()
> """
>
> This could be instrumented with something as simple as:
>
> >>> import em, time
>
> >>> myOutput = """> ...
> ... The total cost is @invoice.total.
> ...
> ... This order will be shipped to @invoice.contact at the following
> ... address:
> ...
> ... @invoice.address
> ...
>
> ... This order was generated at @time.ctime()
> ... """
> >>>
> >>> class Invoice: pass
> ...
> >>> invoice = Invoice()
> >>> invoice.total = "$123.45"
> >>> invoice.contact = "Jack McCoy"
> >>> invoice.address = "1 Police Plaza\nNew York City, NY"
> >>> print em.expand(myOutput, globals())
>
> The total cost is $123.45.
>
> This order will be shipped to Jack McCoy at the following
> address:
>
> 1 Police Plaza
> New York City, NY
>
> This order was generated at Thu Mar 6 18:41:58 2008
>
>
> --
> Erik Max Francis && max@alcyone.com && http://www.alcyon...
> San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
> There's a reason why we / Keep chasing morning
> -- Sandra St. Victor
>
> --
> http://mail.python.org/mailman/listinfo/p...
>

Bernard

3/7/2008 4:38:00 AM

0

Cheetah! Cheetah! Cheetah!

On 6 mar, 20:56, "Malcolm Greene" <pyt...@bdurham.com> wrote:
> New to Python and looking for a template library that allows Python
> expressions embedded in strings to be evaluated in place. In other words
> something more powerful than the basic "%(variable)s" or "$variable"
> (Template) capabilities.
>
> I know that some of the web frameworks support this type of template
> capability but I don't need a web framework, just a library that
> supports embedded expression evaluation.
>
> Use case:
>
> myOutput = """>
> The total cost is {{invoice.total}}.
>
> This order will be shipped to {{invoice.contact}} at the following
> address:
>
> {{invoice.address}}
>
> This order was generated at {{some date/time expression}}
> """
>
> Any suggestions appreciated.
>
> Malcolm

Duncan Booth

3/7/2008 8:47:00 AM

0

"Malcolm Greene" <python@bdurham.com> wrote:

> New to Python and looking for a template library that allows Python
> expressions embedded in strings to be evaluated in place. In other
words
> something more powerful than the basic "%(variable)s" or "$variable"
> (Template) capabilities.
>
> I know that some of the web frameworks support this type of template
> capability but I don't need a web framework, just a library that
> supports embedded expression evaluation.

You could try using the Template class:

>>> from string import Template
>>> class EvalTemplate(Template):
idpattern = '[^{}]+'


>>> class EvalDict(dict):
def __getitem__(self, name):
if name in self:
return dict.__getitem__(self, name)
return eval(name, globals(), self)


>>> class Invoice:
def __init__(self, total):
self.total = total


>>> i = Invoice(42)
>>> template = EvalTemplate("The total cost is ${invoice.total}")
>>> template.substitute(EvalDict(invoice=i))
'The total cost is 42'

The usual caveats about using 'eval' apply (maybe it should be called
EvilDict), and I'm sure you could override substitute/safe_substitute to
construct the EvalDict transparently.

Roman Bertle

3/7/2008 10:05:00 AM

0

* Malcolm Greene <python@bdurham.com>:
> New to Python and looking for a template library that allows Python
> expressions embedded in strings to be evaluated in place. In other words
> something more powerful than the basic "%(variable)s" or "$variable"
> (Template) capabilities.
[...]
>
> Use case:
>
> myOutput = """>
> The total cost is {{invoice.total}}.
[...]

You might look at YAPTU
http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...
or YAPTOO
http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...,
very small but powerful templating engines. I use YAPTU myself for
invoice templating.

Regards, Roman

Stefan Behnel

3/7/2008 10:22:00 AM

0

Jeff McNeil wrote:
> Isn't there some long
> running joke about new Python programmers creating their own template
> language or something, too? =)

http://article.gmane.org/gmane.comp.python.gene...

Stefan