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

Tung Wai Yip tungwaiyip at yahoo.com
Tue Feb 13 23:55:25 CET 2007


On Tue, 13 Feb 2007 14:33:04 -0800, Shannon -jj Behrens <jjinux at gmail.com>  
wrote:

> On 2/9/07, Dennis Reinhardt <DennisR at dair.com> wrote:
>> At 08:28 PM 2/9/2007, Laszlo Antal wrote:
>> >Hi,
>> >This is a great thread.
>> >I want to thank everyone for sharing their wisdom.
>> >Keep it coming.
>>
>> I *was* going to duck out because I was outnumbered by those favoring
>> triple quoting.
>>
>> Yes, triple quoting can work for simple situations.  The triple quoting
>> examples displayed in this thread have been just that: simple.  They do  
>> not
>> include conditionally executed code or do parameter substitution.  What
>> they do is obscure the block structure of the code.
>>
>> Here is a code fragment from my talk last night (foil 15, I believe).   
>> How
>> would you re-write this to utilize triple quoting?  The task here is to
>> build a two level visual tree structure:
>>
>> # build entire initial tree structure
>> def init_tree(ht_obj):
>>      html = "<table border=0>"
>>      index = 0
>>      last_exe = ""
>>      for xml_entry in util_actwin.display_list("enm", "+"):
>>          exe_name = str_extract(xml_entry, ["enm"])
>>          keye   = safe_int(str_extract(xml_entry, ["key"]))
>>          act = str_extract(xml_entry, ["act"])
>>          #logger("init_tree %s %s" % (keye, exe_name))
>>          fname, exe = exe_name.split(".")
>>          if fname != last_exe:
>>              html += "<tr>"
>>              if fname == expanded_node:
>>                  html += "<td><img src=image/slct32.gif border=0></td>"
>>              else:
>>                  html += "\r\n<td><a
>> href=edt_main.py?slct=expand&fname=%s>" % fname
>>                  html += "<%s></a></td>\r\n" % hovered_img(ht_obj,  
>> index)
>>              html += "<td><img src=%s width=32 height=32></td>" %
>> get_program_image(fname)
>>              html += "<td>%s</td>" % fname
>>              html += "<td>&nbsp;</td>"
>>              html += "</tr>"
>>          if fname == expanded_node:
>>              html += "<tr>"
>>              html += "<td>&nbsp;</td>"
>>              parm_index = safe_int_default(ht_obj.param("index"), -1)
>>              if parm_index == keye:
>>                  html += "<td><img src=image/slct32.gif border=0></td>"
>>                  html += "<td><img src=%s " %  
>> get_action_image(xml_entry)
>>                  html +=     "width=32 height=32 border=0></td>"
>>              else:
>>                  html += "<td><a href=edt_main.py?slct=edit"
>>                  html +=     "&class=tree_open32"
>>                  html +=     "&index=%s"       % keye
>>                  html +=     "&act=%s"         % act
>>                  html +=     "><%s></a></td>" % hovered_img(ht_obj,  
>> index)
>>                  html += "<td><img src=%s " %  
>> get_action_image(xml_entry)
>>                  html +=     "width=16 height=16 border=0></td>"
>>              html += "<td>%s</td>" % get_caption(xml_entry)
>>              html += "</tr>"
>>          else:
>>              pass
>>          last_exe = fname
>>          index += 1
>>      html += "</table>"
>>      return html
>
> Seriously, that's what a templating language is used for!  I recommend
> Cheetah, Myghty, Mako, or Genshi, depending on your tastes.  Please
> excuse me if I say that the above is *ugly*.  It's much better to
> break what you can into Python, and use a templating language for the
> rest.
>
> Best Regards,
> -jj
>


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.

Wai Yip




More information about the Baypiggies mailing list