[Baypiggies] Discussion for newbies/beginner night talks
Aahz
aahz at pythoncraft.com
Wed Feb 14 05:18:50 CET 2007
[top-posting for once to preserve Dennis's code without require people
to read over it for my material]
There are several aspects to your code that I would object to if you
were submitting it to me for code review:
* Your internal indentation makes this difficult to maintain
* URL generation should be done with function calls, for example:
def make_link(url, **kwargs):
q = url_quote
params = [ q(i[0])+'='+q(i[1]) for i in kwargs.items() ]
params = '&'.join(params)
return url + '?' + params
make_link('edt_main.py', slct='edit', class='tree_open32',
index=key, act=act)
* Using += makes for bad performance
* Hardcoding image size attributes is horrible
* For that matter, hardcoding URLs is a Bad Idea
* Interleaving program logic with HTML makes it difficult to follow both
Triple-quoting doesn't solve any of these problems directly, but it
would allow you to take more of a templating approach, particularly if
you used dict interpolation instead of tuple interpolation.
On Fri, Feb 09, 2007, Dennis Reinhardt wrote:
>
> 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> </td>"
> html += "</tr>"
> if fname == expanded_node:
> html += "<tr>"
> html += "<td> </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
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"I disrespectfully agree." --SJM
More information about the Baypiggies
mailing list