trying to modify simple wx.grid code

I am having two problems with this code (I haven't got the repy down so this is a new message) one is that I get an error when I try to add new arguments to def loadFile(self): The other is that when I added a file in '' and changed lines to line in the routine it stopped doing anything (I don't get an error though it seems to make the interpiter happy)

!/usr/local/python
"""

import wx
import wx.grid as gridlib
import os
import string
fileContents = """\
Column headers go here
This is a test of the
emergency broadcast system.
Had this been an actual
emergency, you would have been
told to sit down, put your
head between your legs,
and kiss your ass goodbye.
"""

···

#---------------------------------------------------------------------------

class WordGrid(gridlib.Grid):
    def __init__(self, parent, log):
        gridlib.Grid.__init__(self, parent, -1)

        self.loadFile()

        self.CreateGrid(len(self.rows), self.widestRow)

        for r, row in enumerate(self.rows):
            for c, col in enumerate(row):
                self.SetCellValue(r, c, col)
            self.SetColSize(c, 10*self.widestCol)

        for c, label in enumerate(self.header):
            self.SetColLabelValue(c, label)

    def loadFile(self):
        # you'd probably want to read a file here instead of a text string
        #lines = fileContents.split('\n')
        infile = open('test.sco', 'r')
        testline = 'false'
        for line in infile:
            if ";<sco_header>" in line:
                self.header = line.split()
                testline = 'true'
            if testline == 'true':
                self.rows = [i.split() for i in line[1:]]
                self.widestRow = max([len(r) for r in self.rows])
                self.widestCol = max([len(c) for c in [r for r in self.rows]])

#---------------------------------------------------------------------------

class TestFrame(wx.Frame):
    def __init__(self, parent, log):
        wx.Frame.__init__(self, parent, -1, "sco file editor",
size=(640,480))
        grid = WordGrid(self, log)

#---------------------------------------------------------------------------

******************** this is derived from ***************************

import wx
import wx.grid as gridlib

fileContents = """\
Column headers go here
This is a test of the
emergency broadcast system.
Had this been an actual
emergency, you would have been
told to sit down, put your
head between your legs,
and kiss your ass goodbye.
"""

#---------------------------------------------------------------------------

class WordGrid(gridlib.Grid):
    def __init__(self, parent, log):
        gridlib.Grid.__init__(self, parent, -1)

        self.loadFile()

        self.CreateGrid(len(self.rows), self.widestRow)

        for r, row in enumerate(self.rows):
            for c, col in enumerate(row):
                self.SetCellValue(r, c, col)
            self.SetColSize(c, 10*self.widestCol)

        for c, label in enumerate(self.header):
            self.SetColLabelValue(c, label)

    def loadFile(self):
        # you'd probably want to read a file here instead of a text string
        lines = fileContents.split('\n')
        self.header = lines[0].split()
        self.rows = [i.split() for i in lines[1:]]
        self.widestRow = max([len(r) for r in self.rows])
        self.widestCol = max([len(c) for c in [r for r in self.rows]])

#---------------------------------------------------------------------------

class TestFrame(wx.Frame):
    def __init__(self, parent, log):
        wx.Frame.__init__(self, parent, -1, "Simple Grid Demo",
size=(640,480))
        grid = WordGrid(self, log)

#---------------------------------------------------------------------------

if __name__ == '__main__':
    import sys
    app = wx.PySimpleApp()
    frame = TestFrame(None, sys.stdout)
    frame.Show(True)
    app.MainLoop()

--
------------------------------------------------------------
Kent Quirk I'm making a game about global warming.
Game Architect Track the progress at:
CogniToy http://www.cognitoy.com/meltingpoint

https://sourceforge.net/projects/dex-tracker

question what is the effect of blacktop vs whitetop on global warming ??? :slight_smile:

_________________________________________________________________
Add a Yahoo! contact to Windows Live Messenger for a chance to win a free trip! http://www.imagine-windowslive.com/minisites/yahoo/default.aspx?locale=en-us&hmtagline

Eric Dexter wrote:

I am having two problems with this code (I haven't got the repy down so this is a new message) one is that I get an error when I try to add new arguments to def loadFile(self):

What error? Did you also change where you called the function to pass new parameters?

The other is that when I added a file in '' and changed lines to line in the routine it stopped doing anything

Are you sure that your file has ";<sco_header>" in it? And that there are lines after that line in the file?

···

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

Eric:

You really need to try to understand what you're trying to do.

You have this:

   def loadFile(self):
       # you'd probably want to read a file here instead of a text string
       #lines = fileContents.split('\n')
       infile = open('test.sco', 'r')
       testline = 'false'
       for line in infile:
           if ";<sco_header>" in line:
               self.header = line.split()
               testline = 'true'
           if testline == 'true':
               self.rows = [i.split() for i in line[1:]]
               self.widestRow = max([len(r) for r in self.rows])
               self.widestCol = max([len(c) for c in [r for r in self.rows]])

Several points:

* Python has boolean values called True and False; you should use those instead of the strings.
* You should probably rename the variable "testline" to something like "foundHeader"
* The self.rows variable is intended to be a list of all of the rows, where each element in the list is a list of columns in that row. You will need to append to that list in the loop.
* The [:1] is a slicing operator to start after the first row...but it has no application in your new loop, because you're already working on a line
* The list comprehension in self.rows was intended to walk the full set of rows; you already have an iteration here, so you don't need another one.
* The calculations for widestRow and widestCol belong outside the loop. (I think you need to read up on list comprehensions so you can understand the code you're reading).
I'd rewrite that routine to look something like this (not tested!):

   def loadFile(self):
       infile = open('test.sco', 'r')
       foundHeader = False
       self.rows =
       for line in infile:
           if ";<sco_header>" in line:
               self.header = line.split()
               foundHeader = 'true'
               continue # we don't want to process this line any further
           if testline:
               self.rows.append(line.split())

       self.widestRow = max([len(r) for r in self.rows])
       self.widestCol = max([len(c) for c in [r for r in self.rows]])

Please, when someone gives you code, take some time to understand it.

Eric Dexter wrote:

···

I am having two problems with this code (I haven't got the repy down so this is a new message) one is that I get an error when I try to add new arguments to def loadFile(self): The other is that when I added a file in '' and changed lines to line in the routine it stopped doing anything (I don't get an error though it seems to make the interpiter happy)

!/usr/local/python
"""

import wx
import wx.grid as gridlib
import os
import string

#---------------------------------------------------------------------------

class WordGrid(gridlib.Grid):
   def __init__(self, parent, log):
       gridlib.Grid.__init__(self, parent, -1)

       self.loadFile()

       self.CreateGrid(len(self.rows), self.widestRow)

       for r, row in enumerate(self.rows):
           for c, col in enumerate(row):
               self.SetCellValue(r, c, col)
           self.SetColSize(c, 10*self.widestCol)

       for c, label in enumerate(self.header):
           self.SetColLabelValue(c, label)

   def loadFile(self):
       # you'd probably want to read a file here instead of a text string
       #lines = fileContents.split('\n')
       infile = open('test.sco', 'r')
       foundHeader = False
       for line in infile:
           if ";<sco_header>" in line:
               self.header = line.split()
               foundHeader = True
           if foundHeader:
               self.rows = [i.split() for i in line[1:]]
               self.widestRow = max([len(r) for r in self.rows])
               self.widestCol = max([len(c) for c in [r for r in self.rows]])

#---------------------------------------------------------------------------

class TestFrame(wx.Frame):
   def __init__(self, parent, log):
       wx.Frame.__init__(self, parent, -1, "sco file editor",
size=(640,480))
       grid = WordGrid(self, log)

#---------------------------------------------------------------------------

--
------------------------------------------------------------
Kent Quirk I'm making a game about global warming.
Game Architect Track the progress at:
CogniToy cognitoy.com - Diese Website steht zum Verkauf! - Informationen zum Thema cognitoy.

wx crashes when you delete the current folder selected with a wx.FileDialog
wx crashes when you sent a bad Apple Event (no protection)

Thread 0 Crashed:
0 com.apple.AE 0x91505acc implArrayRelease(__CFAllocator const*, void const*) + 0
1 com.apple.CoreFoundation 0x907f1928 CFArraySetValueAtIndex + 784
2 com.apple.AE 0x914fd470 AEListImpl::putElement(long, unsigned long, AEImpl const*) + 184
3 com.apple.AE 0x914fd23c AEPutDesc + 188
4 com.apple.NavigationServices 0x93150afc TBrowseDialog::NotifySelection(TNavNodeArray&, long) + 184
5 com.apple.NavigationServices 0x93150454 TBrowseDialog::NodeBrowserControlSelectionChanged(TNodeBrowserControl&) + 136
6 com.apple.NavigationServices 0x93146d20 TNodeBrowserControl::ItemNotification(OpaqueControlRef*, unsigned long, unsigned long) + 108
7 com.apple.HIToolbox 0x934fded0 CacheSelectionNotifications::~CacheSelectionNotifications [unified]() + 144
8 com.apple.HIToolbox 0x93321860 TListData::RemoveItems(unsigned long, unsigned long, unsigned long const*, IDataSorter const&) + 496

-plum

the problem appears on multi-files
" on a mix selection " it's crazy i don't understand why the array is " junk "

my app produces some files

1
/Users/plumber/Desktop/untitled folder.zip.MD5
2
/Users/plumber/Desktop/MD5.SUM
3
/Users/plumber/Desktop/MD5.SUM
1
/Users/plumber/Desktop/MD5.SUM.MD5
4
/Users/plumber/Desktop/MD5.SUM

the problem is when i mix droptarget/and file selection

this file creates the problem
/Users/plumber/Desktop/MD5.SUM.MD5

i reproduce the problem
when i delete this file /Users/plumber/Desktop/MD5.SUM.MD5
it crashes

-plum

i get it

it's when i remove/delete all files produced in one selection
one by one the "event" seems to "be refreshed"

HIToolBox problem

-plum