In psycopg2, register_composite you can specify a factory parameter where:
factory – if specified it should be a CompositeCaster subclass: use it to customize how to cast composite types
found in https://www.psycopg.org/docs/extras.html#custom-composite
is there a psycopg 3.0.9 factory subclass that is the equivalent of a CompositeCaster subclass in psycopg2? The docs for psycopg3 are https://www.psycopg.org/psycopg3/docs/basic/pgtypes.html but they dont really say much about the factory parameter and if theres a subclass I can use to customize how to cast the composite types.
register_composite
can optionally take afactory
parameter.factory
should be a function that takes as many parameters as fields the custom type defined on your database has. Thefactory
takes the fields as arguments and should return a Python object that will replace thepsycopg.types.composite.XXXX
instance that you would get otherwise.For example in my database I have the following type:
however when I read quaternions on my Python program I would like them to be the quaternions from the numpy-quaternion library. To achieve that I can do the following: