[Twisted-Python] Using different cursor types with psycopg+twisted.enterprise.adbapi?

Vlado Handziski handzisk at tkn.tu-berlin.de
Fri Apr 18 14:12:36 MDT 2008


On Fri, Apr 18, 2008 at 7:09 PM, Nathan <nathan.stocks at gmail.com> wrote:
> In my non-twisted scripts, I usually use psycopg2 DictCursors so that
>  my query results come back in dicts instead of lists.
>
>  curs = db.cursor(cursor_factory = psycopg2.extras.DictCursor)
>
>  Now I'd like to do the same thing in Twisted.  Is there a way to ask
>  t.e.adbapi to use that DictCursor cursor factory?
>
>  Or maybe there's a way to globally configure psycopg2 so that it used
>  psycopg2.extras.DictCursor by default...
>

I am using the hack below from Federico Di Gregorio to force a
RealDictCursor in my adbapi stores:

#module twistedpg.py
#Author: Federico Di Gregorio

from psycopg2 import *
from psycopg2 import _psycopg as _2psycopg
from psycopg2.extensions import register_type, UNICODE, connection as
_2connection
from psycopg2.extras import RealDictCursor

del connect
def connect(*args, **kwargs):
    kwargs['connection_factory'] = connection
    return _2psycopg.connect(*args, **kwargs)

class connection(_2connection):
    def cursor(self):
        return _2connection.cursor(self, cursor_factory=RealDictCursor)

register_type(UNICODE)

and then:

        self.dbpool = adbapi.ConnectionPool('twistedpg',
                                            database=dname,
                                            user=duser,
                                            password=dpasswd,
                                            host=dhost,
                                            cp_reconnect=True)

etc...

Vlado




More information about the Twisted-Python mailing list