i have functions that look like this that is littered through out the code
def get_M_status(S):
M_id = marital.select(marital.c.marital_status_description == S).execute().fetchone()
if M_id == None:
print "Warning: No Marital id found for %s Marital status to Single" % S
M_id = marital.select(marital.c.marital_status_description == "Single").execute().fetchone()
return M_id[0]
i was wondering if their is a way to write a generic function where i can pass the relevant values ie: table name primary key column filter column and filter value
cheers
If the primary key is only one column, you can do something like:
as the ‘generic’ version of
marital.c.marital_status_description == S.So, something like (please note: this is untested):
If you have mapped classes, this is even easier; you can do things like:
where
Maritalis the mapped class for the tablemarital,sessionis a sql alchemy session,keyis a tuple of key columns (in order). If the key exists in the table,recordwill be the row found; otherwise it will beNone.