I am new to Python - be forewarned!
I have a template class that takes a list as an argument and applies the members of the list to the template. Here's the first class:
class ListTemplate:
def __init__(self, input_list=[]):
self.input_list = input_list
def __str__(self):
return "\n".join([self._template % x for x in self.input_list])
Here's the template code/class:
class HTML_Template(ListTemplate):
_template = """
<li>
<table width="100%%" border="0">
<tr>
<td>Title: %(title)s</td>
<td>Link: %(isbn)s</td>
</tr>
<tr>
<td>Author: %(author)s</td>
<td>Date: %(pub_date)s</td>
</tr>
<tr>
<td>Summary: %(isbn)s</td>
<td>Description: %(descr)s</td>
</tr>
</table>
<hr>
</li>"""
Here's the problem:
I'm pulling data from a sqlite database using fetchall, which return a list of tuples. I can pass this list of tuples to this method fine by changing the template to %s instead of %(isbn) ...etc., however, I need to access the isbn member multiple times in the template. To do this now, I load the tuples in to dictionaries and push them into another list ...then pass the list of dictionaries. This strikes me as inefficient. My question is: how would you rewrite this so that the original list of tuples could be passed to the template (thus, saving a step ), while still allowing access to the individual members by ie., item[0] or similar?
Thanks ~
Bubnoff