def ensure_postgresql_casts() -> None: """Ensure that psycopg2 will handle some data types right.""" psycopg2.extensions.register_adapter(bytes, psycopg2.Binary) typecast_bytea = lambda data, cur: None if data is None else bytes( psycopg2.BINARY(data, cur)) # noqa BYTEA = psycopg2.extensions.new_type(psycopg2.BINARY.values, "BYTEA", typecast_bytea) psycopg2.extensions.register_type(BYTEA)
def _typecast_array(value, cur): if value is None: return None data = psycopg2.BINARY(value, cur) bdata = io.BytesIO(data) bdata.seek(0) return np.load(bdata)
def normalize_str(v, cur): if isinstance(v, str) and v.startswith("\\x"): buf = psycopg2.BINARY(v, cur) v = bytes(buf).decode(cur.connection.encoding) #import binascii #pout.v(binascii.unhexlify(v[2:])) #v = v.encode(cur.connection.encoding) #v = bytes(v, encoding=cur.connection.encoding) return v
def bytea2bytes(value, cur): m = psycopg2.BINARY(value, cur) if m is not None: return m.tobytes()
def cast_pickle(data, cur): if data is None: return None return cPickle.loads(str(psycopg2.BINARY(data, cur)))
def cast_pickle(data, cur): ''' Function to retrieve binary data from Postgresql ''' if data is None: return None return pickle.loads(psycopg2.BINARY(data, cur), encoding='latin1')
def typecast_bytea(value, cur): if value is not None: data = psycopg2.BINARY(value, cur) return data.tobytes()