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

Re: plpython3

From: James William Pye <lists(at)jwp(dot)name>
To: Greg Smith <greg(at)2ndquadrant(dot)com>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, jd(at)commandprompt(dot)com, Josh Berkus <josh(at)agliodbs(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpython3
Date: 2010-01-17 21:07:07
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-hackers
On Jan 14, 2010, at 7:08 PM, Greg Smith wrote:
> So more targeted examples like you're considering now would help.

Here's the first example. This covers an advantage of function modules.

This is a conversion of a plpythonu function published to the wiki:

In the above link, the code is executed in the body of a Python function.
Please see plpython's documentation if you don't understand what I mean by that.

The effect of this is that every time the FUNCTION is called from PG, the import statements are ran, a new class object, UrlOpener, is created, and a new function object, translate, is created. Granted, a minor amount of overhead in this case, but the point is that in order to avoid it the author would have to use SD:

if "urlopener" in SD:
 UrlOpener = SD["urlopener"]
 class UrlOpener(urllib.UrlOpener):
 SD["urlopener"] = UrlOpener

While some may consider this a minor inconvenience, the problem is that *setup code is common*, so it's, at least, a rather frequent, minor inconvenience.

With function modules, users have a module body to run any necessary setup code.

Now, WRT the actual example code, I'm not suggesting that either example is ideal. Only that it should *help* identify one particular advantage of function modules.

CREATE OR REPLACE FUNCTION public.gtranslate(src text, target text, phrase text)
 LANGUAGE plpython3u
AS $function$
from urllib.request import URLopener
from urllib.parse import quote_plus
import json

base_uri = ""

class UrlOpener(URLopener):
    version = "py-gtranslate/1.0"
urlopen = UrlOpener().open

equal_fmt = '{0}={1}'.format

def main(src, to, phrase):
    args = (
        ('v', '1.0'),
        ('langpair', quote_plus(src + '|' + to)),
        ('q', quote_plus(phrase)),
    argstring = '&'.join([equal_fmt(k,v) for (k,v) in args])

    resp = urlopen(base_uri + argstring).read()
    resp = json.loads(resp.decode('utf-8'))
        return resp['responseData']['translatedText']
        # should probably warn about failed translation
        return phrase

pl_regression=# SELECT gtranslate('en', 'es', 'i like coffee');
 Me gusta el café
(1 row)

pl_regression=# SELECT gtranslate('en', 'de', 'i like coffee');
 Ich mag Kaffee
(1 row)

In response to


pgsql-hackers by date

Next:From: Tom LaneDate: 2010-01-17 21:42:39
Subject: Re: Git out of sync vs. CVS
Previous:From: James William PyeDate: 2010-01-17 21:06:19
Subject: Re: plpython3

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