Passing information to Python handler in SPSS 14

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

Passing information to Python handler in SPSS 14

Bryan Tec
Hello,



I am trying to send a variable list to the python handler from within a
macro in SPSS 14.  Specifically, I am attempting to do something like this:



DEFINE simplemacro (vars = v1 v2 v3)



BEGIN PROGRAM.

import spss



varlist = !vars



END PROGRAM.



!ENDDEFINE.



Of course there will be more meat in the middle, but this is the relevant
task.  The Programming and Data Management for SPSS 16 text suggests
defining and getting a data file attribute using the
spss.GetDataFileAttributes function but when I try this route, I get an
error that the module is not found.  The module is not listed in the online
help installed with the plugin so I imagine that it is not included in the
version for SPSS 14.



Is there any other way to achieve this?



Thanks,

Bryan

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Passing information to Python handler in SPSS 14

Albert-Jan Roskam
Hi,

I am pretty sure that what you're trying to do is not possible: a beginprogram-endprogram block within a define-!enddefine block (or vice versa) is not allowed.

begin program.
import spss
for i in range(spss.GetVariableCount()):
____print spss.GetVariableName(i)
end program.

Cheers,
Albert-Jan

--- On Thu, 6/26/08, Bryan Tec <[hidden email]> wrote:

> From: Bryan Tec <[hidden email]>
> Subject: Passing information to Python handler in SPSS 14
> To: [hidden email]
> Date: Thursday, June 26, 2008, 3:48 PM
> Hello,
>
>
>
> I am trying to send a variable list to the python handler
> from within a
> macro in SPSS 14.  Specifically, I am attempting to do
> something like this:
>
>
>
> DEFINE simplemacro (vars = v1 v2 v3)
>
>
>
> BEGIN PROGRAM.
>
> import spss
>
>
>
> varlist = !vars
>
>
>
> END PROGRAM.
>
>
>
> !ENDDEFINE.
>
>
>
> Of course there will be more meat in the middle, but this
> is the relevant
> task.  The Programming and Data Management for SPSS 16 text
> suggests
> defining and getting a data file attribute using the
> spss.GetDataFileAttributes function but when I try this
> route, I get an
> error that the module is not found.  The module is not
> listed in the online
> help installed with the plugin so I imagine that it is not
> included in the
> version for SPSS 14.
>
>
>
> Is there any other way to achieve this?
>
>
>
> Thanks,
>
> Bryan
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body
> text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the
> command
> INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Passing information to Python handler in SPSS 14

Peck, Jon
In reply to this post by Bryan Tec
Macros are not processed inside a program.  Between BEGIN PROGRAM PYTHON and END PROGRAM, all the code must be pure Python.

The typical way of passing a parameter list to a program would be to define a function - inline in the program or in a separate module, and then to call that function with the appropriate variable list.

For example,
BEGIN PROGRAM.
import spss, mymodulelibrary

mymodulelibrary('a','b','c')
END PROGRAM.

or something like that.  The spssaux.VariableDict class has methods for dealing with variable dictionaries and processing TO specifications among other things.

However, if you want to create the variable list in pure SPSS code and save it in a datafile attribute and then retrieve it with Python code, that is quite possible with SPSS 14.

There is a GetDataFileAttributes api in the spss module, but it was added in SPSS 15.  To do this with SPSS 14 is a bit more roundabout.  Suppose you have a dataset attribute named "fred".  Here is how you can retrieve it - it requires the spssaux module from SPSS Developer Central.

begin program.
import spss, spssaux
tag, err = spssaux.CreateXmlOutput("display attributes",
omsid="file information", subtype="Datafile Attributes")
attrvalue = spssaux.getValuesFromXmlWorkspace(tag, tableSubtype='Datafile Attributes',
 rowCategory="fred")


The CreateXMLOutput api inserts the attributes table into the XMLWorkspace.  The getValuesFromXmlWorkspace call returns the value(s) of fred as a list - a one-item list if fred is a simple attribute and more if it is an array attribute.


HTH,
Jon Peck



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bryan Tec
Sent: Thursday, June 26, 2008 7:48 AM
To: [hidden email]
Subject: [SPSSX-L] Passing information to Python handler in SPSS 14

Hello,



I am trying to send a variable list to the python handler from within a
macro in SPSS 14.  Specifically, I am attempting to do something like this:



DEFINE simplemacro (vars = v1 v2 v3)



BEGIN PROGRAM.

import spss



varlist = !vars



END PROGRAM.



!ENDDEFINE.



Of course there will be more meat in the middle, but this is the relevant
task.  The Programming and Data Management for SPSS 16 text suggests
defining and getting a data file attribute using the
spss.GetDataFileAttributes function but when I try this route, I get an
error that the module is not found.  The module is not listed in the online
help installed with the plugin so I imagine that it is not included in the
version for SPSS 14.



Is there any other way to achieve this?



Thanks,

Bryan

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Passing information to Python handler in SPSS 14

Bauer, John H.
In reply to this post by Albert-Jan Roskam
It isn't supposed to work, and it's incredibly ugly, but not impossbile.  You have to end every line of Python code with '#.' and there must be no blank lines.  See the discussion at:

http://www.spss.com/fusetalk/forum/messageview.cfm?catid=9&threadid=52&highlight_key=y&keyword1=macro

Then try to figure out a better way of getting the job done.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Albert-jan Roskam
Sent: Thursday, June 26, 2008 10:15 AM
To: [hidden email]
Subject: Re: Passing information to Python handler in SPSS 14

Hi,

I am pretty sure that what you're trying to do is not possible: a beginprogram-endprogram block within a define-!enddefine block (or vice versa) is not allowed.

begin program.
import spss
for i in range(spss.GetVariableCount()):
____print spss.GetVariableName(i)
end program.

Cheers,
Albert-Jan

--- On Thu, 6/26/08, Bryan Tec <[hidden email]> wrote:

> From: Bryan Tec <[hidden email]>
> Subject: Passing information to Python handler in SPSS 14
> To: [hidden email]
> Date: Thursday, June 26, 2008, 3:48 PM Hello,
>
>
>
> I am trying to send a variable list to the python handler
> from within a
> macro in SPSS 14.  Specifically, I am attempting to do
> something like this:
>
>
>
> DEFINE simplemacro (vars = v1 v2 v3)
>
>
>
> BEGIN PROGRAM.
>
> import spss
>
>
>
> varlist = !vars
>
>
>
> END PROGRAM.
>
>
>
> !ENDDEFINE.
>
>
>
> Of course there will be more meat in the middle, but this
> is the relevant
> task.  The Programming and Data Management for SPSS 16 text
> suggests
> defining and getting a data file attribute using the
> spss.GetDataFileAttributes function but when I try this
> route, I get an
> error that the module is not found.  The module is not
> listed in the online
> help installed with the plugin so I imagine that it is not
> included in the
> version for SPSS 14.
>
>
>
> Is there any other way to achieve this?
>
>
>
> Thanks,
>
> Bryan
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body
> text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the
> command
> INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Passing information to Python handler in SPSS 14

Albert-Jan Roskam
In reply to this post by Bryan Tec
Sorry, I was rushing earlier. Here's some more code. Not sure if it is what you want. It assigns the variable list to both a Python variable and an SPSS macro.

begin program.
import spss
for i in range(spss.GetVariableCount()):
        varlist = spss.GetVariableName(i)
        print varlist
spss.SetMacroValue('!simplemacro',varlist)
spss.Submit("""display macros.
                fre !simplemacro.""")
end program.

Cheers!!
Albert-Jan

--- On Thu, 6/26/08, Bryan Tec <[hidden email]> wrote:

> From: Bryan Tec <[hidden email]>
> Subject: Passing information to Python handler in SPSS 14
> To: [hidden email]
> Date: Thursday, June 26, 2008, 3:48 PM
> Hello,
>
>
>
> I am trying to send a variable list to the python handler
> from within a
> macro in SPSS 14.  Specifically, I am attempting to do
> something like this:
>
>
>
> DEFINE simplemacro (vars = v1 v2 v3)
>
>
>
> BEGIN PROGRAM.
>
> import spss
>
>
>
> varlist = !vars
>
>
>
> END PROGRAM.
>
>
>
> !ENDDEFINE.
>
>
>
> Of course there will be more meat in the middle, but this
> is the relevant
> task.  The Programming and Data Management for SPSS 16 text
> suggests
> defining and getting a data file attribute using the
> spss.GetDataFileAttributes function but when I try this
> route, I get an
> error that the module is not found.  The module is not
> listed in the online
> help installed with the plugin so I imagine that it is not
> included in the
> version for SPSS 14.
>
>
>
> Is there any other way to achieve this?
>
>
>
> Thanks,
>
> Bryan
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body
> text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the
> command
> INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD