Re: plpython3 - Mailing list pgsql-hackers
From | James William Pye |
---|---|
Subject | Re: plpython3 |
Date | |
Msg-id | 359A6FF5-E28B-419C-8B29-C5FD2A2C855A@jwp.name Whole thread Raw |
In response to | Re: plpython3 (Greg Smith <greg@2ndquadrant.com>) |
Responses |
Re: plpython3
|
List | 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: 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 thiscase, 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 ratherfrequent, 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 oneparticular advantage of function modules. CREATE OR REPLACE FUNCTION public.gtranslate(src text, target text, phrase text)RETURNS textLANGUAGE 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)
pgsql-hackers by date: