Re: plpythonu array parameter

Поиск
Список
Период
Сортировка
От _
Тема Re: plpythonu array parameter
Дата
Msg-id 1169525063.248460.85790@v45g2000cwv.googlegroups.com
обсуждение исходный текст
Ответ на plpythonu array parameter  (Sim Zacks <sim@compulab.co.il>)
Список pgsql-general
Anyone interested in arrays and plpython might find this interesting.
Based on the conversation below I put these functions in a library
(pg_stuff.py):

def arr2list(a):
    a = a.replace("{","[").replace("}","]")
    pylist = eval(a)
    return pylist

def list2arr(a):
    parm = `a`
    parm=parm.replace("[","{").replace("]","}")
    return parm

Then I was able to call them like this:

CREATE  TYPE int_triple AS(
    x int, y int, z int);
CREATE FUNCTION py_explode_4 (a int[], b int[])
  returns setof int_triple
AS $$
import sys
sys.path.append('/Users/wsprague/lib')
import pg_stuff
x_list = pg_stuff.arr2list(a)
y_list = pg_stuff.arr2list(b)
for x in x_list:
    for y in y_list:
        yield(x, y, x+y)
$$ LANGUAGE plpythonu;

Yielding the following in postgres:

or_gis=# select * from  py_explode_4(array[1,2], array[10,20]);
 x | y  | z
---+----+----
 1 | 10 | 11
 1 | 20 | 21
 2 | 10 | 12
 2 | 20 | 22
(4 rows)

Not exactly earth shattering, but still cool.  My next hack will be to
convert an array to a matrix and find the eigenvalues and convert
back...  It does seem like array to list conversion should happen
automatically, though.

Sim Zacks wrote:
> I ran into an interesting issue trying to pass an array to a plpythonu function (Postgresql 8.03).
> When I googled the issue I found a number of people asking similar questions and they haven't found
> an answer. The problem is that there is no type mapped from a postgresql array to a python list.
>
> These conversion functions will map between a postgresql array and a python list and back. Is there
> a way to stick this in the integration code somewhere, so that every time an array is passed to/from
> a python function it converts it transparently?
>
> In the python function itself this type of code will work, what I don't know is how to change the
> internals so that when an array variable is passed in it automatically puts it through this code and
> hands off a python list variable.
>
> The second function would work as is, and return a postgresql array.
>
> Of course both would have to work with any type of array and not just text[]
>
> create or replace function pgarray_to_pylist(text[]) returns {python list} as
> $$
> parm=args[0]
> parm=parm.replace("{","[").replace("}","]")
> pylist=eval(parm)
> return pylist
> $$ language 'plpythonu'
>
>
> create or replace function pylist_to_pgarray({python list}) return text[] as
> $$
> parm=`args[0]`
> parm=parm.replace("[","{").replace("]","}")
> return parm
> $$ language 'plpythonu'


В списке pgsql-general по дате отправления:

Предыдущее
От: Subramaniam Aiylam
Дата:
Сообщение: Postgres processes have a burst of CPU usage
Следующее
От: erick.papa@gmail.com
Дата:
Сообщение: Installing PostgreSQL under Cpanel