Postgresql Plpython Cheatsheet
Postgresql Plpython Cheatsheet
IMMUTABLE + - *
STABLE / // *
VOLATILE ** # power operator
& ^ | == #boolean operators
SECURITY CONTEXT &= ^= |= #compound bool
<< >> #shift operators
SECURITY DEFINER
<<= >>= #compound shift operators
CONTROL FLOW EXCEPTION Handling
if foo =='blah': try:
do_something()
stuff happens
for x in somearray:
return something
statements
except (IOError, OSError):
[x['somefield'] for x in rv]
return "an error has happened"
[1.1*x/(2 + x) for x in range(5)]
try:
if some boolean expression:
stuff happens
stuff happens
return something
except IOError:
while b < n:
return "an IOError"
stuff happens except RuntimeError:
return "runtime error"
RETURN constructs
except:
return somevariable return "have no clue what happened"
return somearray_variable
Common Error States
Common Constructs Exception
import somepackage StandardError
#this is a comment ArithmeticError
a, b = b, a+b FloatingPointError
alist =['Jim', 'Guru', 'x3456'] OverflowError
name, title, phone = alist ZeroDivisionError
alist = [] #declares empty array EnvironmentError
adict = {} #declare empty dictionary IOError
adict = {'Jim': 'Guru', 'Jimmy' : 'Intern'} OSError
EOFError
Constants and functions ImportError
RuntimeError
True SystemError
False
coerce(somestring, ther) Built-in Objects
dict(..)
globals() plpy
float(num_string) execute(sql) #returns a python dictionary object
hasattr(object, name) prepare(sql) # returns a prepared plan object
hash(obj) TD["new"] # trigger new data
int(num_string) TD["old"] # trigger old data
len(somearray_ordict) TD["when"] # BEFORE, AFTER, UNKNOWN
long(num_string) SD
map(function, sequence[, sequence, ...])
Common Packages and Package Functions
pow(x, y [, z])
range([start,] end [, step]) os -- chdir(path), chmod(path), listdir(path)
xrange(start [, end [, step]]) (#use for big lists) mkdir(path,[mode]), rmdir(path), unlink(path),
round(x,[numdigits]) write(fp, str), path.exists()
slice([start,] stop[, step]) math -- pi
split(pattern,string, [maxsplit]) sys -- argv, modules,path,stdin,stdout,
str(object) stderr, version, exit(n)
zip(seq1[, seq2,...]) time -- time(), clock(), strftime(format, timetuple), sleep(secs)
somestring.split
COSTING
http://www.postgresonline.com