473,561 Members | 3,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SQLit eConnection('us erdata.db')

class Person(sqlobjec t.SQLObject):
firstName = sqlobject.Strin gCol()
# 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.Sa feConfigParser( )
config.read('~/.myapp/myapp.cfg')
database_filena me = config.get("dat abase", "filename")
# etc.

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

And now the problem arises: how do I get that value into the
sqldefs.__conne ction__ 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.Sa feConfigParser( )
def load_config(fil ename):
config.read(fil ename)
---- END globalconfig.py ----

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

--- BEGIN sqldefs.py ---
import globalconfig
import sqlobject
db_filename = globalconfig.co nfig.get("datab ase", "filename")
__connection__ = sqlobject.SQLit eConnection(db_ filename)

class Person(sqlobjec t.SQLObject):
firstName = sqlobject.Strin gCol()
# 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.No SectionError: "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 1622
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.SQLit eConnection(glo balconf.db_file name)

Istvan.
Jul 18 '05 #2

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

Similar topics

2
1562
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. - Jake
0
1162
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 prolonged. Meanwhile the stable 0.7 branch will be maintained, and there will be at least 0.7.3 release. What is SQLObject =================
0
802
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 classes, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with.
0
781
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, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with.
0
1248
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 classes, and rows are instances of those classes. SQLObject is meant to be
2
1396
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 classes, and rows are instances of those classes. SQLObject is meant to be
0
966
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. Your database tables are described
3
1758
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. Your database tables are described
0
1171
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 Python 2.4.4 (#2, Apr 5 2007, 20:11:18) on linux2 Type "help", "copyright", "credits" or "license" for more information. .... _connection =...
0
7858
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8085
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7614
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6206
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3598
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1180
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
892
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.