writing sql functions in python