By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

python class inheritance

P: 6
Hi,

I'm on XP Pro using PythonWin, but I use a remote virtual unix machine on a server somewhere to run scripts. Using Python 2.5, MySQL 5.0

The problem is that I have a need to regularly query several MySQL databases on different machines to do my analysis. So I decided to try and organise myself a bit better and have some class structure with common code in appropriate levels of it.

I have set the top class to default to the database I use most often. But I wanted the derived classes to overwrite the host, port and db. Nothing I try seems to work. Obviously I am too old and need to be taken outside and shot. Alternatively somebody could point out the error of my ways I would be eternaly grateful. PS I can't spell either.

So I have a top class MySQLconnection as follows.

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. import MySQLdb
  3.  
  4. class MySQLconnection:
  5.     def __init__ (self, username, password):
  6.         self.username=username
  7.         self.password=password
  8.         self.host_details=abc.def.ghi.com'
  9.         self.port_number=1234
  10.         self.db_name='lots_of+things'
  11.         self.sslparms = { 'something': '/dev/null' }
  12.         self.db = MySQLdb.connect(
  13.             host=self.host_details, 
  14.             port=self.port_number, 
  15.             user=self.username, 
  16.             passwd=self.password, 
  17.             db=self.db_name)
  18.         self.cursor = self.db.cursor()
  19.  
  20.     def single_result(self):
  21.         row = self.cursor.fetchone()
  22.         if row is not None:
  23.             return row[0]
  24.  
  25.     def array_result(self):
  26.         rows = self.cursor.fetchall()
  27.         if rows is not None:
  28.             return [row[0] for row in rows]
  29.  
  30.     def result(self):
  31.         return self.cursor.fetchall()
  32.  
and a dervided class for each Database as follows

Expand|Select|Wrap|Line Numbers
  1. mport sys
  2. import MySQLdb
  3. import MySQLconnection
  4.  
  5. class StatsConnection(MySQLconnection.MySQLconnection):
  6.     self.host_details='some other database'
  7.     self.port_number=1236
  8.     self.db_name='other_things'
  9.     self.sslparms = { 'qwerty': '/dev/null' }
  10.  
  11. def some database specifc methods etc etc
  12.  
Then an example query becomes

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. import StatsConnection
  3.  
  4. username="TopSecret"
  5. password="ExtremelyTopSecret"
  6.  
  7. stats=StatsConnection.StatsConnection(username, password)
  8.  
  9. def select_thing():
  10.     stats.cursor.execute("""select e.thing from 
  11.         database.tbl_things as e limit 1""")
  12.     return stats.single_result()
  13.  
  14. x=select_rs()
  15. print x
  16.  
  17.  

obviously the self is wrong in the derived class. What I am trying to do is have the derived class overwrite the specifics for the database connection it represnts.
Jun 16 '09 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
There are several ways to do this. One would be to use class variables. Example:
Expand|Select|Wrap|Line Numbers
  1. class MySQLconnection(object):
  2.     host_details='abc.def.ghi.com'
  3.     port_number=1234
  4.     db_name='lots_of+things'
  5.     sslparms = { 'something': '/dev/null' }
  6.     def __init__ (self, username, password):
  7.         self.username=username
  8.         self.password=password
  9.         print getattr(self.__class__, 'host_details')
  10.         self.db = MySQLdb.connect(
  11.             host=getattr(self.__class__, 'host_details'), 
  12.             port=getattr(self.__class__, 'port_number'), 
  13.             user=self.username, 
  14.             passwd=self.password, 
  15.             db=getattr(self.__class__, 'db_name')
  16.         self.cursor = self.db.cursor()
  17.  
  18.     def single_result(self):
  19.         row = self.cursor.fetchone()
  20.         if row is not None:
  21.             return row[0]
  22.  
  23.     def array_result(self):
  24.         rows = self.cursor.fetchall()
  25.         if rows is not None:
  26.             return [row[0] for row in rows]
  27.  
  28.     def result(self):
  29.         return self.cursor.fetchall()
  30.  
  31. class StatsConnection(MySQLconnection):
  32.     host_details='some other database'
  33.     port_number=1236
  34.     db_name='other_things'
  35.     sslparms = { 'qwerty': '/dev/null' } 
  36.     def __init__ (self, username, password):
  37.         super(StatsConnection, self).__init__(username, password)
Jun 16 '09 #2

P: 6
Fantastic, Thanks. Just putting (object) in the top class did the trick - the super was not needed after that. Maybe that changed it to a "new style class"?
Jun 17 '09 #3

bvdet
Expert Mod 2.5K+
P: 2,851
You must initialize the instance by calling the base class __init__(). Also, I was using class variables. In your code, a StatsConnection() instance is not initialized, so self is not defined.

Using the base class object creates a new style class. Old-style classes are deprecated but still supported.
Jun 17 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.