Options, ConfigParser, modules, and SQLObject connections

Robin Munn rmunn at pobox.com
Tue Oct 12 10:24:16 EDT 2004


I've been loving SQLObject. The ability to set up a database
connection and then completely *forget* about it and just manipulate
Python objects has been great. But I'm running into a problem, and I'd
like some advice.

I'm writing an app using SQLite as the database backend, and
interfacing to it via SQLObject. I have all my SQL table definitions
(classes inheriting from SQLObject) in one module, and for
simplicity's sake I'm using a module-level __connection__ variable
that all my classes will pick up. But the SQL manual warns, "The
__connection__ magic variable can be a little fragile -- it has to be
defined before the class is defined. This means it must be assigned
above the class ...: line." No problem, my module looks like this:

--- BEGIN sqldefs.py ---

import sqlobject

__connection__ = sqlobject.SQLiteConnection('userdata.db')

class Person(sqlobject.SQLObject):
    firstName = sqlobject.StringCol()
    # etc.

---- END sqldefs.py ----

But I don't want to hardcode the database filename. I want to let the
user choose the filename, and I'll be saving their choice in a
configuration file readable by ConfigParser. Thus:

--- BEGIN main.py ---

import ConfigParser

config = ConfigParser.SafeConfigParser()
config.read('~/.myapp/myapp.cfg')
database_filename = config.get("database", "filename")
# etc.

---- END main.py ----

And now the problem arises: how do I get that value into the
sqldefs.__connection__ object? Once I import sqldefs, it's too late:
__connection__ will get set at import time, and the classes will be
created at import time.

I've thought of a couple solutions, but neither one seems all that
clean to me.

1) Make sqldefs.py my main module, the one that imports all the
others. Ew.

2) Keep the "config" object in its own module, and make sure that the
object has been initialized before importing sqldefs:

--- BEGIN globalconfig.py ---
import ConfigParser
config = ConfigParser.SafeConfigParser()
def load_config(filename):
    config.read(filename)
---- END globalconfig.py ----

--- BEGIN main.py ---
import globalconfig
globalconfig.load_config('~/.myapp/myapp.cfg')
import sqldefs
# etc.
---- END main.py ----

--- BEGIN sqldefs.py ---
import globalconfig
import sqlobject
db_filename = globalconfig.config.get("database", "filename")
__connection__ = sqlobject.SQLiteConnection(db_filename)

class Person(sqlobject.SQLObject):
    firstName = sqlobject.StringCol()
    # etc.
---- END sqldefs.py ----

This works, but it feels a little fragile to me. Suddenly the order in
which I import my modules matters a lot. If I ever forget and import
sqldefs before I've called load_config(), I'll get a
ConfigParser.NoSectionError: "No section: 'database'", which may not
be the clearest way to remind me "Hey, you imported your modules in
the wrong order."

I'm sure others have faced similar problems before. How have you
managed?



More information about the Python-list mailing list