passing values to a program

E

Eric_Dexter

I almost have this thing running like I want it to run but I want
the values to come from the program that calls this one. There are two
things I want to pass File_Name and CutString. They both need to go to
loadFile routine of Class WordGrid to replace constants. Thank you for
putting up with my quesitons in advance.


import wx
import wx.grid as gridlib



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

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):
#from_file
infile = open('test.sco', 'r')
foundHeader = False
self.rows = []
for line in infile:
if ";<sco_header>" in line:
#removefirst = line.split(' ')
self.header = line.split()
#foundHeader = 'true'
continue # we don't want to process this line any
further
else:
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]])



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

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)

#---------------------------------------------------------------------------
#def main():

def main(From_File, string):
import sys
From_file = argv[1]
#split_string = argv2[2]
app = wx.PySimpleApp()
frame = TestFrame(None, sys.stdout)
frame.Show(True)
app.MainLoop()
pass

if __name__ == '__main__':
import sys
main('test.sco', 'sfd')

http://www.dexrow.com
 
F

Fredrik Lundh

I almost have this thing running like I want it to run but I want
the values to come from the program that calls this one. There are two
things I want to pass File_Name and CutString. They both need to go to
loadFile routine of Class WordGrid to replace constants.

note that your code is already using "sys.argv" to read arguments from
the command line (inside the main function), so obviously you know how
to do that, and your code is passing arguments around (to both the
constructor and the main function), so obviously you know how to do that.

so what's the problem here? why not just use the mechanisms you already
know how to use?

</F>
 
W

wittempj

I almost have this thing running like I want it to run but I want
the values to come from the program that calls this one. There are two
things I want to pass File_Name and CutString. They both need to go to
loadFile routine of Class WordGrid to replace constants. Thank you for
putting up with my quesitons in advance.


import wx
import wx.grid as gridlib



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

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):
#from_file
infile = open('test.sco', 'r')
foundHeader = False
self.rows = []
for line in infile:
if ";<sco_header>" in line:
#removefirst = line.split(' ')
self.header = line.split()
#foundHeader = 'true'
continue # we don't want to process this line any
further
else:
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]])



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

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)

#---------------------------------------------------------------------------
#def main():

def main(From_File, string):
import sys
From_file = argv[1]
#split_string = argv2[2]
app = wx.PySimpleApp()
frame = TestFrame(None, sys.stdout)
frame.Show(True)
app.MainLoop()
pass

if __name__ == '__main__':
import sys
main('test.sco', 'sfd')

http://www.dexrow.com

Try this code, save it in file called test.py

def main(From_File, string):
print 'From_File: %s' % From_File
print 'string: %s' % string

if __name__ == '__main__':
import sys
print 'command line'
print sys.argv
main(sys.argv[1], sys.argv[2])

print 'hardcoded'
main('test.sco', 'sfd')

H:\>test.py arg1 arg2
command line
['H:\\test.py', 'arg1', 'arg2']
From_File: arg1
string: arg2
hardcoded
From_File: test.sco
string: sfd

argv is in namespace sys, but you don't tell that. Also I would
consider using a none built in and less generic name for the argument
'string' you're using.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top