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:

 http://wiki.postgresql.org/wiki/Google_Translate

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"]
else:
 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)
 RETURNS text
 LANGUAGE plpython3u
AS $function$
from urllib.request import URLopener
from urllib.parse import quote_plus
import json

base_uri = "http://ajax.googleapis.com/ajax/services/language/translate?";

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

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

@pytypes
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'))
    try:
        return resp['responseData']['translatedText']
    except:
        # should probably warn about failed translation
        return phrase
$function$;


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

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


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to