[Twisted-Python] How to properly return a dict from adbapi

Itamar Shtull-Trauring itamar at itamarst.org
Mon Feb 19 21:51:36 EST 2007


On Mon, 2007-02-19 at 16:39 +0100, "Einar S. Idsø" wrote:
> Hi,
> 
> I am currently using the below technique to return a list of dicts
> instead of a tuple of tuples from adbapi. Is there a cleaner way?
> 
> def dictQuery(self, query):
>    self.execute(query)
>    result = self.fetchall()
>    columns = [d[0] for d in self._cursor.description]
>    return [dict(zip(columns, r)) for r in result]

Some database adapters will return objects that act like dictionaries,
or have an extra method that does so. adabpi itself doesn't really
provide any extra infrastructure for doing so since it's intended as a
thin layer wrapping db-api.





More information about the Twisted-Python mailing list