conditional importing

Scott Hathaway slhath at charter.net
Sun Jul 28 21:29:48 EDT 2002


How can I do a conditional import?  I am creating a database wrapper class
that is similar to adodb in PHP.
I have the following class:

from mx.ODBC import Windows as db1
import MySQLdb as db2

class adodb:
    # global vars
    global db1,db2

    # class level vars
    dbType      = 'odbc'
    db          = db1

    def __init__(self,dbType='odbc'):
        self.dbType(dbType)

    def dbType(self,dbType):
        global db1,db2
        self.dbType = dbType
        if(dbType=='odbc'):
            self.db = db1
        if(dbType=='mysql'):
            self.db = db2
        return self.db

Is this the best way to do a conditional import?  Also, if there is an error
in loading one of the two
modules at the top, what is the best way to catch that?  I could put try ...
excepts around each one,
but is there a better way?

By the way, when I am finished with this class, I will post a link to the
group so that if anyone else
wants to use it, they can.

It allows you to access the fields in the database by name instead of
position of the field.  It also has
a function that allows you to pass a dictionary with key-values pairs that
match your database and
it automatically decides whether to do an insert or an update and then
returns the appropriate SQL
statement.

Thanks,
Scott





More information about the Python-list mailing list