FUNCTION CACHINGIMMUTABLE STABLE VOLATILE SECURITY CONTEXTSECURITY DEFINER CONTROL FLOWif foo =='blah': do_something() for x in somearray: statements [x['somefield'] for x in rv] [1.1*x/(2 + x) for x in range(5)] if some boolean expression: stuff happens while b < n: stuff happens RETURN constructsreturn somevariable return somearray_variable Common Constructsimport somepackage #this is a comment a, b = b, a+b alist =['Jim', 'Guru', 'x3456'] name, title, phone = alist alist = [] #declares empty array adict = {} #declare empty dictionary adict = {'Jim': 'Guru', 'Jimmy' : 'Intern'} Constants and functionsTrue False coerce(somestring, ther) dict(..) globals() float(num_string) hasattr(object, name) hash(obj) int(num_string) len(somearray_ordict) long(num_string) map(function, sequence[, sequence, ...]) pow(x, y [, z]) range([start,] end [, step]) xrange(start [, end [, step]]) (#use for big lists) round(x,[numdigits]) slice([start,] stop[, step]) split(pattern,string, [maxsplit]) str(object) zip(seq1[, seq2,...]) somestring.split |
Operators+ - * / // * ** # power operator & ^ | == #boolean operators &= ^= |= #compound bool << >> #shift operators <<= >>= #compound shift operators EXCEPTION Handlingtry: stuff happens return something except (IOError, OSError): return "an error has happened" try: stuff happens return something except IOError: return "an IOError" except RuntimeError: return "runtime error" except: return "have no clue what happened" Common Error StatesException StandardError ArithmeticError FloatingPointError OverflowError ZeroDivisionError EnvironmentError IOError OSError EOFError ImportError RuntimeError SystemError Built-in Objectsplpy execute(sql) #returns a python dictionary object prepare(sql) # returns a prepared plan object TD["new"] # trigger new data TD["old"] # trigger old data TD["when"] # BEFORE, AFTER, UNKNOWN SD Common Packages and Package Functionsos -- chdir(path), chmod(path), listdir(path) mkdir(path,[mode]), rmdir(path), unlink(path), write(fp, str), path.exists() math -- pi sys -- argv, modules,path,stdin,stdout, stderr, version, exit(n) time -- time(), clock(), strftime(format, timetuple), sleep(secs) COSTINGCOST cost metric ROWS estimated number of rows |
||
Official PostgreSQL 8.3 PL/Python Documentation URL: http://www.postgresql.org/docs/8.3/interactive/plpython.html Official Python documentation: http://docs.python.org/ We cover only a subset of what we feel are the most useful constructs that we could squash in a single cheatsheet page commonly used 1 New in this release. PLPython FUNCTION SAMPLES
|
|||
http://www.postgresonline.com |