Grid advice solicited...

Hi,
I am building a Postgresql Browser with wxPython.

The user will enter some sql in a panel and cause
the sql to be sent to the back end for execution...

     def ExecString(self,cmd):
         wx.SetCursor(wx.StockCursor(wx.CURSOR_WATCH))
         self.Status.SetValue("Working...\n")
         wx.SafeYield()
         startTime = time.time()
         try :
            self.cur.execute(cmd)

         except psycopg.DatabaseError, errorInfo:
            self.Status.AppendText("Error: "+ str(errorInfo) + '\n')
            wx.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
            return

         if( self.cur.description == None ):
             self.Status.AppendText("Ok. " + self.cur.statusmessage+'\n')
             text="%d row(s) affected\n" % self.cur.rowcount
             self.Status.AppendText( text + '\n')
             wx.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
             return

         results = self.cur.fetchall()
         self.dbTable=DbTable(results,self.grid,self.cur.description,self.colResizeFlag) <----------
         #self.grid.ForceRefresh()
         wx.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
         wx.SafeYield()
         finishTime = int( 1000*(time.time()-startTime))
         self.Status.AppendText("Ok. "+self.cur.statusmessage+'\n')
         text="%d rows(s), %d column(s)\n" % (self.cur.rowcount, len(self.cur.description))
         self.Status.AppendText(text)
         self.Status.AppendText("Done in %d milliseconds\n" % finishTime)

DbTable looks like..
class DbTable(wx.grid.PyGridTableBase):
     def __init__(self, theResult,theGrid,colInfo,colresize):

         wx.grid.PyGridTableBase.__init__(self)

         self.data = theResult
         self.rowCnt = len(theResult)
         self.colCnt = len(colInfo)
         self.grid = theGrid
         self.colInfo = colInfo
         self.colResizeFlag = colresize

         self.odd=wx.grid.GridCellAttr()
         self.odd.SetBackgroundColour("light blue")
         self.odd.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))

         self.even=wx.grid.GridCellAttr()
         self.even.SetBackgroundColour("white")
         self.even.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))

         # we need to do this before the call to autoresizecols
         self.grid.SetTable(self,False)

         #wx.MessageBox( "in dbtable flag = %s" % self.colResizeFlag)
         if self.colResizeFlag :
            self.grid.AutoSizeColumns(self.colResizeFlag)
         #wx.CallAfter(self.WorkaroundScrollBug, self.grid)

     # these five are the required methods
     def GetNumberRows(self):
         return self.rowCnt
     more standard methods

Question 1: Is it "Ok" to create a new DbTable for each query rather than directly
             modifying the critical variables for a single instance of DbTable and calling SetTable
      on the grid each time a sql statement is executed? It appears to work ok but
      I think I have a memory leak somewhere ( top shows memory requirements slowing growing).
             The code does display the results properly.

Question 2: When does AutoSizeColumns actually do what it is supposed to do? I rewrote the
             ExecuteString and DbTable routine to follow the single DbTable strategy as above
             but the column resizing was unpredictable...Is there a private instance variable that
      Grid uses to see if the columns need to be adjusted...

By the way having AutoSizeColumns on can easily increase the overall execution time by a factor
of 10 ( 5500 rows, 7 columns) , so I really like the idea of being able to turn autosizing on/off

Thanks

Jerry

Jerry LeVan wrote:

Question 1: Is it "Ok" to create a new DbTable for each query rather than directly
            modifying the critical variables for a single instance of DbTable and calling SetTable
        on the grid each time a sql statement is executed? It appears to work ok but
        I think I have a memory leak somewhere ( top shows memory requirements slowing growing).
            The code does display the results properly.

Try calling the Destroy method of the old table after setting the new one into the grid.

Question 2: When does AutoSizeColumns actually do what it is supposed to do? I rewrote the
            ExecuteString and DbTable routine to follow the single DbTable strategy as above
            but the column resizing was unpredictable...Is there a private instance variable that
        Grid uses to see if the columns need to be adjusted...

AutoSizeColumns doesn't turn anything on or off, it simply scans the grid and does whatever sizing is needed at the time you call AutoSizeColumns.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!