[DB-SIG] DB-API Spec. 1.1a1
Michael McLay
mclay@nist.gov
Tue, 9 Dec 1997 13:38:19 -0500
Hannu Krosing writes:
> M.-A. Lemburg wrote:
> can't we still have _one_ dbi module, that the db modules also use so that
> we can still do the 'is' comparison.this is needed for exceptions anyhow.
>
> Also, considering the new packaging scheme in 1.5, the db interface might
> also be restructured to use the new scheme.
> so that it will actually be db.interface or something
Here's an example dbi packaging hierarchy that could be used for
organizing databases in python. An example __init__.py is attached
for the top level. This module has a function that can be used by an
end users to selectively load a particular database interface directly
into the dbi namespace.
This approach has a problem which I think makes it a bad idea. Only
one database could be loaded into the dbi namespace in an application.
It would not be possible to safely connect to another database in the
same manner. This would only be a problem if more than one person
tried to use the dbi.use() function in an application. Multiple
databases could be opened, but they would have to use fully qualified
names to distinguish namespaces.
package dbi:
dbi
|---__init__.py
|
|---demos
| |---__init__.py
| |
| |---dbtest.py
|
|---sybase
| |---__init__.py
| |
| |---sybase.so ???
|
|---otherdb
|---__init__.py
###################### package dbi #########################
#contains one file: __init__.py
"""function 'use' will try loading a module named in parameter 'db'
Names from the module will be injected into the dbi namespace.
"""
import sys
class DB_ERROR:
"""manage problems with dbi here."""
def __init__(self, message):
self.message = message
def __repr__(self):
return self.message
def use(db):
if db == "gdbm":
print "loading gdbm"
import gdbm
sys.modules['dbi'].__dict__.update(gdbm.__dict__)
return "a"
elif db == "sybase":
try:
print "loading sybase"
import sysbase
sys.modules['dbi'].__dict__.update(sybase.__dict__)
return "sybase"
except:
raise DB_ERROR, "Sybase not installed properly"
###################### demo script dbtest.py ##################
""" demo script that show how to use name injection trick
Fir try to open sybase. If sybase isn't available the exception will
trigger loading gdbm instead.
"""
import dbi
try:
dbi.use("sybase")
except:
dbi.use("gdbm")
print "local namespace should have gdbm names in it:"
print " ", dir(dbi)
db = dbi.open("/tmp/foo","c")
db['y'] = 'r'
del db
ndb = dbi.open("/tmp/foo","c")
print ndb['x']
print ndb['y']
_______________
DB-SIG - SIG on Tabular Databases in Python
send messages to: db-sig@python.org
administrivia to: db-sig-request@python.org
_______________