Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
        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)
Ejemplo n.º 3
0
            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
Ejemplo n.º 4
0
def bytea2bytes(value, cur):
    m = psycopg2.BINARY(value, cur)
    if m is not None:
        return m.tobytes()
Ejemplo n.º 5
0
def cast_pickle(data, cur):
    if data is None: return None
    return cPickle.loads(str(psycopg2.BINARY(data, cur)))
Ejemplo n.º 6
0
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')
Ejemplo n.º 7
0
def typecast_bytea(value, cur):
    if value is not None:
        data = psycopg2.BINARY(value, cur)
        return data.tobytes()