python-decorated-templates

Python templating strategy involving decorators and inline expressions
Download

python-decorated-templates Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Caleb P. Burns
  • Publisher web site:
  • https://github.com/cpburnz/

python-decorated-templates Tags


python-decorated-templates Description

Why bother working with embedded templating languages within Python when Python is already a fully functional and extensible scripting language perfect for templating? python-decorated-templates is a Python module that provides a straight forword templating strategy for Python. All that's involed is decorating your template functions, and all expressions within them will be concatenated and returned at function completion.TemplatesHere's an example using a simple template:: import ptd @pdt.template def spam(eggs, ham=None): ''' This would normally be the doc string but this is going to be outputted like an expression. ''' eggs # Output some eggs sum(xrange(10)) # Output 45 "Here's another string expression." print "These are still regular print statements that will be" print "printed to stdout." if not ham: # All expressions up to this point will be returned with the # empty return statement. return "Have some %s ham." % ham # Output the ham # All expressions outputted will be return at the end of a # template.Here's what the template will look like after being recompiled:: import pdt def spam(eggs, ham=None) _buffer = pdt.ListIO() _buffer.write(''' This would normally be the doc string but this is going to be outputted like an expression. ''') _buffer.write(eggs) # Output some eggs. _buffer.write(sum(xrange(10))) # Output 45 _buffer.write("Here's another string expression.") print "These are still regular print statements that will be" print "printed to stdout." if not ham: # All expressions up to this point will be returned with the # empty return statement. return _buffer.getvalue() _buffer.write("Have some %s ham." % ham) # Output the ham # All expressions outputted will be returned at the end of a # template. return _buffer.getvalue()Template IO BufferTemplates use an internal buffer to store expression results which willbe returned at the end of the function. A custom buffer factory functionand arguments can be specified with:: import pdt @pdt.template(io_factory=myfactory, io_args=myargs, io_kw=mykeywords) def spam(...):*io_factory* (**callable**) creates ``file``-like instances implementing *write()* and *getvalue()* when called. Typically, this will be a class object. By default this is ``ListIO``.*io_args* (``tuple``) optionally specifies any positional arguments passed to *io_factory* when it is called. Default is an empty ``tuple``.*io_kw* (``dict``) optionally specifies keyword arguments passed to*io_factory* when it is called. Default is an empty ``dict``.Here's a simplified version of the ``ListIO`` class: class SimpleListIO(object): def __init__(self): self.buff = [] def write(self, data): if data is not None: self.buff.append(str(data)) def getvalue(self): return "".join(self.buff) import pdt @pdt.template(io_factory=SimpleListIO) def spam(...): ...Here's an example IO Buffer that encodes the results and stores them using ``cStringIO``:: import cStringIO class CustomIO(object): def __init__(self, encoding='utf8'): self.buff = cStringIO.StringIO() self.enc = encoding def write(self, data): if data is not None: self.buff.write(unicode(data).encode(self.enc)) def getvalue(self): return self.buff.getvalue() import pdt @pdt.template(io_factory=CustomIO, io_kw={'encoding': 'latin1'}) def spam(...):The *io_args* and *io_kw* are passed as positional and keyword argumentsto *io_factory* which is the class constructor.The *write()* function will receive the result of each expression in the first (*data*) argument. *data* will have to be converted to either a ``str`` or ``unicode`` manually. If *data* is ``None``, it should be ignored so functions which do not return a value (i.e., ``None``) do not output "None" for each call.The *getvalue()* function returns the concatenated ``str`` or ``unicode`` result of every expression sent to *write()*.ImplementationPDT is inspired by Quixote's PTL_ (Python Template Language) but without the need for special file syntax, extensions and import hooks. The PDT template decorator modifies the source of wrapped functions, and recompiles them to allow for the expression output... _PTL: http://quixote.ca/Only in source ``def``ed functions are supported. Functions for which their text source (not byte code) is not available are not supported. Neither are closures, generators, nor are ``lambda``s supported. Functions can only be decorated above/after (not below/before) being decorated as a template... NOTE: Generator functions might be supported in the future.Product's homepage


python-decorated-templates Related Software