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

Vlado Handziski handzisk at tkn.tu-berlin.de
Fri Apr 18 16:17:46 MDT 2008


On Fri, Apr 18, 2008 at 10:38 PM, Nathan <nathan.stocks at gmail.com> wrote:
>
> On Fri, Apr 18, 2008 at 2:12 PM, Vlado Handziski
>  <handzisk at tkn.tu-berlin.de> wrote:
>  >  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)
>
>  Oh, ok!  That makes sense.  So since twisted just calls the underlying
>  db module's connect() function, you just (in essence) sub-classed the
>  whole psycopg2 module and overrode connect() to use the DictCursor.
>
>  I don't understand what the "UNICODE" stuff is about, though.  Do you
>  know what that is?
>

Effectively it is forcing psycopg2 to convert the results to unicode.
I am using it so that unicode form data can be transparently
read/written in the DB.

Vlado




More information about the Twisted-Python mailing list