Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple implementations for a psycopg2 cursor

How can I mix cursor implementations in psycopg2? In my case I want the cursor to act both as a NamedTupleCursor and a LoggingCursor.

like image 832
c0dem4gnetic Avatar asked Sep 02 '25 09:09

c0dem4gnetic


2 Answers

Currently not, I've committed just a few days ago a patch to enable cooperative subclassing.

The logging cursor is nothing special, it's more a demo than something really useful: I suggest you to subclass the NamedTupleCursor and add the logging statements you need, taking a look at the LoggingCursor as a hint.

like image 88
piro Avatar answered Sep 04 '25 23:09

piro


For the record, with @piro patch is possible using a Mixin:

from psycopg2.extras import LoggingConnection,LoggingCursor,RealDictCursor

class MixinLoggedDictCursor(LoggingCursor, RealDictCursor):
   pass

conn = psycopg2.connect(
    cursor_factory=MixinLoggedDictCursor,
    connection_factory=LoggingConnection,
    **kwargs
)
conn.initialize(my_logger)
like image 21
charli Avatar answered Sep 04 '25 23:09

charli