Why wouldn't you do it the other way around?
Each function should be as focused as possible. Your CreateGrid
function should have the responsibility to read the CSV file and return
the data list and the column names. That way, if you decide to change
to a database or some other data source, you only have to change that
one spot.
Then, OnOpen can focus on taking those results and turning them into a GUI.
def GetData( filename ):
csvfile = open( filename, 'rb' )
sample = csvfile.read( 2048 )
csvfile.seek( 0 )
# Check for file format with sniffer.
dialect = csv.Sniffer().sniff(sample)
csvfile = csv.reader( csvfile, dialect )
# Grab a sample and see if there is a header.
if csv.Sniffer().has_header( sample ):
colnames = csvfile.next()
datalist = list( csvfile )
else:
datalist = list( csvfile )
colnames = ['col_%d' for %d in range(len(datalist[0]))]
return colnames, datalist
class MyFrame(wx.Frame):
...
def OnOpen( self, event ):
dlg = wx.FileDialog( ... )
if dlg.ShowModal() != ID_OK:
return
colnames, datalist = GetData( dlg.GetFilename() )
self.grid = wx.grid.Grid( self, -1 )
self.grid.CreateGrid( len(datalist), len(colname) )
... etc ...
···
On Tue, 14 Apr 2009 14:58:42 -0300,Carlos "Guâno" Grohmann <carlos.grohmann@gmail.com> wrote:
I'm working my way through python/wxpython trying to make a simple csv
viewer. So far, it works (code attached showcsv.py), and opens the
data (file data.txt) in to a grid. Now I'm trying to separate the grid
creation from the onOpen function, but without success (code
showcsv2.py). My problem is that I don't know how to retrieve the
values of "datalist" and "colnames" from the onOpen function and use
them in the creatGrid function.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Thanks Tim, that was really helpful
Carlos
···
On Tue, Apr 14, 2009 at 15:49, Tim Roberts <timr@probo.com> wrote:
On Tue, 14 Apr 2009 14:58:42 -0300,Carlos "Guâno" Grohmann > <carlos.grohmann@gmail.com> wrote:
I'm working my way through python/wxpython trying to make a simple csv
viewer. So far, it works (code attached showcsv.py), and opens the
data (file data.txt) in to a grid. Now I'm trying to separate the grid
creation from the onOpen function, but without success (code
showcsv2.py). My problem is that I don't know how to retrieve the
values of "datalist" and "colnames" from the onOpen function and use
them in the creatGrid function.
Why wouldn't you do it the other way around?
Each function should be as focused as possible. Your CreateGrid
function should have the responsibility to read the CSV file and return
the data list and the column names. That way, if you decide to change
to a database or some other data source, you only have to change that
one spot.
Then, OnOpen can focus on taking those results and turning them into a GUI.
def GetData( filename ):
csvfile = open( filename, 'rb' )
sample = csvfile.read( 2048 )
csvfile.seek( 0 )
\# Check for file format with sniffer\.
dialect = csv\.Sniffer\(\)\.sniff\(sample\)
csvfile = csv\.reader\( csvfile, dialect \)
\# Grab a sample and see if there is a header\.
if csv\.Sniffer\(\)\.has\_header\( sample \):
colnames = csvfile\.next\(\)
datalist = list\( csvfile \)
else:
datalist = list\( csvfile \)
colnames = \['col\_%d' for %d in range\(len\(datalist\[0\]\)\)\]
return colnames, datalist
class MyFrame(wx.Frame):
...
def OnOpen( self, event ):
dlg = wx.FileDialog( ... )
if dlg.ShowModal() != ID_OK:
return
colnames, datalist = GetData\( dlg\.GetFilename\(\) \)
self\.grid = wx\.grid\.Grid\( self, \-1 \)
self\.grid\.CreateGrid\( len\(datalist\), len\(colname\) \)
\.\.\. etc \.\.\.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
--
Carlos Henrique Grohmann - Geologist D.Sc.
a.k.a. Guano - Linux User #89721
ResearcherID: A-9030-2008
carlos dot grohmann at gmail dot com
http://www.igc.usp.br/pessoais/guano/
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke
Can’t stop the signal.