c0dem4gnetic
c0dem4gnetic

Reputation: 942

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.

Upvotes: 4

Views: 743

Answers (2)

charli
charli

Reputation: 1778

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)

Upvotes: 2

piro
piro

Reputation: 13931

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.

Upvotes: 2

Related Questions