Guest User

Untitled

a guest
Oct 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. class PostgreSQLConnection(psycopg2.extensions.connection):
  2. """
  3. A custom `connection_factory` for :func:`psycopg2.connect`.
  4.  
  5. This
  6. * puts the connection into unicode mode (for text)
  7. * modifies the :meth:`cursor` method of a :class:`psycopg2.connection`,
  8. facilitating easy acquiring of cursors made from
  9. :cls:`psycopg2.extras.RealDictCursor`.
  10. """
  11.  
  12. # this may be omitted in py3k
  13. def __init__(self, *args, **kwargs):
  14. super(PostgreSQLConnection, self).__init__(*args, **kwargs)
  15. for type in (psycopg2.extensions.UNICODE,
  16. psycopg2.extensions.UNICODEARRAY):
  17. psycopg2.extensions.register_type(type, self)
  18.  
  19. def cursor(self, real_dict_cursor=False):
  20. """
  21. Get a new cursor.
  22.  
  23. If real_dict_cursor is set, a RealDictCursor is returned
  24. """
  25.  
  26. kwargs = {}
  27. if real_dict_cursor:
  28. kwargs["cursor_factory"] = psycopg2.extras.RealDictCursor
  29. return super(PostgreSQLConnection, self).cursor(**kwargs)
Add Comment
Please, Sign In to add comment