[DB-SIG] dbiDate - help urgently needed
Lloyd Kvam
pythontutor at venix.com
Mon Jun 2 23:08:31 EDT 2003
Presumably you only need to determine the functions for each column once. However,
that would risk passing None as a row value (from NULLs) into the functions. It also
requires using a row with no NULLs. Alternatively, you can use the information that
comes back in the cursor.description to determine column types. (I have not used the
Version 1 API, so I can't guarantee that description has the type info.)
http://www.python.org/topics/database/DatabaseAPI-2.0.html#cursor
Lists the values for each column returned in cursor.description.
<pseudo code typed directly into the email - AND BASED ON API 2>
def choose_clean(dbtype):
if dbtype in (??? some list based on what your DB gives you ???):
return clean_str
elif dbtype in (?? date, timestamp type things ??):
return clean_date
elif ???? and so on ????
clean_funcs = [choose_clean(dbtype) for dbname,dbtype,x1,x2,x3,x4,x5 in cursor.description]
for row_item in row:
clean_items = map(apply, clean_funcs, zip(row_item))
??? do what you do with clean_items ???
< end funny pseudo code >
This may not be much of an improvement depending on just what you are trying to
accomplish. It avoids making the same decision over and over again for each
column. However the map(apply... is rather strange looking. It also requires that
the functions can deal with a None.
I've only used the dbAPI-2. Your references to dbiDate (so far as I know) suggest
you are using dbAPI-1. Using the builtin type function is reasonable. Look at
the types module. You might really want StringTypes rather than StringType.
Jeff Sacksteder wrote:
> This is working but seems suboptimal. Is this the most correct way to go
> about it?
>
> for row_item in row:
> if type(row_item) is StringType:
> clean_item = clean_str(row_item)
> elif type(row_item) == type(dbi.dbiDate(0)):
> clean_item = clean_date(row_item)
> else: clean_item =
> clean_misc(row_item)
>
>
>
>
> _______________________________________________
> DB-SIG maillist - DB-SIG at python.org
> http://mail.python.org/mailman/listinfo/db-sig
>
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice: 603-443-6155
fax: 801-459-9582
More information about the DB-SIG
mailing list