passing values to a program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric_Dexter@msn.com

    #1

    passing values to a program

    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(gridli b.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.SetCellVal ue(r, c, col)
    self.SetColSize (c, 10*self.widestC ol)

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

    def loadFile(self):
    #from_file
    infile = open('test.sco' , 'r')
    foundHeader = False
    self.rows = []
    for line in infile:
    if ";<sco_head er>" 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.appen d(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.Fr ame):
    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')



  • Fredrik Lundh

    #2
    Re: passing values to a program

    Eric_Dexter@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.
    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>

    Comment

    • wittempj@hotmail.com

      #3
      Re: passing values to a program


      Eric_Dexter@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(gridli b.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.SetCellVal ue(r, c, col)
      self.SetColSize (c, 10*self.widestC ol)
      >
      for c, label in enumerate(self. header):
      self.SetColLabe lValue(c, label)
      >
      def loadFile(self):
      #from_file
      infile = open('test.sco' , 'r')
      foundHeader = False
      self.rows = []
      for line in infile:
      if ";<sco_head er>" 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.appen d(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.Fr ame):
      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.

      Comment

      • Eric_Dexter@msn.com

        #4
        Re: passing values to a program

        If it is as simple as using the sys and dot befour the argv then I have
        it made.. The example I was trying to get that from came from the
        first edition programming python (I think they were using version 1.3).
        thanks for the help I will give that a try.

        https://sourceforge.net/project/stat...gn=dex-tracker


        Fredrik Lundh wrote:
        Eric_Dexter@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.
        >
        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>

        Comment

        Working...