I want to store Numpy arrays in a PostgreSQL database in binary form (bytea). I can make this work fine in test # 1 (see below), but I donβt want me to manipulate the data arrays before inserting and after each selection every time - I want to use psycopg2 adapters and converters.
Here is what I have at the moment:
import numpy as np import psycopg2, psycopg2.extras def my_adapter(spectrum): return psycopg2.Binary(spectrum) def my_converter(my_buffer, cursor): return np.frombuffer(my_buffer) class MyBinaryTest():
Now test # 1 works fine. When I take the code that I used in test 1 and set up the adapter and psycopg2 converter, it does not work (test 2). This is because the data supplied to the converter is no longer a buffer; this is a string representation of PosgreSQL bytea. The output is as follows:
In [1]: run -i test_binary.py Type of data retrieved: type 'buffer'> Everything went swimmingly in test one! ERROR: An unexpected error occurred while tokenizing input The following traceback may be corrupted or invalid The error message is: ('EOF in multi-line statement', (273, 0)) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) /Users/andycasey/thesis/scope/scope/test_binary.py in <module>() 155 test.perform_test_one() 156 test.setup_adapters_and_converters() --> 157 test.perform_test_two() 158 test.tear_down() 159 /Users/andycasey/thesis/scope/scope/test_binary.py in perform_test_two(self) 101
Does anyone know how I can (a) de-serialize the string representation returning to me in my_converter so that I return a Numpy array each time or (b) force the PostgreSQL / psycopg2 representation into the converter (which I can use) instead of the string representation ?
Thanks!
I'm on OS X 10.6.8 with Python 2.6.1 (r261: 67515), PostgreSQL 9.0.3 and psycopg2 2.4 (dt dec pq3 ext)
source share