473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Options, ConfigParser, modules, and SQLObject connections

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?
Jul 18 '05 #1
1 1607
Robin Munn wrote:
I'm sure others have faced similar problems before. How have you
managed?


What I do in a similar situation is to set an environment
variable. This environment variable points to
the .ini configuration file.

Then I put the configuration loading in the body of
the module. That way when it gets imported for the
first time it executes the instructions.

--- globalconf.py ---
# put whatever code you need here to
# fetch the environment variable and process the file
db_filename = 'database_name'

--- sqldefs.py ---
import globalconf
import sqlobject
__connection__ = sqlobject.SQLiteConnection(globalconf.db_filename)

Istvan.
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jacob.miles | last post by:
Hello. Does SQLObject provide connection pooling? If so, is it automatic or do I have to do something to manage it? If not, how do people generally solve this problem? Thanks for any help. ...
0
by: Oleg Broytmann | last post by:
Hello! I'm pleased to announce the 0.8.0b1 release of SQLObject. This is the first beta of the new branch. Taking into account that it is a result of rather large job the beta period will be...
0
by: Oleg Broytmann | last post by:
Hello! I'm pleased to announce the 0.7.3b1 release of SQLObject. What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as...
0
by: Oleg Broytmann | last post by:
Hello! I'm pleased to announce the 0.7.3 release of SQLObject. What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as classes,...
0
by: Oleg Broytmann | last post by:
Hello! I'm pleased to announce the 0.8.0 release of SQLObject. What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as...
2
by: Oleg Broytmann | last post by:
Hello! I'm pleased to announce the 0.8.1 release of SQLObject. What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as...
0
by: Oleg Broytmann | last post by:
Hello! I'm pleased to announce the 0.9.0b1 release of SQLObject, the first beta of the upcoming 0.9 release. What is SQLObject ================= SQLObject is an object-relational mapper....
3
by: Oleg Broytmann | last post by:
Hello! I'm pleased to announce the 0.9.0 release of SQLObject, the first stable release of the 0.9 branch. What is SQLObject ================= SQLObject is an object-relational mapper. ...
0
by: Guillermo Heizenreder | last post by:
I'm creating one aplicattion and I use SQLObject, but I have a little problem, when I try to create one table my aplicattion crash! :( Let me show you: nercof@debian:~/Proyectos/ghhp/lib$ python...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.