[Tutor] code design problem

Marco Scodeggio Marco Scodeggio <marcos@ifctr.mi.cnr.it>
Tue, 24 Jul 2001 10:41:08 +0200 (MET DST)


Dear most helpful friends,
I'm having a problem in the conceptual design of a piece of software,
that certainly originates from a mix of my far from perfect Python knowledge
and my total ignorance about object oriented programming.....

What I'm trying to achieve is the following: a tool to manipulate and
visualize data, that can get the data straight out of a database. 
The two requirements I need to satisfy are the following: 
1) it should be possible (and easy) to use it with the database engine 
of choice;
2) it should be possible (and easy) to plug in new data handling
modules to make the tool more powerful.

Therefore I am trying to put the database connection code in a class
by itself, that will take care of loading the module(s) implementing
the database interface, open the connection, and always return a database
"cursor", independently from the specific db engine.

Then all the code needed to carry out database queries is in a
separate class, since the queries only need the cursor to operate, and
therefore this code can be completely db engine neutral. This class
(in my naive model) should be initialized once and forever passing to
it the db cursor "returned" by the connection class. Thus it would 
effectively represent the interface that the plug-in modules would use 
to communicate with the db.

In this way if I use mySQL and user Joe uses Oracle, Joe needs only to
change the connection class (or the class could already contain a
number of connection alternatives, that's not important), and nothing
else. And if he wants to add some functionality, he just needs to know
what functions the query class offers to grab the required data.

Thus the pseudo-code for this part of the tool would look something
like this:

import dbConncection
import dbQuery

class myTool:
   __init__(whatever):
       myConnection = dbConnection('mysql')
       myCursor = myConnection.doConnect()
       myQuery = dbQuery(myCursor)
       myResult = myQuery.doQuery( < list of parameters > )

and this works perfectly OK. 

Now here comes my big problem: 
I want to plug in a new module, containing class myPlugin, and 
I would like to be able to do something like this:

import dbQuery

class myPlugin:
   __init__(whatever):
       pluginResult = dbQuery.doQuery( < list of parameters > )

but of course if I do that I get the error 
"TypeError: unbound method must be called with class instance 1st argument"

Therefore the question is: what should I do to get an equivalent
result ?? I do not want to create a new instance of the
dbQuery class (I presume) within myPlugin, because this new instance 
would not know what "myCursor" is, and I do not want to create one db 
connection for each plug-in module, as one such connection is
certainly enough for all of them.

So, if you were able to follow me this far, I would like to ask you
guys for any suggestions that might help me achieve the objectives I
have tried to describe.

Thanks a lot in advance,

	 Marco Scodeggio