Mimic SPSS 'to' keyword in Python

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

Mimic SPSS 'to' keyword in Python

Ruben Geert van den Berg
Dear all,
 
I've been trying to write Python code that mimics the 'to' keyword in SPSS. So if my variables are v1 through v20 and I enter [v3 v5 to v7 v9] I'd like it to return [v3 v5 v6 v7 v9]. Could anyone please out why my code is not working? It doesn't generate any error but simply returns my original varlist.
 
TIA!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

*Test data.

data list free/id.
begin data
end data.
numeric v1 to v20.
 
*Code.
 
begin program.
import spssaux
vardic=spssaux.VariableDict()
vars='v1 v3 to v5 v7 v9 to v11 v13 v15 tO v20'
vars=vars.split(" ")
while 'to' in [k.lower() for k in vars]=='True':
    for i in range(len(vars)):
        if vars[i].lower().find('to') != -1:
            start=vardic.VariableIndex(vars[i-1])
            end=vardic.VariableIndex(vars[i+1])
            vars[i-1:i+2]=[]
            l3=spssaux.GetVariableNamesList()[start:end+1]
            for j in range(len(l3)):
                vars.insert(i-1,l3[len(l3)-(j+1)])
print "\n".join(vars)
end program.
Reply | Threaded
Open this post in threaded view
|

Re: Mimic SPSS 'to' keyword in Python

Jon K Peck

First, note that the spssaux.VariableDict class has an expand method that works like TO in Statistics (with a few enhancements).  Look at that code for how the list is expanded.

But there are at least two problems in the code below.
First, in is lower precedence than ==, so the code is asking whether the list contents == "True" rather than what you want.
Second, True should not be in quotes: you mean the boolean value, not the string literal "True".
And, BTW, no need to use the  ==True portion anyway: x in [...]  will be True or False, and the loop will terminate accordingly

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 07/30/2010 07:15 AM
Subject: [SPSSX-L] Mimic SPSS 'to' keyword in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I've been trying to write Python code that mimics the 'to' keyword in SPSS. So if my variables are v1 through v20 and I enter [v3 v5 to v7 v9] I'd like it to return [v3 v5 v6 v7 v9]. Could anyone please out why my code is not working? It doesn't generate any error but simply returns my original varlist.

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com

*Test data.

data list free/id.
begin data
end data.
numeric v1 to v20.

*Code.

begin program.
import spssaux
vardic=spssaux.VariableDict()
vars='v1 v3 to v5 v7 v9 to v11 v13 v15 tO v20'
vars=vars.split(" ")
while 'to' in [k.lower() for k in vars]=='True':
   for i in range(len(vars)):
       if vars[i].lower().find('to') != -1:
           start=vardic.VariableIndex(vars[i-1])
           end=vardic.VariableIndex(vars[i+1])
           vars[i-1:i+2]=[]
           l3=spssaux.GetVariableNamesList()[start:end+1]
           for j in range(len(l3)):
               vars.insert(i-1,l3[len(l3)-(j+1)])
print "\n".join(vars)
end program.


Reply | Threaded
Open this post in threaded view
|

Re: Mimic SPSS 'to' keyword in Python

J. R. Carroll
My apologies for taking so long on this - spent a few minutes here and there through out the last two weeks. 

I am sure I reinvented the wheel here as I believe Jon was saying that this has already been developed (or some part of it).  All the same, this code is NOT optimized to run efficiently and/or I am never sure if I 'follow' Python standards.  Honest attempt - take it for what it is worth.  Ruben, I had some trouble following what you scripted so I didn't spend energies translating what you did, rather I just started over.  Maybe you can compare what I did against what you are trying to do to see where yours messed up?

If you have questions and/or suggestions let me know.  I am curious to know if it works.  If there is interest I will continue to add logic to it (adding flags to allow for leading zeros (in case you have 'v01, v02, ..., v11, v12, vN'), removing extra spaces, etc), but I doubt anyone will find use for this.

Hope you can make use of this Ruben (or at least see where your code got hung up),


Instructions:  Go the below link (my web space), copy the code to a python IDE and save it as "SPSS_Jpack.py".  You will need to save it in a place where python can LOAD/import it as a module. 

http://jrcresearch.net/spss/SPSS_Jpack.txt


I did NOT test this out in SPSS, but I imagine it would like like this:

begin program.
import SPSS_Jpack

sampleVars =  'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'
 
testVar = spssToRange(sampleVars)

print testVar
end program.

This should return the following result to you:

v1 v2 v3 v4 v5 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v23 v25 v26 EsT3G123 EsT3G124 EsT3G125 EsT3G126 EsT3G127 EsT3G128 EsT3G129 EsT3G130 EsT3G131 EsT3G132 EsT3G133 EsT3G134 EsT3G135 EsT3G136 EsT3G137 EsT3G138 EsT3G139 EsT3G140 EsT3G141 EsT3G142 EsT3G143 EsT3G144 EsT3G145 EsT3G146 EsT3G147 EsT3G148 EsT3G149 EsT3G150

I scripted it so it converts the "TO" or "To" or ... etc all to lowercase (as I assumed you were trying to program that logic into the script in the first place); notice the variability in the value passed to the function "'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'". 

NB:  I pasted the code as a text file and put it up on my web space because the code did not paste well into the email, and I was afraid it would not be well-received if it was garbled.  If you have problems coping/pasting the text file and saving it as a .py, let me know and I can send out the 'module' as an attachment on a personal communique.  Also, the code is not optimized and could use some cleaning up.  I know there are areas it could be simplified I just didn't have time to clean it up.  Feel free to send suggestions.

Ruben, my apologies if I missed the mark on what you were trying to accomplish. 

Enjoy,

J. R. Carroll
Grad. Student in Pre-Doc Psychology at CSUS
Research Assistant for Just About Everyone.
Email:  [hidden email]   -or-   [hidden email]
Phone:  (916) 628-4204


On Fri, Jul 30, 2010 at 6:41 AM, Jon K Peck <[hidden email]> wrote:

First, note that the spssaux.VariableDict class has an expand method that works like TO in Statistics (with a few enhancements).  Look at that code for how the list is expanded.

But there are at least two problems in the code below.
First, in is lower precedence than ==, so the code is asking whether the list contents == "True" rather than what you want.
Second, True should not be in quotes: you mean the boolean value, not the string literal "True".
And, BTW, no need to use the  ==True portion anyway: x in [...]  will be True or False, and the loop will terminate accordingly

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From:
Ruben van den Berg <[hidden email]>
To:
Date: 07/30/2010 07:15 AM
Subject: [SPSSX-L] Mimic SPSS 'to' keyword in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I've been trying to write Python code that mimics the 'to' keyword in SPSS. So if my variables are v1 through v20 and I enter [v3 v5 to v7 v9] I'd like it to return [v3 v5 v6 v7 v9]. Could anyone please out why my code is not working? It doesn't generate any error but simply returns my original varlist.

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com

*Test data.

data list free/id.
begin data
end data.
numeric v1 to v20.

*Code.

begin program.
import spssaux
vardic=spssaux.VariableDict()
vars='v1 v3 to v5 v7 v9 to v11 v13 v15 tO v20'
vars=vars.split(" ")
while 'to' in [k.lower() for k in vars]=='True':
   for i in range(len(vars)):
       if vars[i].lower().find('to') != -1:
           start=vardic.VariableIndex(vars[i-1])
           end=vardic.VariableIndex(vars[i+1])
           vars[i-1:i+2]=[]
           l3=spssaux.GetVariableNamesList()[start:end+1]
           for j in range(len(l3)):
               vars.insert(i-1,l3[len(l3)-(j+1)])
print "\n".join(vars)
end program.



Reply | Threaded
Open this post in threaded view
|

Re: Mimic SPSS 'to' keyword in Python

Ruben Geert van den Berg
Dear Justin,
 
Thanks a lot for your help! Thanks to Jon, my code eventually ran fine. However, as you and Jon pointed out, I invented a wheel that had already been invented. It was a lovely exercise however. 

Unfortunately, your code wouldn't run. For details, please see below.
 
Best, 

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

*Test data.

data list free/v_a v_b v_c v_d v_e v_f v_g v_h v_i v_j v_k v_l v_m v_n v_o.
begin data
end data.
 
*Varlist.
 
begin program.
import SPSS_Jpack
varlist=SPSS_Jpack.spssToRange(v_a to v_d, v_e, v_f, v_h to v_j, v_o)
print varlist
end program.
 
*Result (from viewer).
 

File "<string>", line 2

varlist=SPSS_Jpack.spssToRange(v_a to v_d, v_e, v_f, v_h to v_j, v_o)

                                    ^

SyntaxError: invalid syntax

 
 


Date: Wed, 11 Aug 2010 12:08:38 -0700
From: [hidden email]
Subject: Re: Mimic SPSS 'to' keyword in Python
To: [hidden email]

My apologies for taking so long on this - spent a few minutes here and there through out the last two weeks. 

I am sure I reinvented the wheel here as I believe Jon was saying that this has already been developed (or some part of it).  All the same, this code is NOT optimized to run efficiently and/or I am never sure if I 'follow' Python standards.  Honest attempt - take it for what it is worth.  Ruben, I had some trouble following what you scripted so I didn't spend energies translating what you did, rather I just started over.  Maybe you can compare what I did against what you are trying to do to see where yours messed up?

If you have questions and/or suggestions let me know.  I am curious to know if it works.  If there is interest I will continue to add logic to it (adding flags to allow for leading zeros (in case you have 'v01, v02, ..., v11, v12, vN'), removing extra spaces, etc), but I doubt anyone will find use for this.

Hope you can make use of this Ruben (or at least see where your code got hung up),


Instructions:  Go the below link (my web space), copy the code to a python IDE and save it as "SPSS_Jpack.py".  You will need to save it in a place where python can LOAD/import it as a module. 

http://jrcresearch.net/spss/SPSS_Jpack.txt


I did NOT test this out in SPSS, but I imagine it would like like this:


begin program.
import SPSS_Jpack

sampleVars =  'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'
 
testVar = spssToRange(sampleVars)

print testVar
end program.

This should return the following result to you:

v1 v2 v3 v4 v5 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v23 v25 v26 EsT3G123 EsT3G124 EsT3G125 EsT3G126 EsT3G127 EsT3G128 EsT3G129 EsT3G130 EsT3G131 EsT3G132 EsT3G133 EsT3G134 EsT3G135 EsT3G136 EsT3G137 EsT3G138 EsT3G139 EsT3G140 EsT3G141 EsT3G142 EsT3G143 EsT3G144 EsT3G145 EsT3G146 EsT3G147 EsT3G148 EsT3G149 EsT3G150

I scripted it so it converts the "TO" or "To" or ... etc all to lowercase (as I assumed you were trying to program that logic into the script in the first place); notice the variability in the value passed to the function "'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'". 

NB:  I pasted the code as a text file and put it up on my web space because the code did not paste well into the email, and I was afraid it would not be well-received if it was garbled.  If you have problems coping/pasting the text file and saving it as a .py, let me know and I can send out the 'module' as an attachment on a personal communique.  Also, the code is not optimized and could use some cleaning up.  I know there are areas it could be simplified I just didn't have time to clean it up.  Feel free to send suggestions.

Ruben, my apologies if I missed the mark on what you were trying to accomplish. 

Enjoy,

J. R. Carroll
Grad. Student in Pre-Doc Psychology at CSUS
Research Assistant for Just About Everyone.
Email:  [hidden email]   -or-   [hidden email]
Phone:  (916) 628-4204


On Fri, Jul 30, 2010 at 6:41 AM, Jon K Peck <[hidden email]> wrote:

First, note that the spssaux.VariableDict class has an expand method that works like TO in Statistics (with a few enhancements).  Look at that code for how the list is expanded.

But there are at least two problems in the code below.
First, in is lower precedence than ==, so the code is asking whether the list contents == "True" rather than what you want.
Second, True should not be in quotes: you mean the boolean value, not the string literal "True".
And, BTW, no need to use the  ==True portion anyway: x in [...]  will be True or False, and the loop will terminate accordingly

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From:
Ruben van den Berg <[hidden email]>
To:
Date: 07/30/2010 07:15 AM
Subject: [SPSSX-L] Mimic SPSS 'to' keyword in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I've been trying to write Python code that mimics the 'to' keyword in SPSS. So if my variables are v1 through v20 and I enter [v3 v5 to v7 v9] I'd like it to return [v3 v5 v6 v7 v9]. Could anyone please out why my code is not working? It doesn't generate any error but simply returns my original varlist.

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com

*Test data.

data list free/id.
begin data
end data.
numeric v1 to v20.

*Code.

begin program.
import spssaux
vardic=spssaux.VariableDict()
vars='v1 v3 to v5 v7 v9 to v11 v13 v15 tO v20'
vars=vars.split(" ")
while 'to' in [k.lower() for k in vars]=='True':
   for i in range(len(vars)):
       if vars[i].lower().find('to') != -1:
           start=vardic.VariableIndex(vars[i-1])
           end=vardic.VariableIndex(vars[i+1])
           vars[i-1:i+2]=[]
           l3=spssaux.GetVariableNamesList()[start:end+1]
           for j in range(len(l3)):
               vars.insert(i-1,l3[len(l3)-(j+1)])
print "\n".join(vars)
end program.



Reply | Threaded
Open this post in threaded view
|

Re: Mimic SPSS 'to' keyword in Python

Albert-Jan Roskam
Hmm, you're right, it's a fun exercise to reinvent this wheel ;-)
Here's what I just cobbled together. Not thoroughly tested. Might not work as intended with variable names with many numbers in them, eg. v12v32x3434 or something similar.
 
import re
sampleVars =  'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'
froms = re.findall("(\w+\d+) to ", sampleVars, re.IGNORECASE)
tos = re.findall(" to (\w+\d+)", sampleVars, re.IGNORECASE)
vars = [v for v in sampleVars.split() if v.lower() != "to"]
for from_, to in zip(froms, tos):
    prefix = re.search("(\D*\d*\D+)\d+", from_).group(1)
    begin = int(re.search("\d+$", from_).group(0))
    end = int(re.search("\d+$", to).group(0))
    vars.extend([prefix + str(i) for i in range(begin, end+1)])
allVars = sorted(list(set(vars)))
print allVars


 
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: Ruben van den Berg <[hidden email]>
To: [hidden email]
Sent: Thu, August 12, 2010 10:46:45 AM
Subject: Re: [SPSSX-L] Mimic SPSS 'to' keyword in Python

Dear Justin,
 
Thanks a lot for your help! Thanks to Jon, my code eventually ran fine. However, as you and Jon pointed out, I invented a wheel that had already been invented. It was a lovely exercise however. 

Unfortunately, your code wouldn't run. For details, please see below.
 
Best, 

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

*Test data.

data list free/v_a v_b v_c v_d v_e v_f v_g v_h v_i v_j v_k v_l v_m v_n v_o.
begin data
end data.
 
*Varlist.
 
begin program.
import SPSS_Jpack
varlist=SPSS_Jpack.spssToRange(v_a to v_d, v_e, v_f, v_h to v_j, v_o)
print varlist
end program.
 
*Result (from viewer).
 

File "<string>", line 2

varlist=SPSS_Jpack.spssToRange(v_a to v_d, v_e, v_f, v_h to v_j, v_o)

                                    ^

SyntaxError: invalid syntax

 
 


Date: Wed, 11 Aug 2010 12:08:38 -0700
From: [hidden email]
Subject: Re: Mimic SPSS 'to' keyword in Python
To: [hidden email]

My apologies for taking so long on this - spent a few minutes here and there through out the last two weeks. 

I am sure I reinvented the wheel here as I believe Jon was saying that this has already been developed (or some part of it).  All the same, this code is NOT optimized to run efficiently and/or I am never sure if I 'follow' Python standards.  Honest attempt - take it for what it is worth.  Ruben, I had some trouble following what you scripted so I didn't spend energies translating what you did, rather I just started over.  Maybe you can compare what I did against what you are trying to do to see where yours messed up?

If you have questions and/or suggestions let me know.  I am curious to know if it works.  If there is interest I will continue to add logic to it (adding flags to allow for leading zeros (in case you have 'v01, v02, ..., v11, v12, vN'), removing extra spaces, etc), but I doubt anyone will find use for this.

Hope you can make use of this Ruben (or at least see where your code got hung up),


Instructions:  Go the below link (my web space), copy the code to a python IDE and save it as "SPSS_Jpack.py".  You will need to save it in a place where python can LOAD/import it as a module. 

http://jrcresearch.net/spss/SPSS_Jpack.txt


I did NOT test this out in SPSS, but I imagine it would like like this:


begin program.
import SPSS_Jpack

sampleVars =  'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'
 
testVar = spssToRange(sampleVars)

print testVar
end program.

This should return the following result to you:

v1 v2 v3 v4 v5 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v23 v25 v26 EsT3G123 EsT3G124 EsT3G125 EsT3G126 EsT3G127 EsT3G128 EsT3G129 EsT3G130 EsT3G131 EsT3G132 EsT3G133 EsT3G134 EsT3G135 EsT3G136 EsT3G137 EsT3G138 EsT3G139 EsT3G140 EsT3G141 EsT3G142 EsT3G143 EsT3G144 EsT3G145 EsT3G146 EsT3G147 EsT3G148 EsT3G149 EsT3G150

I scripted it so it converts the "TO" or "To" or ... etc all to lowercase (as I assumed you were trying to program that logic into the script in the first place); notice the variability in the value passed to the function "'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'". 

NB:  I pasted the code as a text file and put it up on my web space because the code did not paste well into the email, and I was afraid it would not be well-received if it was garbled.  If you have problems coping/pasting the text file and saving it as a .py, let me know and I can send out the 'module' as an attachment on a personal communique.  Also, the code is not optimized and could use some cleaning up.  I know there are areas it could be simplified I just didn't have time to clean it up.  Feel free to send suggestions.

Ruben, my apologies if I missed the mark on what you were trying to accomplish. 

Enjoy,

J. R. Carroll
Grad. Student in Pre-Doc Psychology at CSUS
Research Assistant for Just About Everyone.
Email:  [hidden email]   -or-   [hidden email]
Phone:  (916) 628-4204


On Fri, Jul 30, 2010 at 6:41 AM, Jon K Peck <[hidden email]> wrote:

First, note that the spssaux.VariableDict class has an expand method that works like TO in Statistics (with a few enhancements).  Look at that code for how the list is expanded.

But there are at least two problems in the code below.
First, in is lower precedence than ==, so the code is asking whether the list contents == "True" rather than what you want.
Second, True should not be in quotes: you mean the boolean value, not the string literal "True".
And, BTW, no need to use the  ==True portion anyway: x in [...]  will be True or False, and the loop will terminate accordingly

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From:
Ruben van den Berg <[hidden email]>
To:
Date: 07/30/2010 07:15 AM
Subject: [SPSSX-L] Mimic SPSS 'to' keyword in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I've been trying to write Python code that mimics the 'to' keyword in SPSS. So if my variables are v1 through v20 and I enter [v3 v5 to v7 v9] I'd like it to return [v3 v5 v6 v7 v9]. Could anyone please out why my code is not working? It doesn't generate any error but simply returns my original varlist.

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com

*Test data.

data list free/id.
begin data
end data.
numeric v1 to v20.

*Code.

begin program.
import spssaux
vardic=spssaux.VariableDict()
vars='v1 v3 to v5 v7 v9 to v11 v13 v15 tO v20'
vars=vars.split(" ")
while 'to' in [k.lower() for k in vars]=='True':
   for i in range(len(vars)):
       if vars[i].lower().find('to') != -1:
           start=vardic.VariableIndex(vars[i-1])
           end=vardic.VariableIndex(vars[i+1])
           vars[i-1:i+2]=[]
           l3=spssaux.GetVariableNamesList()[start:end+1]
           for j in range(len(l3)):
               vars.insert(i-1,l3[len(l3)-(j+1)])
print "\n".join(vars)
end program.




Reply | Threaded
Open this post in threaded view
|

Re: Mimic SPSS 'to' keyword in Python

Ruben Geert van den Berg
Lovely! So that's the fourth time this wheel has been invented ;-) No, seriously, when I began the exercise I wasn't aware of the existence of the official version. I love to see all the RE statements in your code, I find those very difficult to write, I'll study them carefully :^D
 
I just retested my own code (below) and it seems to run exactly as I intended :-)
 
P.s. did you find the Python syntax for adding more than 50 files you promised me some weeks ago?

Best,

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

begin program.

import spssaux

def spssto(vars):

    vardic=spssaux.VariableDict()

    vars=vars.split(" ")

    while 'to' in [k.lower() for k in vars]:

        for i in range(len(vars)):

            if vars[i].lower().find('to') != -1:

                start=vardic.VariableIndex(vars[i-1])

                end=vardic.VariableIndex(vars[i+1])

                vars[i-1:i+2]=[]

                l3=spssaux.GetVariableNamesList()[start:end+1]

                for j in range(len(l3)):

                    vars.insert(i-1,l3[len(l3)-(j+1)])

    return vars

end program.

 
 

 

Date: Thu, 12 Aug 2010 04:20:02 -0700
From: [hidden email]
Subject: Re: [SPSSX-L] Mimic SPSS 'to' keyword in Python
To: [hidden email]; [hidden email]

Hmm, you're right, it's a fun exercise to reinvent this wheel ;-)
Here's what I just cobbled together. Not thoroughly tested. Might not work as intended with variable names with many numbers in them, eg. v12v32x3434 or something similar.
 
import re
sampleVars =  'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'
froms = re.findall("(\w+\d+) to ", sampleVars, re.IGNORECASE)
tos = re.findall(" to (\w+\d+)", sampleVars, re.IGNORECASE)
vars = [v for v in sampleVars.split() if v.lower() != "to"]
for from_, to in zip(froms, tos):
    prefix = re.search("(\D*\d*\D+)\d+", from_).group(1)
    begin = int(re.search("\d+$", from_).group(0))
    end = int(re.search("\d+$", to).group(0))
    vars.extend([prefix + str(i) for i in range(begin, end+1)])
allVars = sorted(list(set(vars)))
print allVars


 
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: Ruben van den Berg <[hidden email]>
To: [hidden email]
Sent: Thu, August 12, 2010 10:46:45 AM
Subject: Re: [SPSSX-L] Mimic SPSS 'to' keyword in Python

Dear Justin,
 
Thanks a lot for your help! Thanks to Jon, my code eventually ran fine. However, as you and Jon pointed out, I invented a wheel that had already been invented. It was a lovely exercise however. 

Unfortunately, your code wouldn't run. For details, please see below.
 
Best, 

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

*Test data.

data list free/v_a v_b v_c v_d v_e v_f v_g v_h v_i v_j v_k v_l v_m v_n v_o.
begin data
end data.
 
*Varlist.
 
begin program.
import SPSS_Jpack
varlist=SPSS_Jpack.spssToRange(v_a to v_d, v_e, v_f, v_h to v_j, v_o)
print varlist
end program.
 
*Result (from viewer).
 

File "<string>", line 2

varlist=SPSS_Jpack.spssToRange(v_a to v_d, v_e, v_f, v_h to v_j, v_o)

                                    ^

SyntaxError: invalid syntax

 
 


Date: Wed, 11 Aug 2010 12:08:38 -0700
From: [hidden email]
Subject: Re: Mimic SPSS 'to' keyword in Python
To: [hidden email]

My apologies for taking so long on this - spent a few minutes here and there through out the last two weeks. 

I am sure I reinvented the wheel here as I believe Jon was saying that this has already been developed (or some part of it).  All the same, this code is NOT optimized to run efficiently and/or I am never sure if I 'follow' Python standards.  Honest attempt - take it for what it is worth.  Ruben, I had some trouble following what you scripted so I didn't spend energies translating what you did, rather I just started over.  Maybe you can compare what I did against what you are trying to do to see where yours messed up?

If you have questions and/or suggestions let me know.  I am curious to know if it works.  If there is interest I will continue to add logic to it (adding flags to allow for leading zeros (in case you have 'v01, v02, ..., v11, v12, vN'), removing extra spaces, etc), but I doubt anyone will find use for this.

Hope you can make use of this Ruben (or at least see where your code got hung up),


Instructions:  Go the below link (my web space), copy the code to a python IDE and save it as "SPSS_Jpack.py".  You will need to save it in a place where python can LOAD/import it as a module. 

http://jrcresearch.net/spss/SPSS_Jpack.txt


I did NOT test this out in SPSS, but I imagine it would like like this:


begin program.
import SPSS_Jpack

sampleVars =  'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'
 
testVar = spssToRange(sampleVars)

print testVar
end program.

This should return the following result to you:

v1 v2 v3 v4 v5 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v23 v25 v26 EsT3G123 EsT3G124 EsT3G125 EsT3G126 EsT3G127 EsT3G128 EsT3G129 EsT3G130 EsT3G131 EsT3G132 EsT3G133 EsT3G134 EsT3G135 EsT3G136 EsT3G137 EsT3G138 EsT3G139 EsT3G140 EsT3G141 EsT3G142 EsT3G143 EsT3G144 EsT3G145 EsT3G146 EsT3G147 EsT3G148 EsT3G149 EsT3G150

I scripted it so it converts the "TO" or "To" or ... etc all to lowercase (as I assumed you were trying to program that logic into the script in the first place); notice the variability in the value passed to the function "'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'". 

NB:  I pasted the code as a text file and put it up on my web space because the code did not paste well into the email, and I was afraid it would not be well-received if it was garbled.  If you have problems coping/pasting the text file and saving it as a .py, let me know and I can send out the 'module' as an attachment on a personal communique.  Also, the code is not optimized and could use some cleaning up.  I know there are areas it could be simplified I just didn't have time to clean it up.  Feel free to send suggestions.

Ruben, my apologies if I missed the mark on what you were trying to accomplish. 

Enjoy,

J. R. Carroll
Grad. Student in Pre-Doc Psychology at CSUS
Research Assistant for Just About Everyone.
Email:  [hidden email]   -or-   [hidden email]
Phone:  (916) 628-4204


On Fri, Jul 30, 2010 at 6:41 AM, Jon K Peck <[hidden email]> wrote:

First, note that the spssaux.VariableDict class has an expand method that works like TO in Statistics (with a few enhancements).  Look at that code for how the list is expanded.

But there are at least two problems in the code below.
First, in is lower precedence than ==, so the code is asking whether the list contents == "True" rather than what you want.
Second, True should not be in quotes: you mean the boolean value, not the string literal "True".
And, BTW, no need to use the  ==True portion anyway: x in [...]  will be True or False, and the loop will terminate accordingly

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From:
Ruben van den Berg <[hidden email]>
To:
Date: 07/30/2010 07:15 AM
Subject: [SPSSX-L] Mimic SPSS 'to' keyword in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I've been trying to write Python code that mimics the 'to' keyword in SPSS. So if my variables are v1 through v20 and I enter [v3 v5 to v7 v9] I'd like it to return [v3 v5 v6 v7 v9]. Could anyone please out why my code is not working? It doesn't generate any error but simply returns my original varlist.

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com

*Test data.

data list free/id.
begin data
end data.
numeric v1 to v20.

*Code.

begin program.
import spssaux
vardic=spssaux.VariableDict()
vars='v1 v3 to v5 v7 v9 to v11 v13 v15 tO v20'
vars=vars.split(" ")
while 'to' in [k.lower() for k in vars]=='True':
   for i in range(len(vars)):
       if vars[i].lower().find('to') != -1:
           start=vardic.VariableIndex(vars[i-1])
           end=vardic.VariableIndex(vars[i+1])
           vars[i-1:i+2]=[]
           l3=spssaux.GetVariableNamesList()[start:end+1]
           for j in range(len(l3)):
               vars.insert(i-1,l3[len(l3)-(j+1)])
print "\n".join(vars)
end program.




Reply | Threaded
Open this post in threaded view
|

Re: Mimic SPSS 'to' keyword in Python

J. R. Carroll
Ruben,

  I totally understand that this is quickly becoming the "dead horse" of a thread, however due to my script 'not working' I just couldn't let it go! :P

I just ran what I wrote on my PC and it works fine - I am thinking that the problem might have been that the sys.pathI() was not pointing to the directory where the module file was at.

BEGIN program.

import SPSS_Jpack as jp, sys

for i in sys.path:
    print i


sv = 'v1 v2 v20 to v30'

returnJP = jp.spssToRange(sv)

print returnJP

END program.

The above code returns:

C:\Windows\system32\python25.zip
C:\Python25\Lib
C:\Python25\DLLs
C:\Python25\Lib\lib-tk
C:\Program Files\SPSSInc\Statistics17
C:\Python25
C:\Python25\lib\site-packages
C:\Python25\lib\site-packages\SpssClient170
C:\Python25\lib\site-packages\spss170
C:\Users\jc\Desktop
 
v1 v2 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30  

I have bolded the line to show where the module is saved (SPSS_Jpack is saved on my desktop).  Python does not normally explicitly know to look on your desktop (or other places on your computer) unless you tell it (it will look in the location on the HD from where Python was called, which is why you see "C:\Program Files\SPSSInc\Statistics17".  Likewise, you can either place the module in one of the available paths reported from sys.path() or you can append to the list via sys.path.append('[your directory]') - in my case I appended the desktop and put the document there just to confirm that everything is working.  

If you then run the above code (the Syntax I pasted) it should work fine. 
 
For me, within SPSS, it returned : " v1 v2 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 "

When you have a free moment I would appreciate it if you could try again - it would help me debug the script further, but I don't suspect that anything is wrong with it at this time; if you never get around to it - no biggie!

For whomever cares, I have put together the entire package (a sample SPSS-Syntax file, and the Python function/module):

http://jrcresearch.net/blog/wp-content/uploads/2010/08/samplesyntax.zip

Thanks,

J. R. Carroll
Grad. Student in Pre-Doc Psychology at CSUS
Research Assistant for Just About Everyone.
Email:  [hidden email]   -or-   [hidden email]
Phone:  (916) 628-4204


On Thu, Aug 12, 2010 at 4:52 AM, Ruben van den Berg <[hidden email]> wrote:
Lovely! So that's the fourth time this wheel has been invented ;-) No, seriously, when I began the exercise I wasn't aware of the existence of the official version. I love to see all the RE statements in your code, I find those very difficult to write, I'll study them carefully :^D
 
I just retested my own code (below) and it seems to run exactly as I intended :-)
 
P.s. did you find the Python syntax for adding more than 50 files you promised me some weeks ago?


Best,

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

begin program.

import spssaux

def spssto(vars):

    vardic=spssaux.VariableDict()

    vars=vars.split(" ")

    while 'to' in [k.lower() for k in vars]:

        for i in range(len(vars)):

            if vars[i].lower().find('to') != -1:

                start=vardic.VariableIndex(vars[i-1])

                end=vardic.VariableIndex(vars[i+1])

                vars[i-1:i+2]=[]

                l3=spssaux.GetVariableNamesList()[start:end+1]

                for j in range(len(l3)):

                    vars.insert(i-1,l3[len(l3)-(j+1)])

    return vars

end program.

 
 

 

Date: Thu, 12 Aug 2010 04:20:02 -0700
From: [hidden email]

Subject: Re: [SPSSX-L] Mimic SPSS 'to' keyword in Python
To: [hidden email]; [hidden email]


Hmm, you're right, it's a fun exercise to reinvent this wheel ;-)
Here's what I just cobbled together. Not thoroughly tested. Might not work as intended with variable names with many numbers in them, eg. v12v32x3434 or something similar.
 
import re
sampleVars =  'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'
froms = re.findall("(\w+\d+) to ", sampleVars, re.IGNORECASE)
tos = re.findall(" to (\w+\d+)", sampleVars, re.IGNORECASE)
vars = [v for v in sampleVars.split() if v.lower() != "to"]
for from_, to in zip(froms, tos):
    prefix = re.search("(\D*\d*\D+)\d+", from_).group(1)
    begin = int(re.search("\d+$", from_).group(0))
    end = int(re.search("\d+$", to).group(0))
    vars.extend([prefix + str(i) for i in range(begin, end+1)])
allVars = sorted(list(set(vars)))
print allVars


 
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: Ruben van den Berg <[hidden email]>
To: [hidden email]
Sent: Thu, August 12, 2010 10:46:45 AM
Subject: Re: [SPSSX-L] Mimic SPSS 'to' keyword in Python

Dear Justin,
 
Thanks a lot for your help! Thanks to Jon, my code eventually ran fine. However, as you and Jon pointed out, I invented a wheel that had already been invented. It was a lovely exercise however. 

Unfortunately, your code wouldn't run. For details, please see below.
 
Best, 

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

*Test data.

data list free/v_a v_b v_c v_d v_e v_f v_g v_h v_i v_j v_k v_l v_m v_n v_o.
begin data
end data.
 
*Varlist.
 
begin program.
import SPSS_Jpack
varlist=SPSS_Jpack.spssToRange(v_a to v_d, v_e, v_f, v_h to v_j, v_o)
print varlist
end program.
 
*Result (from viewer).
 

File "<string>", line 2

varlist=SPSS_Jpack.spssToRange(v_a to v_d, v_e, v_f, v_h to v_j, v_o)

                                    ^

SyntaxError: invalid syntax

 
 


Date: Wed, 11 Aug 2010 12:08:38 -0700
From: [hidden email]
Subject: Re: Mimic SPSS 'to' keyword in Python
To: [hidden email]

My apologies for taking so long on this - spent a few minutes here and there through out the last two weeks. 

I am sure I reinvented the wheel here as I believe Jon was saying that this has already been developed (or some part of it).  All the same, this code is NOT optimized to run efficiently and/or I am never sure if I 'follow' Python standards.  Honest attempt - take it for what it is worth.  Ruben, I had some trouble following what you scripted so I didn't spend energies translating what you did, rather I just started over.  Maybe you can compare what I did against what you are trying to do to see where yours messed up?

If you have questions and/or suggestions let me know.  I am curious to know if it works.  If there is interest I will continue to add logic to it (adding flags to allow for leading zeros (in case you have 'v01, v02, ..., v11, v12, vN'), removing extra spaces, etc), but I doubt anyone will find use for this.

Hope you can make use of this Ruben (or at least see where your code got hung up),


Instructions:  Go the below link (my web space), copy the code to a python IDE and save it as "SPSS_Jpack.py".  You will need to save it in a place where python can LOAD/import it as a module. 

http://jrcresearch.net/spss/SPSS_Jpack.txt


I did NOT test this out in SPSS, but I imagine it would like like this:


begin program.
import SPSS_Jpack

sampleVars =  'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'
 
testVar = spssToRange(sampleVars)

print testVar
end program.

This should return the following result to you:

v1 v2 v3 v4 v5 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v23 v25 v26 EsT3G123 EsT3G124 EsT3G125 EsT3G126 EsT3G127 EsT3G128 EsT3G129 EsT3G130 EsT3G131 EsT3G132 EsT3G133 EsT3G134 EsT3G135 EsT3G136 EsT3G137 EsT3G138 EsT3G139 EsT3G140 EsT3G141 EsT3G142 EsT3G143 EsT3G144 EsT3G145 EsT3G146 EsT3G147 EsT3G148 EsT3G149 EsT3G150

I scripted it so it converts the "TO" or "To" or ... etc all to lowercase (as I assumed you were trying to program that logic into the script in the first place); notice the variability in the value passed to the function "'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'". 

NB:  I pasted the code as a text file and put it up on my web space because the code did not paste well into the email, and I was afraid it would not be well-received if it was garbled.  If you have problems coping/pasting the text file and saving it as a .py, let me know and I can send out the 'module' as an attachment on a personal communique.  Also, the code is not optimized and could use some cleaning up.  I know there are areas it could be simplified I just didn't have time to clean it up.  Feel free to send suggestions.

Ruben, my apologies if I missed the mark on what you were trying to accomplish. 

Enjoy,

J. R. Carroll
Grad. Student in Pre-Doc Psychology at CSUS
Research Assistant for Just About Everyone.
Email:  [hidden email]   -or-   [hidden email]
Phone:  (916) 628-4204


On Fri, Jul 30, 2010 at 6:41 AM, Jon K Peck <[hidden email]> wrote:

First, note that the spssaux.VariableDict class has an expand method that works like TO in Statistics (with a few enhancements).  Look at that code for how the list is expanded.

But there are at least two problems in the code below.
First, in is lower precedence than ==, so the code is asking whether the list contents == "True" rather than what you want.
Second, True should not be in quotes: you mean the boolean value, not the string literal "True".
And, BTW, no need to use the  ==True portion anyway: x in [...]  will be True or False, and the loop will terminate accordingly

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From:
Ruben van den Berg <[hidden email]>
To:
Date: 07/30/2010 07:15 AM
Subject: [SPSSX-L] Mimic SPSS 'to' keyword in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I've been trying to write Python code that mimics the 'to' keyword in SPSS. So if my variables are v1 through v20 and I enter [v3 v5 to v7 v9] I'd like it to return [v3 v5 v6 v7 v9]. Could anyone please out why my code is not working? It doesn't generate any error but simply returns my original varlist.

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com

*Test data.

data list free/id.
begin data
end data.
numeric v1 to v20.

*Code.

begin program.
import spssaux
vardic=spssaux.VariableDict()
vars='v1 v3 to v5 v7 v9 to v11 v13 v15 tO v20'
vars=vars.split(" ")
while 'to' in [k.lower() for k in vars]=='True':
   for i in range(len(vars)):
       if vars[i].lower().find('to') != -1:
           start=vardic.VariableIndex(vars[i-1])
           end=vardic.VariableIndex(vars[i+1])
           vars[i-1:i+2]=[]
           l3=spssaux.GetVariableNamesList()[start:end+1]
           for j in range(len(l3)):
               vars.insert(i-1,l3[len(l3)-(j+1)])
print "\n".join(vars)
end program.





Reply | Threaded
Open this post in threaded view
|

Re: Mimic SPSS 'to' keyword in Python

Ruben Geert van den Berg
Dear Justin,
 
I actually think this was a pretty cool thread.
 
Thanks for the explanation. I was able to replicate what you wrote below, but I'm afraid you may have missed the purpose of the code. It is supposed to replace 'to' with the variables located between the variables surrounding 'to'. It seems that if the variables in my datafile are [v1, v2, v20, TinkyWinky, v30] your code will not render [v1, v2, v20, TinkyWinky, v30] but [v1, v2, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30], which is not the desired output.
 
Well, thanks for the lovely (although not overly productive ;-)) discussion!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com



 

Date: Sun, 15 Aug 2010 19:49:38 -0700
Subject: Re: Mimic SPSS 'to' keyword in Python
From: [hidden email]
To: [hidden email]
CC: [hidden email]

Ruben,

  I totally understand that this is quickly becoming the "dead horse" of a thread, however due to my script 'not working' I just couldn't let it go! :P

I just ran what I wrote on my PC and it works fine - I am thinking that the problem might have been that the sys.pathI() was not pointing to the directory where the module file was at.

BEGIN program.

import SPSS_Jpack as jp, sys

for i in sys.path:
    print i


sv = 'v1 v2 v20 to v30'

returnJP = jp.spssToRange(sv)

print returnJP

END program.

The above code returns:

C:\Windows\system32\python25.zip
C:\Python25\Lib
C:\Python25\DLLs
C:\Python25\Lib\lib-tk
C:\Program Files\SPSSInc\Statistics17
C:\Python25
C:\Python25\lib\site-packages
C:\Python25\lib\site-packages\SpssClient170
C:\Python25\lib\site-packages\spss170
C:\Users\jc\Desktop
 
v1 v2 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30  

I have bolded the line to show where the module is saved (SPSS_Jpack is saved on my desktop).  Python does not normally explicitly know to look on your desktop (or other places on your computer) unless you tell it (it will look in the location on the HD from where Python was called, which is why you see "C:\Program Files\SPSSInc\Statistics17".  Likewise, you can either place the module in one of the available paths reported from sys.path() or you can append to the list via sys.path.append('[your directory]') - in my case I appended the desktop and put the document there just to confirm that everything is working.  

If you then run the above code (the Syntax I pasted) it should work fine. 
 
For me, within SPSS, it returned : " v1 v2 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 "

When you have a free moment I would appreciate it if you could try again - it would help me debug the script further, but I don't suspect that anything is wrong with it at this time; if you never get around to it - no biggie!

For whomever cares, I have put together the entire package (a sample SPSS-Syntax file, and the Python function/module):

http://jrcresearch.net/blog/wp-content/uploads/2010/08/samplesyntax.zip

Thanks,

J. R. Carroll
Grad. Student in Pre-Doc Psychology at CSUS
Research Assistant for Just About Everyone.
Email:  [hidden email]   -or-   [hidden email]
Phone:  (916) 628-4204


On Thu, Aug 12, 2010 at 4:52 AM, Ruben van den Berg <[hidden email]> wrote:
Lovely! So that's the fourth time this wheel has been invented ;-) No, seriously, when I began the exercise I wasn't aware of the existence of the official version. I love to see all the RE statements in your code, I find those very difficult to write, I'll study them carefully :^D
 
I just retested my own code (below) and it seems to run exactly as I intended :-)
 
P.s. did you find the Python syntax for adding more than 50 files you promised me some weeks ago?


Best,

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

begin program.

import spssaux

def spssto(vars):

    vardic=spssaux.VariableDict()

    vars=vars.split(" ")

    while 'to' in [k.lower() for k in vars]:

        for i in range(len(vars)):

            if vars[i].lower().find('to') != -1:

                start=vardic.VariableIndex(vars[i-1])

                end=vardic.VariableIndex(vars[i+1])

                vars[i-1:i+2]=[]

                l3=spssaux.GetVariableNamesList()[start:end+1]

                for j in range(len(l3)):

                    vars.insert(i-1,l3[len(l3)-(j+1)])

    return vars

end program.

 
 

 

Date: Thu, 12 Aug 2010 04:20:02 -0700
From: [hidden email]

Subject: Re: [SPSSX-L] Mimic SPSS 'to' keyword in Python
To: [hidden email]; [hidden email]


Hmm, you're right, it's a fun exercise to reinvent this wheel ;-)
Here's what I just cobbled together. Not thoroughly tested. Might not work as intended with variable names with many numbers in them, eg. v12v32x3434 or something similar.
 
import re
sampleVars =  'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'
froms = re.findall("(\w+\d+) to ", sampleVars, re.IGNORECASE)
tos = re.findall(" to (\w+\d+)", sampleVars, re.IGNORECASE)
vars = [v for v in sampleVars.split() if v.lower() != "to"]
for from_, to in zip(froms, tos):
    prefix = re.search("(\D*\d*\D+)\d+", from_).group(1)
    begin = int(re.search("\d+$", from_).group(0))
    end = int(re.search("\d+$", to).group(0))
    vars.extend([prefix + str(i) for i in range(begin, end+1)])
allVars = sorted(list(set(vars)))
print allVars


 
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: Ruben van den Berg <[hidden email]>
To: [hidden email]
Sent: Thu, August 12, 2010 10:46:45 AM
Subject: Re: [SPSSX-L] Mimic SPSS 'to' keyword in Python

Dear Justin,
 
Thanks a lot for your help! Thanks to Jon, my code eventually ran fine. However, as you and Jon pointed out, I invented a wheel that had already been invented. It was a lovely exercise however. 

Unfortunately, your code wouldn't run. For details, please see below.
 
Best, 

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

*Test data.

data list free/v_a v_b v_c v_d v_e v_f v_g v_h v_i v_j v_k v_l v_m v_n v_o.
begin data
end data.
 
*Varlist.
 
begin program.
import SPSS_Jpack
varlist=SPSS_Jpack.spssToRange(v_a to v_d, v_e, v_f, v_h to v_j, v_o)
print varlist
end program.
 
*Result (from viewer).
 

File "<string>", line 2

varlist=SPSS_Jpack.spssToRange(v_a to v_d, v_e, v_f, v_h to v_j, v_o)

                                    ^

SyntaxError: invalid syntax

 
 


Date: Wed, 11 Aug 2010 12:08:38 -0700
From: [hidden email]
Subject: Re: Mimic SPSS 'to' keyword in Python
To: [hidden email]

My apologies for taking so long on this - spent a few minutes here and there through out the last two weeks. 

I am sure I reinvented the wheel here as I believe Jon was saying that this has already been developed (or some part of it).  All the same, this code is NOT optimized to run efficiently and/or I am never sure if I 'follow' Python standards.  Honest attempt - take it for what it is worth.  Ruben, I had some trouble following what you scripted so I didn't spend energies translating what you did, rather I just started over.  Maybe you can compare what I did against what you are trying to do to see where yours messed up?

If you have questions and/or suggestions let me know.  I am curious to know if it works.  If there is interest I will continue to add logic to it (adding flags to allow for leading zeros (in case you have 'v01, v02, ..., v11, v12, vN'), removing extra spaces, etc), but I doubt anyone will find use for this.

Hope you can make use of this Ruben (or at least see where your code got hung up),


Instructions:  Go the below link (my web space), copy the code to a python IDE and save it as "SPSS_Jpack.py".  You will need to save it in a place where python can LOAD/import it as a module. 

http://jrcresearch.net/spss/SPSS_Jpack.txt


I did NOT test this out in SPSS, but I imagine it would like like this:


begin program.
import SPSS_Jpack

sampleVars =  'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'
 
testVar = spssToRange(sampleVars)

print testVar
end program.

This should return the following result to you:

v1 v2 v3 v4 v5 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v23 v25 v26 EsT3G123 EsT3G124 EsT3G125 EsT3G126 EsT3G127 EsT3G128 EsT3G129 EsT3G130 EsT3G131 EsT3G132 EsT3G133 EsT3G134 EsT3G135 EsT3G136 EsT3G137 EsT3G138 EsT3G139 EsT3G140 EsT3G141 EsT3G142 EsT3G143 EsT3G144 EsT3G145 EsT3G146 EsT3G147 EsT3G148 EsT3G149 EsT3G150

I scripted it so it converts the "TO" or "To" or ... etc all to lowercase (as I assumed you were trying to program that logic into the script in the first place); notice the variability in the value passed to the function "'v1 to v5 v10 TO v20 v23 v25 v26 EsT3G123 tO EsT3G150'". 

NB:  I pasted the code as a text file and put it up on my web space because the code did not paste well into the email, and I was afraid it would not be well-received if it was garbled.  If you have problems coping/pasting the text file and saving it as a .py, let me know and I can send out the 'module' as an attachment on a personal communique.  Also, the code is not optimized and could use some cleaning up.  I know there are areas it could be simplified I just didn't have time to clean it up.  Feel free to send suggestions.

Ruben, my apologies if I missed the mark on what you were trying to accomplish. 

Enjoy,

J. R. Carroll
Grad. Student in Pre-Doc Psychology at CSUS
Research Assistant for Just About Everyone.
Email:  [hidden email]   -or-   [hidden email]
Phone:  (916) 628-4204


On Fri, Jul 30, 2010 at 6:41 AM, Jon K Peck <[hidden email]> wrote:

First, note that the spssaux.VariableDict class has an expand method that works like TO in Statistics (with a few enhancements).  Look at that code for how the list is expanded.

But there are at least two problems in the code below.
First, in is lower precedence than ==, so the code is asking whether the list contents == "True" rather than what you want.
Second, True should not be in quotes: you mean the boolean value, not the string literal "True".
And, BTW, no need to use the  ==True portion anyway: x in [...]  will be True or False, and the loop will terminate accordingly

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From:
Ruben van den Berg <[hidden email]>
To:
Date: 07/30/2010 07:15 AM
Subject: [SPSSX-L] Mimic SPSS 'to' keyword in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I've been trying to write Python code that mimics the 'to' keyword in SPSS. So if my variables are v1 through v20 and I enter [v3 v5 to v7 v9] I'd like it to return [v3 v5 v6 v7 v9]. Could anyone please out why my code is not working? It doesn't generate any error but simply returns my original varlist.

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com

*Test data.

data list free/id.
begin data
end data.
numeric v1 to v20.

*Code.

begin program.
import spssaux
vardic=spssaux.VariableDict()
vars='v1 v3 to v5 v7 v9 to v11 v13 v15 tO v20'
vars=vars.split(" ")
while 'to' in [k.lower() for k in vars]=='True':
   for i in range(len(vars)):
       if vars[i].lower().find('to') != -1:
           start=vardic.VariableIndex(vars[i-1])
           end=vardic.VariableIndex(vars[i+1])
           vars[i-1:i+2]=[]
           l3=spssaux.GetVariableNamesList()[start:end+1]
           for j in range(len(l3)):
               vars.insert(i-1,l3[len(l3)-(j+1)])
print "\n".join(vars)
end program.