Loading select queries into objects
Graham Ashton
graham at coms.com
Tue Jun 19 19:50:14 EDT 2001
In article <wGpX6.75343$r4.827948 at e420r-atl3.usenetserver.com>, "Steve
Holden" <sholden at holdenweb.com> wrote:
> From http://www.lyra.org/greg/python/:
>
> """dtuple.py
> This module wraps up return tuples from the fetch* methods in the Python
> Database API. Using this class, the return tuples can be treated as
> tuples, dictionaries, or objects with attributes corresponding to the
> column names.
Thanks for that - very interesting link. I've been playing with dtuple a
bit and have managed to get it to work fine by ripping off the standard
example. Here's what I've got:
rows = cursor.fetchone()
tdesc = dtuple.TupleDescriptor(cursor.description)
rows = dtuple.DatabaseTuple(tdesc, rows)
dict = rows.asMapping()
print dict
It works great. When it prints things out they look like this:
{'col1': 234, 'col2': 'foo', 'some_col': 9473.0}
No, my question would be how would I take the above dictionary and convert
it into calls to a col1(), col2() and some_col() methods (in the current
class). I've tried the following and am completely fathomed as to why the
second parameter (i.e. the value of dict['key']) doesn't get passed to the
method that I'm calling.
for key in dict.keys():
code = "apply(self.%s(), (self, dict['%s']))" % (key, key)
print code
eval(code)
The code printed looks like this:
apply(self.col1(), (self, dict['col1']))
The only problem is that when it's evaluated the self.col1() method
doesn't get passed the value of dict['col1'], though self.col1() is
called. Is there something I don't know about eval()? I've read up on it
but don't see anything relevant...
Should eval() do the trick here?
Thanks.
More information about the Python-list
mailing list