Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Looking for very light weight template library (not framework)

Reply
Thread Tools

Looking for very light weight template library (not framework)

 
 
Malcolm Greene
Guest
Posts: n/a
 
      03-07-2008
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
 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      03-07-2008
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/software/empy/

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 && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
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
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      03-07-2008
On Mar 6, 8:56 pm, "Malcolm Greene" <(E-Mail Removed)> 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
 
Reply With Quote
 
Jeff McNeil
Guest
Posts: n/a
 
      03-07-2008
Cheetah (http://www.cheetahtemplate.org/) and Mako
(http://www.makotemplates.org/) 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 <(E-Mail Removed)> 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/software/empy/
>
> 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 && (E-Mail Removed) && http://www.alcyone.com/max/
> 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/python-list
>

 
Reply With Quote
 
Bernard
Guest
Posts: n/a
 
      03-07-2008
Cheetah! Cheetah! Cheetah!

On 6 mar, 20:56, "Malcolm Greene" <(E-Mail Removed)> 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


 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      03-07-2008
"Malcolm Greene" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Roman Bertle
Guest
Posts: n/a
 
      03-07-2008
* Malcolm Greene <(E-Mail Removed)>:
> 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/Coo.../Recipe/52305A
or YAPTOO
http://aspn.activestate.com/ASPN/Coo.../Recipe/465508,
very small but powerful templating engines. I use YAPTU myself for
invoice templating.

Regards, Roman
 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      03-07-2008
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....general/544922

Stefan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Light-weight/very-simple version control under Windows using Python? python@bdurham.com Python 2 07-25-2010 06:42 PM
Light weight library Diilb Javascript 18 02-22-2007 01:40 AM
Show light weight thumbnail of large images moondaddy ASP .Net 4 06-29-2004 03:39 PM
light weight messaging without mdb j Java 2 11-26-2003 07:41 PM
How to create multiple light-weight process in Java Song Ching Koh Java 2 10-31-2003 02:51 PM



Advertisments