Dialog Windows and Syntax

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Dialog Windows and Syntax

Philip Papush
Hi,

Suppose I have a simple dialog window that acceps a value for the parameter
x:

Sub Main
 Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1
    Text 20,21,190,28,"Enter value for x:",.Text1
    TextBox 20,63,90,21,.tbVariable
    OKButton 130,63,90,21
 End Dialog
 Dim dlg As UserDialog
 Dialog dlg
 Dim strSyntax As String
 strSyntax = "x=" & dlg.tbVariable & "."
 objSpssApp.ExecuteCommands strSyntax, True
End Sub


 Let's call this code 'Test.sbs'.  Now, the challenge for me is to make the
following Python block (inside a Syntax program) work:

...
BEGIN PROGRAM.
import spss
spss.Submit("SCRIPT '[the appropriate path]\Test.sbs'. ")
print x
END PROGRAM.
...

In other words I want to pass the values from a dialog box to Python
functions inside a large Syntax program. But this code doesn't work. It
calls the script which acceps a value for x, but it doesn't print x. SPSS
just gets frozen with label "Running Script...". I suspect that I did not
link the Python block and parameter x properly (I have no experience with
Sax Basic).

I was suggested the altenative solution. To modify the Script:

Sub Main
 Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1
    Text 20,21,190,28,"Enter value for x:",.Text1
    TextBox 20,63,90,21,.tbVariable
    OKButton 130,63,90,21
 End Dialog
 Dim dlg As UserDialog
 Dialog dlg
 Dim strSyntax As String
strX = dlg.tbVariable

strSyntax = "BEGIN PROGRAM." & vbLf & _
"import spss" & vbCr & _
"print " & strX & vbCrLf & _
"END PROGRAM."

objSpssApp.ExecuteCommands strSyntax, True

It works if you have 2-3 lines of Syntax code. But this won't help if you
have a large Syntax progam (> 1000 lines) with multiple Python blocks using
parameter x. I guess it is possible to include all the commands in quotes
inside a script (as shown above) even for such a big program. But there
should be a more elegant solution to the problem. Any help/hints will be
appreciated.

Thanks

Philip
Reply | Threaded
Open this post in threaded view
|

Re: Dialog Windows and Syntax

Mike P-5
Mark Antro has written something similar to what you want in the
developers forum in the SPSS website

http://www.spss.com/fusetalk/forum/messageview.cfm?catid=9&threadid=40&e
nterthread=y

Hth

Mike

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Philip Papush
Sent: 05 October 2006 16:45
To: [hidden email]
Subject: Dialog Windows and Syntax

Hi,

Suppose I have a simple dialog window that acceps a value for the
parameter
x:

Sub Main
 Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1
    Text 20,21,190,28,"Enter value for x:",.Text1
    TextBox 20,63,90,21,.tbVariable
    OKButton 130,63,90,21
 End Dialog
 Dim dlg As UserDialog
 Dialog dlg
 Dim strSyntax As String
 strSyntax = "x=" & dlg.tbVariable & "."
 objSpssApp.ExecuteCommands strSyntax, True End Sub


 Let's call this code 'Test.sbs'.  Now, the challenge for me is to make
the following Python block (inside a Syntax program) work:

...
BEGIN PROGRAM.
import spss
spss.Submit("SCRIPT '[the appropriate path]\Test.sbs'. ") print x END
PROGRAM.
...

In other words I want to pass the values from a dialog box to Python
functions inside a large Syntax program. But this code doesn't work. It
calls the script which acceps a value for x, but it doesn't print x.
SPSS just gets frozen with label "Running Script...". I suspect that I
did not link the Python block and parameter x properly (I have no
experience with Sax Basic).

I was suggested the altenative solution. To modify the Script:

Sub Main
 Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1
    Text 20,21,190,28,"Enter value for x:",.Text1
    TextBox 20,63,90,21,.tbVariable
    OKButton 130,63,90,21
 End Dialog
 Dim dlg As UserDialog
 Dialog dlg
 Dim strSyntax As String
strX = dlg.tbVariable

strSyntax = "BEGIN PROGRAM." & vbLf & _
"import spss" & vbCr & _
"print " & strX & vbCrLf & _
"END PROGRAM."

objSpssApp.ExecuteCommands strSyntax, True

It works if you have 2-3 lines of Syntax code. But this won't help if
you have a large Syntax progam (> 1000 lines) with multiple Python
blocks using parameter x. I guess it is possible to include all the
commands in quotes inside a script (as shown above) even for such a big
program. But there should be a more elegant solution to the problem. Any
help/hints will be appreciated.

Thanks

Philip

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. For more information on a proactive anti-virus
service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
Reply | Threaded
Open this post in threaded view
|

Re: Dialog Windows and Syntax

Philip Papush
In reply to this post by Philip Papush
Yes, you found our correspondence on this issue. But as I mentioned before
his solution works if you have a very small Syntax program.
Reply | Threaded
Open this post in threaded view
|

Re: Dialog Windows and Syntax

Mike P-5
In reply to this post by Philip Papush
Why not create a program in python to find and replace a special
character in your syntax file produced by your script as the argument?

Something along the lines of

main_dic = {"special character to be replaced":"your value X"}
file_name = "location of your syntax file"
f = open(file_name,"r")
file_string = f.read()
f.close()
new_file = open("location of your syntax file","w")
for k in main_dic.keys():
        file_string = file_string.replace("%s" % k, main_dic[k])
new_file.write(file_string)
new_file.close()

Combine this with the script that Antro sent through and it should
automate the process for each iteration...

Then you can use an insert command to run the syntax in python

file_main = ('''INSERT FILE='You syntax file' SYNTAX=interactive
CD=NO.''')

Hth
Mike


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Philip Papush
Sent: 05 October 2006 16:45
To: [hidden email]
Subject: Dialog Windows and Syntax

Hi,

Suppose I have a simple dialog window that acceps a value for the
parameter
x:

Sub Main
 Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1
    Text 20,21,190,28,"Enter value for x:",.Text1
    TextBox 20,63,90,21,.tbVariable
    OKButton 130,63,90,21
 End Dialog
 Dim dlg As UserDialog
 Dialog dlg
 Dim strSyntax As String
 strSyntax = "x=" & dlg.tbVariable & "."
 objSpssApp.ExecuteCommands strSyntax, True End Sub


 Let's call this code 'Test.sbs'.  Now, the challenge for me is to make
the following Python block (inside a Syntax program) work:

...
BEGIN PROGRAM.
import spss
spss.Submit("SCRIPT '[the appropriate path]\Test.sbs'. ") print x END
PROGRAM.
...

In other words I want to pass the values from a dialog box to Python
functions inside a large Syntax program. But this code doesn't work. It
calls the script which acceps a value for x, but it doesn't print x.
SPSS just gets frozen with label "Running Script...". I suspect that I
did not link the Python block and parameter x properly (I have no
experience with Sax Basic).

I was suggested the altenative solution. To modify the Script:

Sub Main
 Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1
    Text 20,21,190,28,"Enter value for x:",.Text1
    TextBox 20,63,90,21,.tbVariable
    OKButton 130,63,90,21
 End Dialog
 Dim dlg As UserDialog
 Dialog dlg
 Dim strSyntax As String
strX = dlg.tbVariable

strSyntax = "BEGIN PROGRAM." & vbLf & _
"import spss" & vbCr & _
"print " & strX & vbCrLf & _
"END PROGRAM."

objSpssApp.ExecuteCommands strSyntax, True

It works if you have 2-3 lines of Syntax code. But this won't help if
you have a large Syntax progam (> 1000 lines) with multiple Python
blocks using parameter x. I guess it is possible to include all the
commands in quotes inside a script (as shown above) even for such a big
program. But there should be a more elegant solution to the problem. Any
help/hints will be appreciated.

Thanks

Philip

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. For more information on a proactive anti-virus
service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
Reply | Threaded
Open this post in threaded view
|

Re: Dialog Windows and Syntax

Peck, Jon
In reply to this post by Philip Papush
Conflating SaxBasic scripting, traditional SPSS syntax, and Python is likely to require a lot of baling wire and produce an unwieldy and unmaintainable solution, if it works at all.  SaxBasic scripts run in the frontend context, while the syntax runs in a backend context.  These two technologies run asynchronously due, in part, to the need for distributed mode and in part due to their mainly separate concerns.

A better way to do this is to drive the gui parameter gathering from Python directly, dispensing with a separate scripting engine.

This is quite workable, but it requires you to learn a bit about programming a gui tool.

However, for simple situations, you can do something like the following.  This example uses wxPython, which is a freely downloadable gui toolkit.  (There are a number of other gui toolkits you could use, but I like this one the best.)

begin program.
import wx
app = wx.PySimpleApp()
dialog = wx.TextEntryDialog(None, "Please enter the value for x:", "My Procedure",
".05", style=wx.OK|wx.CANCEL)
if dialog.ShowModal() == wx.ID_OK:
    param = dialog.GetValue()
dialog.Destroy()
app.Destroy()
#use param in your program
end program.

This displays a text box with the "Please enter..." text and a space for the user to enter a value with a default value of .05.
Then you use the parameter the user entered in your Python program, whether you use it directly or substitute it in SPSS syntax that you submit from the Python code.  You can, of course make this example more elaborate, but it is simple to do simple things.

I have simple examples with wxPython also for selecting from a variable list or just displaying a message box with OK/Cancel buttons.

This solution does not have synchronization problems; it looks good, and it is easy to code.

I can send these simple examples off list to anyone who requests them.

HTH,
Jon Peck
SPSS

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Philip Papush
Sent: Thursday, October 05, 2006 10:45 AM
To: [hidden email]
Subject: [SPSSX-L] Dialog Windows and Syntax

Hi,

Suppose I have a simple dialog window that acceps a value for the parameter
x:

Sub Main
 Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1
    Text 20,21,190,28,"Enter value for x:",.Text1
    TextBox 20,63,90,21,.tbVariable
    OKButton 130,63,90,21
 End Dialog
 Dim dlg As UserDialog
 Dialog dlg
 Dim strSyntax As String
 strSyntax = "x=" & dlg.tbVariable & "."
 objSpssApp.ExecuteCommands strSyntax, True
End Sub


 Let's call this code 'Test.sbs'.  Now, the challenge for me is to make the
following Python block (inside a Syntax program) work:

...
BEGIN PROGRAM.
import spss
spss.Submit("SCRIPT '[the appropriate path]\Test.sbs'. ")
print x
END PROGRAM.
...

In other words I want to pass the values from a dialog box to Python
functions inside a large Syntax program. But this code doesn't work. It
calls the script which acceps a value for x, but it doesn't print x. SPSS
just gets frozen with label "Running Script...". I suspect that I did not
link the Python block and parameter x properly (I have no experience with
Sax Basic).

I was suggested the altenative solution. To modify the Script:

Sub Main
 Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1
    Text 20,21,190,28,"Enter value for x:",.Text1
    TextBox 20,63,90,21,.tbVariable
    OKButton 130,63,90,21
 End Dialog
 Dim dlg As UserDialog
 Dialog dlg
 Dim strSyntax As String
strX = dlg.tbVariable

strSyntax = "BEGIN PROGRAM." & vbLf & _
"import spss" & vbCr & _
"print " & strX & vbCrLf & _
"END PROGRAM."

objSpssApp.ExecuteCommands strSyntax, True

It works if you have 2-3 lines of Syntax code. But this won't help if you
have a large Syntax progam (> 1000 lines) with multiple Python blocks using
parameter x. I guess it is possible to include all the commands in quotes
inside a script (as shown above) even for such a big program. But there
should be a more elegant solution to the problem. Any help/hints will be
appreciated.

Thanks

Philip