SV: Function to convert fetchone() result from a list to a dictionary

Alex Martelli aleax at aleax.it
Tue Apr 29 08:37:17 EDT 2003


<posted & mailed>

Carsten Gehling wrote:

>> -----Oprindelig meddelelse-----
>> Fra: python-list-admin at python.org
>> [mailto:python-list-admin at python.org]Pa vegne af Alex Martelli
>> Sendt: 29. april 2003 11:09
> 
>> Yes.  I think what you REALLY want (even though you have no way to
>> know you want it:-) is Greg Stein's wonderful dtuple.py, see:
>>
>> http://www.lyra.org/greg/python/dtuple.py
>>
>> The best explanation and set of examples I know for this is in Steve
>> Holden's "Python Web Programming" (New Riders) -- I highly recommend
> 
> I already have this book, but unfortunately (for me) the chapters about
> database manipulation is about the only section of the book, that I
> haven't shuffled through. :-)

Excellent choice in books -- but if you're into databases, strange
choice of chapters;-).

> Looking at dtuple.py I think it may be shooting a bit over target. I could
> be wrong - after all, I'm very new to Python :)

Ah, but you can USE dtuple even without fully GRASPING it yet, see!


>> if you can't purchase it I think you can at least get its examples
>> free off the web (start looking at Steve's site, www.holdenweb.com).
> 
> Will do this!
> 
>> Pretty good (except that you're using tabs, which don't show up
>> when I reply with KDE 3's KNode:-)  You can do a bit better (for
> 
> Heh - I like using tabs. It makes it easier for me to control indentation.

...but not everybody's using your tools of choice.  Mail and news
readers, editors, printers & their drivers, etc, are all liable to
do weird things with tabs.  Using spaces you DO control indentation;
using tabs you don't, really, unless you can control the programs
used by everybody you might like to share your code with (e.g. to
ask for help about it).


>> So here's a step-by-step version (using SPACES, not TABS:-)...:
>>
>>
>> def cfd(cursor):
>>     values = cursor,fetchone()
>>     if values is None:
>>         return None
>>     keys = [d[0] for d in cursor.description]
>>     result_dict = dict(zip(keys, values))
>>     return result_dict
> 
> Hmm... (looking up zip() in the documentation...). Okay I understand
> everyting except this contruct:
> 
> keys = [d[0] for d in cursor.description]
> 
> Let me guess: it loops through cursor.description (obvious), for each of
> the items (which are tuples) it "outputs" element 0. The output is then
> automatically formatted in a way, that conforms to the list creation
> syntax.

Good guessing!  It's called a "list comprehension", and it's one of
Python's powerful constructs.

> Or something like that. Kind of neat - haven't seen that in any other
> language, that I've used.

Python mutuated it from the functional-programming language Haskell,
but in Haskell it looks a bit less readable (to me, at least) because
it uses puntuation instead of keywords such as "for" and "in".

>>     return dict(zip([d[0] for d in cursor.description],values))
>>
>> if you're into one-liners!-)
> 
> I'm not - I'd like to be able to read my own code 6 months from now... ;-)

Yep.  There's a balance between terseness and verbosity that makes
for best readability.  Me, I'd prepare the list of keys separately
(as in the cfd function above) and then compact the last TWO lines,
only, into a single return statement:

    return dict(zip(keys, values))

as being the best compromise in this specific case -- but, where
exactly to put the threshold IS a matter of tastes.

> BTW: Wish me welcome - I've just subscribed. And the way I continue to be
> marveled by Python, I'm not likely to unsibscribe again. :-)

Welcome!  Python IS indeed marvelous.


Alex





More information about the Python-list mailing list