[Baypiggies] Output generation - (string operations/triple quote/template)

Tung Wai Yip tungwaiyip at yahoo.com
Wed Feb 14 01:17:24 CET 2007


> On Tue, 13 Feb 2007, Tung Wai Yip wrote:
>
>> My HTMLTestRunner is a real world example of generating moderately  
>> complex
>> text output using triple quoted text blocks.
>>
>>     http://tungwaiyip.info/software/HTMLTestRunner.html
>>
>> I agree with JJ that template language is another good alternative,
>> especially for more complex generation. While this is subjective and
>> everyone have different criteria, I consider the approach of  
>> interleaving
>> Python code with the target output hard to maintain. It goes back to the
>> Java Servlet approach and the many addon projects later to provide  
>> better
>> alternatives.
>
> Are you sort-of saying that having a separate template is like having a  
> ".h" file, and that keeping code+data local in the program makes things  
> easier to understand, and, therefore, perhaps Python needs to include  
> better templating features?
>
> (Yes, I know I'm putting lots of words in your mouth, but I'd appreciate  
> more detail on what you mean.)
>
> Thanks,
>
> -Mike


Sorry for not explaining myself sufficiently. I was contrasting different  
approach of generating non-trivial text output using Python.


1. Use string operations (+, %, etc)

This is the most straight forward approach with no dependency on external  
library. With Python code it can make use of arbitrary logic. The con is  
it leads to mixing of Python code and target text that made it hard to  
read. Too much Python logic sprinkled inside would make the flow  
especially hard to follow.


2. Use triple quoted text block

This is the similar to 1. except you can group more target text into a  
larger unit, thus provide a higher degree of separation of Python code and  
the target output. Also the ' and " characters do not need to be escaped  
inside a text block, therefore it is a big boon in readability when you  
are generating text with a lot of quotes (like HTML or XML).


3. Use a template langauge

By template language it means the likes of Cheetah, Myghty, Mako, or  
Genshi as JJ mentioned. The parallel in Java world is JSP and Velocity.  
They are routinely used in webpage generation because they are more  
powerful and makes complex project easier to maintain. It also mean to  
create an interfacing point between programmers and designers. The con is  
the dependency on third party library, more new language to learn and also  
a risk of lock-in to a third-party language. Also while the goal is  
provide a high degree of separation between code and the template, in  
practice it is never perfect and we often find code spill over to the  
template.


Depends on the complexity of the problem, all approach has their  
appropiate use. In theory more separation is better. But if you have a  
simple problem, it maybe burdensome to invoke a template library and edit  
two separate files.

I especially emphasize triple quote not because it the best solution to  
every problem but that it is an overlooked feature in Python that often  
significantly improve string operations as oppose to using a lot of string  
concatenations. It is also a big improvement in readability.


Wai Yip







More information about the Baypiggies mailing list