Better access to database search results
Gabriel Cooper
gabriel.cooper at mediapulse.com
Tue Apr 12 17:03:41 EDT 2005
Simon Brunning wrote:
>On Apr 12, 2005 4:32 PM, Gabriel Cooper <gabriel.cooper at mediapulse.com> wrote:
>
>
>>Usually when I access db search results it's something like this:
>>
>>cursor.execute("select A1,A2,A3,A4 from B where C")
>>for (a1,a2,a3,a4) in cursor.fetchall():
>> stuff()
>>
>>But sometimes the point at which I use the data returned is not with the
>>search, and so having the ability to access the results as a dictionary
>>is more useful.
>>
>>Let me know what you think. Could this be done simpler?
>><snip>
>>
>>
>
>Have you come across dtuple?
>(<http://www.lyra.org/greg/python/dtuple.py>). It's really easy to use
>- <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81252>.
>
>
Hm. that's interesting, it's pretty much the same idea, only I do all
the processing up front and return a complete variable that's generally
indistinguishable from a list of dictionaries. This method (at least in
the ASPN example) appears to prefer that you process each record as you
come to it (which is undoubtedly more scalable). For my purposes, where
as little programming logic as possible is required in the "view"
section of code, I'm willing to accept the idea that I won't be using my
function on immense amounts of data.
To explain, I develop websites using Snakeskin
(http://snakeskin-tools.sf.net/). It separates design and logic by first
processing a python object with specific event triggers and then loads
the requested template to accompany it. The python object does the
logic, the template does the display. e.g.
def page_process(self, ctx):
c = ctx.request.db.cursor()
c.execute("select a,b,c ....")
ctx.locals.some_var = c.fetchall() # anything in ctx.locals is
available as a var in the template
then in the template:
<html>
<body>
...
<ss-for expr="some_var" iter="i" expand="record">
Print out A: <ss-value expr="record.a"/>
Print out B: <ss-value expr="record['b']"/>
...
</ss-for>
...
</body>
</html>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050412/3da5976a/attachment.html>
More information about the Python-list
mailing list