public inbox for [email protected]  
help / color / mirror / Atom feed
From spycopg2 to psycopg3 data adaptation
2+ messages / 2 participants
[nested] [flat]

* From spycopg2 to psycopg3 data adaptation
@ 2021-10-05 11:29 Paolo De Stefani <[email protected]>
  2021-10-05 12:05 ` Re: From spycopg2 to psycopg3 data adaptation Daniele Varrazzo <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Paolo De Stefani @ 2021-10-05 11:29 UTC (permalink / raw)
  To: psycopg

Hi
Still working on migration to psycopg3 of my (not so) small 
application...
I use PyQt\PySide for user interface and i convert some postgresql data 
types to Qt types.
For example to convert timestamptz to QDateTime in psycopg2 i use:

def cast_timestamp_qdatetime(value, cur):
     if value is None:
         return None
     dt = QDateTime.fromString(value[:23], "yyyy-MM-dd HH:mm:ss.zzz")
     if not dt.isValid(): # no milliseconds
         dt = QDateTime.fromString(value[:19], "yyyy-MM-dd HH:mm:ss")
     return dt

QDATETIME = psycopg2.extensions.new_type((1184,), "QDATETIME", 
cast_timestamp_qdatetime)
psycopg2.extensions.register_type(QDATETIME)


I tryed this to achieve the same result in psycopg3:

class TimestamptzQDateTimeLoader(Loader):
     def load(self, value):
         if value is None:
             return None
         print(value)
         dt = QDateTime.fromString(value[:23], "yyyy-MM-dd HH:mm:ss.zzz")
         if not dt.isValid(): # no milliseconds
             dt = QDateTime.fromString(value[:19], "yyyy-MM-dd HH:mm:ss")
         return dt

psycopg.adapters.register_loader('timestamptz', 
TimestamptzQDateTimeLoader)

BUT it's not working because the "value" is binary not string:

<memory at 0x000000000939BA00>

So how i can get the same result of psycopg2 in psycopg3 ?


-- 
Paolo De Stefani





^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: From spycopg2 to psycopg3 data adaptation
  2021-10-05 11:29 From spycopg2 to psycopg3 data adaptation Paolo De Stefani <[email protected]>
@ 2021-10-05 12:05 ` Daniele Varrazzo <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Daniele Varrazzo @ 2021-10-05 12:05 UTC (permalink / raw)
  To: Paolo De Stefani <[email protected]>; +Cc: psycopg

On Tue, 5 Oct 2021 at 13:30, Paolo De Stefani <[email protected]> wrote:

> class TimestamptzQDateTimeLoader(Loader):
>      def load(self, value):
>          if value is None:
>              return None

A `None` will never make it here: you can drop this check in psycopg 3.


> BUT it's not working because the "value" is binary not string:
>
> <memory at 0x000000000939BA00>
>
> So how i can get the same result of psycopg2 in psycopg3 ?

You can use bytes(value) to get the value as bytes string, and if you
need a str you can use bytes(value).decode()

-- Daniele





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2021-10-05 12:05 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-10-05 11:29 From spycopg2 to psycopg3 data adaptation Paolo De Stefani <[email protected]>
2021-10-05 12:05 ` Daniele Varrazzo <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox