passing values to a program

wittempj@hotmail.com martin.witte at gmail.com
Thu Oct 19 02:47:25 EDT 2006


Eric_Dexter at msn.com wrote:
> 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.




More information about the Python-list mailing list