Python Functions Arguments

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

Python Functions Arguments

Jignesh Sutar
Hello,
 
I'm experimenting with python and have a few questions on how to adapt the following macro to make it super efficient and user friendly...
 
BEGIN PROGRAM.
import spss, spssaux
def demo (vars):
    varList=vars.split()
    vardict= spssaux.VariableDict(varList)
    varStr=" ".join(varList)
    spss.Submit("""freq %(varStr)s. """ % locals())
 
demo(vars="gender bdate educ jobcat salary salbegin jobtime prevexp minority")
 
demo(vars="gender bdate educ jobcat"
"salary salbegin jobtime prevexp minority")
END PROGRAM.
 
1. The parameter vars in the current example accepts only a list of variables but is it possible also to input in this very same parameter (or any other work around) a way where I can specify a regular expression match? For this example, say, the variables starting with the letter "s" as an example?
 
2. I haven't looked into this into detail but I've seen other post about using the TO conversion also can this also be built in...so potentially there could be 3 different ways of specifying the arguments for the parameter vars?
 
3. Is it possible specify vars without having to enclose them in quotation marks?
 
4. Often the list of variables will be a long list so its necessary to allow them to wrap over multiple lines but in a python function calls this can only be done if each line is enclosed in quotation marks (see second macro call in example syntax above). Is there a better work around to this?
 
 
I ask most of these questions because I'm currently in the process of converting macros written in conventional SPSS macro syntax to python programs. In a conventional SPSS macro call I could easily list all the variables, across multiple lines not have to quote them. I'm trying to keep things as similar as possible because others and I have got into a habit of calling macros in a particular way...
 
Many thanks in advance...
 
Jignesh
Reply | Threaded
Open this post in threaded view
|

Re: Python Functions Arguments

jkpeck
1. The spssaux.VariableDict object contents can be defined by a regular expression by using the pattern argument instead of the names.  For example,

vardict = spssaux.VariableDict(pattern=r"s.*")

Alternatively, construct a complete variable dictionary and subselect if necessary:
vardict = spssaux.VariableDict()
svars = vardict.variables(pattern=r"s.*")

2. TO is not supported in the VariableDict parameter list (it probably should have been), but you can extract a TO equivalent via the range method.  E.g.,
vardict = spssaux.VariableDict()
vTov = vardict.range(start="income", end="age")

You can combine patterns and variable type filters in the range function as well.  Range is a bit more general than TO.

If you want to support these different conventions you might want to design your parameter list with different optional parameter names (and check for conflicts.  E'g',
def demo(names=None, pattern=None, to=None)

Then whichever form is used (and you could logically program for all three!), the other versions would have the value None.  For example, start with names if it isn't None; then subselect from that patterns, if not None, and then subselect further with to if it is not None.  If all are None, you could either raise an exception or take that to mean all variables.

3.  In the Python code, you have to quote the names so that Python knows that these are not Python variables, but you only need one set of quotes.  For example,
vardict = spssaux.VariableDict("a b c")
If you want to hide the Python syntax completely, making an extension command would be the way to go, since that would provide standard Statistics syntax for the program.  You might also be interested in the SPSSINC PROGRAM extension command.  It takes the name of a program to run and a list of arguments that are treated as regular Statistics tokens without the need to create an extension.  For example, you might have
SPSSINC PROGRAM myutilities.dosomething a b c.
which would invoke the Python function dosomething in module myutilities and pass it the listed parameters (as strings), which then don't need quoting.  This puts more of a burden on your code to check validity, though.

4.  You could use the triple-quoting convention for long lists and thus have only one set.  You would need to remove the newlines to make it like a single list by doing something like this
mylonglist=""" a b c
d e f"""
mylonglist = mylonglist.replace("\n", " ")

One other point.  The VariableDict variable names are case sensitive (except the regular expressions).  If you want to preserve the case insensitivity for users, you can specify  VariableDict(..., caseless=True).

This parameter was added in Statistics 19, but users of older versions can download spssaux.py from the SPSS Community (www.ibm.com/developerworks/spssdevcentral) to get this behavior.  The newer version is compatible with older versions of Statistics.
Reply | Threaded
Open this post in threaded view
|

Re: Python Functions Arguments

Albert-Jan Roskam
In reply to this post by Jignesh Sutar
Hi Jignesh,
 
If you intend to work with regexes, the following resource is a MUST: http://imgs.xkcd.com/comics/regular_expressions.png
 
;-)
 
Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



From: J Sutar <[hidden email]>
To: [hidden email]
Sent: Thu, January 13, 2011 2:45:03 PM
Subject: [SPSSX-L] Python Functions Arguments

Hello,
 
I'm experimenting with python and have a few questions on how to adapt the following macro to make it super efficient and user friendly...
 
BEGIN PROGRAM.
import spss, spssaux
def demo (vars):
    varList=vars.split()
    vardict= spssaux.VariableDict(varList)
    varStr=" ".join(varList)
    spss.Submit("""freq %(varStr)s. """ % locals())
 
demo(vars="gender bdate educ jobcat salary salbegin jobtime prevexp minority")
 
demo(vars="gender bdate educ jobcat"
"salary salbegin jobtime prevexp minority")
END PROGRAM.
 
1. The parameter vars in the current example accepts only a list of variables but is it possible also to input in this very same parameter (or any other work around) a way where I can specify a regular expression match? For this example, say, the variables starting with the letter "s" as an example?
 
2. I haven't looked into this into detail but I've seen other post about using the TO conversion also can this also be built in...so potentially there could be 3 different ways of specifying the arguments for the parameter vars?
 
3. Is it possible specify vars without having to enclose them in quotation marks?
 
4. Often the list of variables will be a long list so its necessary to allow them to wrap over multiple lines but in a python function calls this can only be done if each line is enclosed in quotation marks (see second macro call in example syntax above). Is there a better work around to this?
 
 
I ask most of these questions because I'm currently in the process of converting macros written in conventional SPSS macro syntax to python programs. In a conventional SPSS macro call I could easily list all the variables, across multiple lines not have to quote them. I'm trying to keep things as similar as possible because others and I have got into a habit of calling macros in a particular way...
 
Many thanks in advance...
 
Jignesh

Reply | Threaded
Open this post in threaded view
|

I need help

Neveen Hafez
In reply to this post by Jignesh Sutar
Hi everyone
i need help
i cant calculate difficulty index for my test quistions using spss
i am using v.16
and also PASW 'trail version'
so, i need steps to make it

 
 
 
 
WISH U HAPPY DAYS
 
Neveen Hafez


--- On Thu, 13/1/11, J Sutar <[hidden email]> wrote:

From: J Sutar <[hidden email]>
Subject: Python Functions Arguments
To: [hidden email]
Date: Thursday, 13 January, 2011, 15:45

Hello,
 
I'm experimenting with python and have a few questions on how to adapt the following macro to make it super efficient and user friendly...
 
BEGIN PROGRAM.
import spss, spssaux
def demo (vars):
    varList=vars.split()
    vardict= spssaux.VariableDict(varList)
    varStr=" ".join(varList)
    spss.Submit("""freq %(varStr)s. """ % locals())
 
demo(vars="gender bdate educ jobcat salary salbegin jobtime prevexp minority")
 
demo(vars="gender bdate educ jobcat"
"salary salbegin jobtime prevexp minority")
END PROGRAM.
 
1. The parameter vars in the current example accepts only a list of variables but is it possible also to input in this very same parameter (or any other work around) a way where I can specify a regular expression match? For this example, say, the variables starting with the letter "s" as an example?
 
2. I haven't looked into this into detail but I've seen other post about using the TO conversion also can this also be built in...so potentially there could be 3 different ways of specifying the arguments for the parameter vars?
 
3. Is it possible specify vars without having to enclose them in quotation marks?
 
4. Often the list of variables will be a long list so its necessary to allow them to wrap over multiple lines but in a python function calls this can only be done if each line is enclosed in quotation marks (see second macro call in example syntax above). Is there a better work around to this?
 
 
I ask most of these questions because I'm currently in the process of converting macros written in conventional SPSS macro syntax to python programs. In a conventional SPSS macro call I could easily list all the variables, across multiple lines not have to quote them. I'm trying to keep things as similar as possible because others and I have got into a habit of calling macros in a particular way...
 
Many thanks in advance...
 
Jignesh