473,473 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

46 New Member
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 3915
bartonc
6,596 Recognized Expert Expert
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 Recognized Expert Expert
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 Recognized Expert Expert
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
eric dexter
46 New Member
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 Recognized Expert Expert
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
eric dexter
46 New Member
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 Recognized Expert Expert
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 Recognized Expert Expert
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
eric dexter
46 New Member
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 Recognized Expert Expert
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 Recognized Expert Expert
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
eric dexter
46 New Member
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 Recognized Expert Expert
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
eric dexter
46 New Member
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 Recognized Expert Expert
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.