import MySQLdb as mdb
import sys


class Mysql():
    def __init__(self):
        self.con = mdb.connect('127.0.0.1', 'shippable', '', 'test')

    def populate(self):
        try:
            con = self.con
            cur = con.cursor()
            cur.execute("DROP TABLE IF EXISTS messages;")
            cur.execute("CREATE TABLE messages(message varchar(200));")
            cur.execute("insert into messages(message) VALUES('this is test message 1');")
            cur.execute("INSERT INTO messages(message) VALUES('this is test message 2');")
            cur.execute("INSERT INTO messages(message) VALUES('this is test message 3');")
            cur.execute("INSERT INTO messages(message) VALUES('this is test message 4');")
            cur.execute("INSERT INTO messages(message) VALUES('this is test message 5');")
            con.commit()
        except mdb.Error, e:
            print "Error %d: %s" % (e.args[0],e.args[1])
            sys.exit(1)

        finally:
            self.disconnect()

    def call_test_proc(self):
        try:
            con = self.reconnect()
            cur = con.cursor()
            return cur.callproc('test_proc')
        finally:
            self.disconnect()


    def rowcount(self):
        try:
            con = self.reconnect()
            cur = con.cursor()
            cur.execute("select * from messages;")
            cnt = cur.fetchall()
            return len(cnt)
        except mdb.Error, e:
            print "Error %d: %s" % (e.args[0],e.args[1])
            sys.exit(1)

        finally:
            self.disconnect()

    def reconnect(self):
        self.con = mdb.connect('127.0.0.1', 'shippable', '', 'test')
        return self.con

    def disconnect(self):
        if self.con:
            self.con.close()


if __name__ == '__main__':
    mysql = Mysql()
    mysql.populate()
    mysql.rowcount()

@@ -55,16 +55,16 @@
 def 
-__init__
+populate
 (sel
@@ -79,93 +79,18 @@
     
-self.
 con = 
-mdb.connect('127.0.0.1', 'shippable', '', 'test')%0A%0A    def populate(self):
+None
 %0A   
@@ -125,16 +125,22 @@
 self.con
+nect()
 %0A       
@@ -909,39 +909,33 @@
             
-self.disconnect
+con.close
 ()%0A%0A    def 
@@ -948,32 +948,51 @@
 est_proc(self):%0A
+        con = None%0A
         try:%0A   
@@ -1132,28 +1132,21 @@
     
-self.disconnect
+con.close
 ()%0A%0A
-%0A
     
@@ -1157,32 +1157,51 @@
 rowcount(self):%0A
+        con = None%0A
         try:%0A   
@@ -1212,34 +1212,32 @@
      con = self.
-re
 connect()%0A      
@@ -1517,23 +1517,17 @@
     
-self.disconnect
+con.close
 ()%0A%0A
@@ -1534,18 +1534,16 @@
     def 
-re
 connect(
@@ -1557,21 +1557,16 @@
         
-self.
 con = md
@@ -1628,21 +1628,16 @@
  return 
-self.
 con%0A%0A   

Refactor connections