Connecting Tech Pros Worldwide Forums | Help | Site Map

trouble using a file dialog to call a main function with two args [solved]

Member
 
Join Date: Sep 2006
Location: corpus christi
Posts: 46
#1: Nov 2 '06
csoundgrid2.main(filename, """;<sco_header>""")
isn't working like I want it to.

I was trying to find out what my value for filename is but it will not
print. The file I am calling csoundgrid2.main seems to work fine when
I call it directly from spe with two values and I have used this file
dialog to load other files although I think it is the broken part of it. The dialog comes from boa-constructer (4.4?) thanks for any
help in advance

File "C:\Python24\Lib\site-packages\boa-constructor\test of
snake\csoundgrid2.py", line 35, in loadFile
infile = open(sys.argv[1], 'r') #The first argument passed in is the
file name
IndexError: list index out of range


Expand|Select|Wrap|Line Numbers
  1.  
  2. def sco_editor(self):
  3.          "Pick a sco file and load it into a sco editor"
  4.          dlg = wx.FileDialog(self,"load sco file", ".", "", "*.sco", wx.OPEN)
  5.          try:
  6.              if dlg.ShowModal() == wx.ID_OK:
  7.                  filename = dlg.GetPath()
  8.                  # Your code
  9.                  print filename
  10.                  csoundgrid2.main(filename, """;<sco_header>""")
  11.                  #("csoundgrid2.py", filename ,""";<sco_header>""", shell=True)
  12.                  #os.startfile('csoundgrid2.py', filename, """;<sco_header>""")
  13.          finally:
  14.             dlg.Destroy()
  15.  
  16.  
if for some reason other files are needed (some are older files though)
https://sourceforge.net/project/showfiles.php?group_id=156455

bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,403
#2: Nov 3 '06

re: trouble using a file dialog to call a main function with two args [solved]


The trouble is the way that you are starting the script.
When I use SPE Tools->Run/Stop and type in args, this works:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. class aclass:
  3.     def test(self):
  4.         print sys.argv[1]
  5. t = aclass()
  6. t.test()
  7.  
It works when I import this into another module, too.

Expand|Select|Wrap|Line Numbers
  1. import ed2
  2. t = ed2.aclass()
  3. t.test()
  4.  
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,403
#3: Nov 3 '06

re: trouble using a file dialog to call a main function with two args [solved]


In order to prevent these kinds of errors, test for len(sys.argv) and do something clever if no args are sent in.
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,403
#4: Nov 3 '06

re: trouble using a file dialog to call a main function with two args [solved]


File "C:\Python24\Lib\site-packages\boa-constructor\test of
snake\csoundgrid2.py", line 35, in loadFile
infile = open(sys.argv[1], 'r') #The first argument passed in is the file name
IndexError: list index out of range
loadfile() is being called in __init__. Don't do that yet, 'cause you don't have the filename yet.
Member
 
Join Date: Sep 2006
Location: corpus christi
Posts: 46
#5: Nov 3 '06

re: trouble using a file dialog to call a main function with two args [solved]


It works fine in spe as long as I call it with test.sco ;<header_file> (I have to check the spelling on the second) even with that declared in init. I call it with two arguments or it fails. When I try to call it with the dialog that is when I start having trouble. I am having trouble everywhere I am using the file dialog box in (in csoundroutines module anyway) this is just the routine that I have tested the most without the file dialog part. (the dialog is out of boa-constructor)
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,403
#6: Nov 4 '06

re: trouble using a file dialog to call a main function with two args [solved]


Quote:

Originally Posted by eric dexter

It works fine in spe as long as I call it with test.sco ;<header_file> (I have to check the spelling on the second) even with that declared in init. I call it with two arguments or it fails. When I try to call it with the dialog that is when I start having trouble. I am having trouble everywhere I am using the file dialog box in (in csoundroutines module anyway) this is just the routine that I have tested the most without the file dialog part. (the dialog is out of boa-constructor)

What version of Boa Constructer are you using?
What version of wx are you using?
If you upgrade wx recently, you may have trouble with older version of Boa.
Member
 
Join Date: Sep 2006
Location: corpus christi
Posts: 46
#7: Nov 4 '06

re: trouble using a file dialog to call a main function with two args [solved]


I haven't upgraded to the latest version yet, I believe it is wxpython 2.6 and boa constructor .44. I am planning to take a stab in the dark and see if I add + 'sco' to the filename and see if it works. possibly adding an extension instead of a wildcard changes the value of the filename. (possibly I am wrong but it should work then)
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,403
#8: Nov 4 '06

re: trouble using a file dialog to call a main function with two args [solved]


Quote:

Originally Posted by eric dexter

I haven't upgraded to the latest version yet, I believe it is wxpython 2.6 and boa constructor .44. I am planning to take a stab in the dark and see if I add + 'sco' to the filename and see if it works. possibly adding an extension instead of a wildcard changes the value of the filename. (possibly I am wrong but it should work then)

You still haven't posted relevent error messages.
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,403
#9: Nov 5 '06

re: trouble using a file dialog to call a main function with two args [solved]


Quote:

Originally Posted by eric dexter

It works fine in spe as long as I call it with test.sco ;<header_file> (I have to check the spelling on the second) even with that declared in init. I call it with two arguments or it fails. When I try to call it with the dialog that is when I start having trouble. I am having trouble everywhere I am using the file dialog box in (in csoundroutines module anyway) this is just the routine that I have tested the most without the file dialog part. (the dialog is out of boa-constructor)

This is how the Boa docs say you open the file dialog:
Expand|Select|Wrap|Line Numbers
  1.     def OnMenuFileOpenMenu(self, event):
  2.         dlg = wx.FileDialog(self, "Choose a file", ".", "", "*.*", wx.OPEN)
  3.         try:
  4.             if dlg.ShowModal() == wx.ID_OK:
  5.                 filename = dlg.GetPath()
  6.                 # Your code
  7.         finally:
  8.             dlg.Destroy()
  9.  
If you don't have menus yet, go through the getting started section of the help file. I can help you insert you code into a Boa generated frame.
Have fun,
Barton
Member
 
Join Date: Sep 2006
Location: corpus christi
Posts: 46
#10: Nov 6 '06

re: trouble using a file dialog to call a main function with two args [solved]


This is the last error that I got

File "C:\Python24\Lib\site-packages\boa-constructor\test of snake\csoundgrid2.py", line 35, in loadFile
print sys.argv[1]
IndexError: list index out of range

evidenly I am not passing two values to the program although it looks like I am
my code is a slightly modified version of what you have

Expand|Select|Wrap|Line Numbers
  1. def sco_editor(self):
  2.          "Pick a sco file and load it into a sco editor"
  3.          dlg = wx.FileDialog(self,"load sco file", ".", "", "*.sco", wx.OPEN)
  4.          try:
  5.              if dlg.ShowModal() == wx.ID_OK:
  6.                  filename = dlg.GetPath()
  7.                  # Your code
  8.                  print filename
  9.                  csoundgrid2.main(filename + '.sco', """;<sco_header>""")
  10.                  #("csoundgrid2.py", filename ,""";<sco_header>""", shell=True)
  11.                  #os.startfile('csoundgrid2.py', filename, """;<sco_header>""")
  12.          finally:
  13.             dlg.Destroy()
  14.  
I am thinking it has to be the line I am trying to call the grid with
csoundgrid2.main(filename,"""<sco_header>""" isn't realy passing arguments to the main function that I am importing through csoundgrid2. Do I need the .main??? Do I have to do it another way i.e. through a subroutine not refered to as main??


Quote:

Originally Posted by bartonc

This is how the Boa docs say you open the file dialog:

Expand|Select|Wrap|Line Numbers
  1.     def OnMenuFileOpenMenu(self, event):
  2.         dlg = wx.FileDialog(self, "Choose a file", ".", "", "*.*", wx.OPEN)
  3.         try:
  4.             if dlg.ShowModal() == wx.ID_OK:
  5.                 filename = dlg.GetPath()
  6.                 # Your code
  7.         finally:
  8.             dlg.Destroy()
  9.  
If you don't have menus yet, go through the getting started section of the help file. I can help you insert you code into a Boa generated frame.
Have fun,
Barton

bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,403
#11: Nov 6 '06

re: trouble using a file dialog to call a main function with two args [solved]


Your problem is here:

Expand|Select|Wrap|Line Numbers
  1.     def loadFile(self):
  2.      #from_file 
  3. #######################
  4.      infile = open(sys.argv[1], 'r') #The first argument passed in is the file name
  5.      foundHeader = False
  6.      self.rows = []
  7.      for line in infile:
  8.          if sys.argv[2] in line: #look for the second argument and make that the header
  9. ########################
  10.              #removefirst = line.split(' ')
  11.              self.header = line.split()
  12.              #foundHeader = 'true'
  13.              continue     # we don't want to process this line any further
  14.          else:
  15.              self.rows.append(line.split())
  16.  
  17.      self.widestRow = max([len(r) for r in self.rows])
  18.      self.widestCol = max([len(c) for c in [r for r in self.rows]])
  19.  
because there are no sys.argv when you call it like this.
Change it to:
Expand|Select|Wrap|Line Numbers
  1.     def loadFile(self, filename, key):
  2.      #from_file 
  3.      infile = open(filename, 'r') #The first argument passed in is the file name
  4.      foundHeader = False
  5.      self.rows = []
  6.      for line in infile:
  7.          if key in line: #look for the second argument and make that the header
  8.  
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,403
#12: Nov 6 '06

re: trouble using a file dialog to call a main function with two args [solved]


Of course, you'll also need to change wordgrid:
Expand|Select|Wrap|Line Numbers
  1. class WordGrid(gridlib.Grid):
  2.  
  3.     def __init__(self, parent, log, filename, key):
  4.         gridlib.Grid.__init__(self, parent, -1)
  5.         self.loadFile(filename, key)
  6.  
etc.
Member
 
Join Date: Sep 2006
Location: corpus christi
Posts: 46
#13: Nov 6 '06

re: trouble using a file dialog to call a main function with two args [solved]


why would that work inside of spe with two arguments and not called from another program??? Is there a way to make two global arguments in the file I am calling that I can use for this and then just use the main function for testing?? Is there any special meaning to the arguments in your example with key exc?? I think I want arrg1 and arrg2 as global to grid so I can leave the rest of it alone, is there a way to do that??

Expand|Select|Wrap|Line Numbers
  1. def csgrid(From_File, find_string):
  2.     "This is just main restated to see if that will make it work"
  3.     arrg1 = From_File
  4.     arrg2 = find_string
  5.     app = wx.PySimpleApp()
  6.     frame = TestFrame(None, sys.stdout)
  7.     frame.Show(True)
  8.     app.MainLoop()
  9.     pass
  10.  
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,403
#14: Nov 6 '06

re: trouble using a file dialog to call a main function with two args [solved]


Starting a script from the command line (inside spe w/ args) creates sys.argv in that module's namespace. Calling a funtion even in another module just passes in arguments.

Quote:

Originally Posted by eric dexter

why would that work inside of spe with two arguments and not called from another program??? Is there a way to make two global arguments in the file I am calling that I can use for this and then just use the main function for testing?? Is there any special meaning to the arguments in your example with key exc?? I think I want arrg1 and arrg2 as global to grid so I can leave the rest of it alone, is there a way to do that??

Expand|Select|Wrap|Line Numbers
  1. def csgrid(From_File, find_string):
  2. "This is just main restated to see if that will make it work"
  3. arrg1 = From_File
  4. arrg2 = find_string
  5. app = wx.PySimpleApp()
  6. frame = TestFrame(None, sys.stdout)
  7. frame.Show(True)
  8. app.MainLoop()
  9. pass
  10.  

I recommend that you study the subject of "variable scope rules" so you understand why arguments are passed into functions and globals are discouraged. There is no special meaning to the names that I used. If you don't want to create the frame with the added arguments, you should add a "setter" method to your subclass, ie:
Expand|Select|Wrap|Line Numbers
  1. def SetUpArgs(filename, key):
  2.     self.filename = filename
  3.     # etc.
  4.  
then move everthing in __init__ after loadfile to its own method (say) CreateGridFromFile()
then call
grid.SetUpArg()
grid.loadfile()
grid.CreateGridFromFile()
Member
 
Join Date: Sep 2006
Location: corpus christi
Posts: 46
#15: Nov 7 '06

re: trouble using a file dialog to call a main function with two args [solved]


Quote:

Originally Posted by bartonc

Starting a script from the command line (inside spe w/ args) creates sys.argv in that module's namespace. Calling a funtion even in another module just passes in arguments.



I recommend that you study the subject of "variable scope rules" so you understand why arguments are passed into functions and globals are discouraged. There is no special meaning to the names that I used. If you don't want to create the frame with the added arguments, you should add a "setter" method to your subclass, ie:

Expand|Select|Wrap|Line Numbers
  1. def SetUpArgs(filename, key):
  2.     self.filename = filename
  3.     # etc.
  4.  
then move everthing in __init__ after loadfile to its own method (say) CreateGridFromFile()
then call
grid.SetUpArg()
grid.loadfile()
grid.CreateGridFromFile()

That answers my question about why it acts different.. I understand that it is bad practice but I do want it work the same regardless of weather it is called or if it is used from the command line.. I'll look into the scope and how to set something up to work like args do in the command line.. Thanks for the help.

http://www.dexrow.com
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,403
#16: Nov 7 '06

re: trouble using a file dialog to call a main function with two args [solved]


Quote:

Originally Posted by eric dexter

That answers my question about why it acts different.. I understand that it is bad practice but I do want it work the same regardless of weather it is called or if it is used from the command line.. I'll look into the scope and how to set something up to work like args do in the command line.. Thanks for the help.

http://www.dexrow.com

Any time,
Barton
Closed Thread