Re: [SQLObject] Aggregate columns / change connection
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Andy T. <an...@ha...> - 2003-08-03 10:22:55
|
Achim Domma (ProCoders) wrote: > Hi, > > I've seen Brad Bollenbach very good talk at Europython, and now I finaly > found the time to give SQLObject a try. I really like it, but have some > questions: > > - In the examples, the connection is always hardcoded in the module, where > my classes are definied. In real world apps I would like to specifiy the > connection from outside of the module. I might have two different instances > of my app, using two different servers/databases. How would I do that? > > - What's about aggregate columns? I want to build a system with some kind of > ranking mechanism, where I usually only want to show the average ranking. As > far as I understand, I would have to calculate that value 'by hand', if I > use SQLObject. Is that right? > > regards, > Achim > I'll leave others to answer your question on aggregate columns, but for connections I do the same thing that I would for 'raw' db-api code. I store the user name, password, host name and database name in a configuration file and then read that in at the top of my module. The values are placed in a dictionary (actually, they are stored in dict format but that's just because I'm lazy) and then used as arguments to the connection attribute. e.g.; """ import os.path CONFIG_FILENAME='myconfig.txt' if '__file__' in locals(): CONFIG=os.path.dirname(__file__)+'/'+CONFIG_FILENAME else: CONFIG=CONFIG_FILENAME config=readAndEvalFile(CONFIG) __connection__=MySQLConnection(db=config['db'], host=config['host'], user=config['user'], passwd=config['passwd']) """ The only magic there is the readAndEvalFile function which simply reads the file you supply and turns it into a dictionary. Of course, you don't need to store the values in a configuration file. If you are building a web or gui front end to your application you can prompt for the values and then pass them in to your module at run time. Regards, Andy -- -------------------------------------------------------------------------------- From the desk of Andrew J Todd esq - http://www.halfcooked.com/ |