Skip site navigation (1) Skip section navigation (2)

Re: Using a psycopg2 converter to retrieve bytea data from PostgreSQL

From: Andy Casey <andycasey(at)gmail(dot)com>
To: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
Cc: "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org>
Subject: Re: Using a psycopg2 converter to retrieve bytea data from PostgreSQL
Date: 2012-05-21 08:49:44
Message-ID: 6B6D422C-8149-4AB0-9DC3-12EDD71DBADA@gmail.com (view raw or flat)
Thread:
Lists: psycopg
Brilliant!

Thank you immensely Daniele!

Cheers,
Andy

Sent from my magical and revolutionary device

On 21/05/2012, at 6:22 PM, Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> wrote:

> On Mon, May 21, 2012 at 5:32 AM, Andy Casey <andycasey(at)gmail(dot)com> wrote:
>> Hi,
>> 
>> I'm just re-raising this problem to anyone on the mailing list, because I
>> haven't had any luck on StackOverflow, or any suggested answers from the
>> mailing list:
>> 
>>  http://stackoverflow.com/questions/10529351/using-a-psycopg2-converter-to-retrieve-bytea-data-from-postgresql
> 
> Sorry, I forgot to get back on the question. The default bytea
> typecaster (which is the object that can parse the postgres binary
> representation and return a buffer object out of it) is
> psycopg2.BINARY. We can use it to create a typecaster converting to
> array instead:
> 
> 
> In [1]: import psycopg2
> 
> In [2]: import numpy as np
> 
> In [3]: a = np.eye(3)
> 
> In [4]: a
> Out[4]:
> array([[ 1.,  0.,  0.],
>       [ 0.,  1.,  0.],
>       [ 0.,  0.,  1.]])
> 
> In [5]: cnn = psycopg2.connect('')
> 
> In [6]: cur = cnn.cursor()
> 
> 
> # The adapter: converts from python to postgres
> # note: this only works on numpy version whose array support the
> buffer protocol,
> # e.g. it works on 1.5.1 but not on 1.0.4 on my tests.
> 
> In [12]: def adapt_array(a):
>   ....:     return psycopg2.Binary(a)
>   ....:
> 
> In [13]: psycopg2.extensions.register_adapter(np.ndarray, adapt_array)
> 
> 
> # The typecaster: from postgres to python
> 
> In [21]: def typecast_array(data, cur):
>   ....:     if data is None: return None
>   ....:     buf = psycopg2.BINARY(data, cur)
>   ....:     return np.frombuffer(buf)
>   ....:
> 
> In [24]: ARRAY = psycopg2.extensions.new_type(psycopg2.BINARY.values,
> 'ARRAY', typecast_array)
> 
> In [25]: psycopg2.extensions.register_type(ARRAY)
> 
> 
> # Now it works "as expected"
> 
> In [26]: cur = cnn.cursor()
> 
> In [27]: cur.execute("select %s", (a,))
> 
> In [28]: cur.fetchone()[0]
> Out[28]: array([ 1.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.])
> 
> 
> As you know, np.frombuffer(a) loses the array shape, so you will have
> to figure out a way to preserve it.
> 
> Cheers,
> 
> -- Daniele

In response to

psycopg by date

Next:From: W. Matthew WilsonDate: 2012-05-28 22:00:03
Subject: How do I use parameterized queries with LIKE?
Previous:From: Daniele VarrazzoDate: 2012-05-21 08:22:56
Subject: Re: Using a psycopg2 converter to retrieve bytea data from PostgreSQL

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group