473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
Nov 2 '06 #1
15 3912
bartonc
6,596 Expert 4TB
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.  
Nov 3 '06 #2
bartonc
6,596 Expert 4TB
In order to prevent these kinds of errors, test for len(sys.argv) and do something clever if no args are sent in.
Nov 3 '06 #3
bartonc
6,596 Expert 4TB
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.
Nov 3 '06 #4
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)
Nov 3 '06 #5
bartonc
6,596 Expert 4TB
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.
Nov 4 '06 #6
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)
Nov 4 '06 #7
bartonc
6,596 Expert 4TB
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.
Nov 4 '06 #8
bartonc
6,596 Expert 4TB
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
Nov 5 '06 #9
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??


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
Nov 6 '06 #10
bartonc
6,596 Expert 4TB
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.  
Nov 6 '06 #11
bartonc
6,596 Expert 4TB
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.
Nov 6 '06 #12
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.  
Nov 6 '06 #13
bartonc
6,596 Expert 4TB
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.

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()
Nov 6 '06 #14
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
Nov 7 '06 #15
bartonc
6,596 Expert 4TB
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
Nov 7 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David | last post by:
I have this error message poping up when I try to import a module I made in C using the Python/C API. Everything compiles like a charm. Gives me this error message : Traceback (most recent...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
5
by: MrNobody | last post by:
I am using the no-arg ShowDialog() method hoping that the window would not be modal to any other window like the other ShowDialog(IWin32Window) method does, but when this opens it somehow becomes...
1
by: ohaqqi | last post by:
Hi guys, I'm still working on my shell. I'm trying to implement a function typefile that will take a command line input as follows: > type <file1> This command will implement a catenation of...
6
by: kdikert | last post by:
I have a worker thread, and when the worker is done I want it to show a popup dialog. The sample application below demonstrates this. There are two buttons: a "Show dialog" button which immediately...
6
by: Scott Gravenhorst | last post by:
Windows XP SP3 My application is set to open a SaveFile dialog when an exit is requested. When I click the app's close button, the save dialog opens, but when I click to change the folder, the...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.