Is there a more elegant way to do this? basic python/spss question

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

Is there a more elegant way to do this? basic python/spss question

David Marso
Administrator
I have written the following to resolve arbitrary variable lists into
expanded form when there is a TO in the specification.  This works but
somehow feels inelegant and a bit wonky.
Basically wish to use python as a preprocessor for macro invocations where
the macros require a !DO block. Yes I have good reasons for doing this. Wait
til Friday Fun
Thank you in advance.

BEGIN PROGRAM.
def ResolveVarList (VarList):
  import spssaux  
  x=VarList.split(" ")
  i=0
  for y in x:
    if y=='TO':
      x[i]=" ".join([item for item in spssaux.VariableDict().expand([x[i-1],
"TO", x[i+1]])])
      x[i-1]=""
      x[i+1] =""    
    i=i+1
    q=" ".join(x)
  print q
END PROGRAM.
 
BEGIN PROGRAM.
ResolveVarList (VarList='var1 TO var3 var4 var5 var7 TO var9')
END PROGRAM.

 var1 var2 var3  var4 var5  var7 var8 var9



-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Is there a more elegant way to do this? basic python/spss question

Jon Peck
expand can do all that for you.  The argument can be a plain string as here or a list, and there can be any number of "to"s.  ALL is also supported.  ALL would be the same as vars.variables except that the list is returned in file order as ALL would be interpreted natively in Statistics.

begin program.
import spssaux

vars = spssaux.VariableDict().expand("gender to salary prevexp to minority")
print vars
end program.

On Wed, Oct 25, 2017 at 8:56 AM, David Marso <[hidden email]> wrote:
I have written the following to resolve arbitrary variable lists into
expanded form when there is a TO in the specification.  This works but
somehow feels inelegant and a bit wonky.
Basically wish to use python as a preprocessor for macro invocations where
the macros require a !DO block. Yes I have good reasons for doing this. Wait
til Friday Fun
Thank you in advance.

BEGIN PROGRAM.
def ResolveVarList (VarList):
  import spssaux
  x=VarList.split(" ")
  i=0
  for y in x:
    if y=='TO':
      x[i]=" ".join([item for item in spssaux.VariableDict().expand([x[i-1],
"TO", x[i+1]])])
      x[i-1]=""
      x[i+1] =""
    i=i+1
    q=" ".join(x)
  print q
END PROGRAM.

BEGIN PROGRAM.
ResolveVarList (VarList='var1 TO var3 var4 var5 var7 TO var9')
END PROGRAM.

 var1 var2 var3  var4 var5  var7 var8 var9



-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
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



--
Jon K Peck
[hidden email]

===================== 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: Is there a more elegant way to do this? basic python/spss question

David Marso
Administrator
Cool Jon, Thanks.
The examples I've seen have either expand(var ,"TO",varn) or "ALL".
So my new code ends up very simply as:
I suppose I should have tried this first ;-)


BEGIN PROGRAM.
def ResolveVarList (VarList):
  import spssaux  
  q=" ".join(spssaux.VariableDict().expand(VarList))
  print q
END PROGRAM.

BEGIN PROGRAM.
ResolveVarList (VarList='var1 TO var3 var4 var5 var7 TO var9')
END PROGRAM.


Jon Peck wrote

> expand can do all that for you.  The argument can be a plain string as
> here
> or a list, and there can be any number of "to"s.  ALL is also supported.
> ALL would be the same as vars.variables except that the list is returned
> in
> file order as ALL would be interpreted natively in Statistics.
>
> begin program.
> import spssaux
>
> vars = spssaux.VariableDict().expand("gender to salary prevexp to
> minority")
> print vars
> end program.
>
> On Wed, Oct 25, 2017 at 8:56 AM, David Marso &lt;

> david.marso@

> &gt; wrote:
>
>> I have written the following to resolve arbitrary variable lists into
>> expanded form when there is a TO in the specification.  This works but
>> somehow feels inelegant and a bit wonky.
>> Basically wish to use python as a preprocessor for macro invocations
>> where
>> the macros require a !DO block. Yes I have good reasons for doing this.
>> Wait
>> til Friday Fun
>> Thank you in advance.
>>
>> BEGIN PROGRAM.
>> def ResolveVarList (VarList):
>>   import spssaux
>>   x=VarList.split(" ")
>>   i=0
>>   for y in x:
>>     if y=='TO':
>>       x[i]=" ".join([item for item in spssaux.VariableDict().expand(
>> [x[i-1],
>> "TO", x[i+1]])])
>>       x[i-1]=""
>>       x[i+1] =""
>>     i=i+1
>>     q=" ".join(x)
>>   print q
>> END PROGRAM.
>>
>> BEGIN PROGRAM.
>> ResolveVarList (VarList='var1 TO var3 var4 var5 var7 TO var9')
>> END PROGRAM.
>>
>>  var1 var2 var3  var4 var5  var7 var8 var9
>>
>>
>>
>> -----
>> Please reply to the list and not to my personal email.
>> Those desiring my consulting or training services please feel free to
>> email me.
>> ---
>> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos
>> ne forte conculcent eas pedibus suis."
>> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in
>> abyssum?"
>> --
>> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>>

> LISTSERV@.UGA

>  (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
>>
>
>
>
> --
> Jon K Peck

> jkpeck@

>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Is there a more elegant way to do this? basic python/spss question

Jon Peck
expand has the additional advantage that variable names are not case sensitive, unlike a list in the VariableDict creator.  If you specify a list in the VariableDict call, though, you can specify caseless=True going back to 2009.  There is this dissonance between the caseless behavior of Statistics and the case sensitive behavior generally in Python (or R).

BTW, it is better practice to put the imports at the top of the file rather than inside a function definition, although Python is smart enough to do the import only once.

On Wed, Oct 25, 2017 at 9:26 AM David Marso <[hidden email]> wrote:
Cool Jon, Thanks.
The examples I've seen have either expand(var ,"TO",varn) or "ALL".
So my new code ends up very simply as:
I suppose I should have tried this first ;-)


BEGIN PROGRAM.
def ResolveVarList (VarList):
  import spssaux
  q=" ".join(spssaux.VariableDict().expand(VarList))
  print q
END PROGRAM.

BEGIN PROGRAM.
ResolveVarList (VarList='var1 TO var3 var4 var5 var7 TO var9')
END PROGRAM.


Jon Peck wrote
> expand can do all that for you.  The argument can be a plain string as
> here
> or a list, and there can be any number of "to"s.  ALL is also supported.
> ALL would be the same as vars.variables except that the list is returned
> in
> file order as ALL would be interpreted natively in Statistics.
>
> begin program.
> import spssaux
>
> vars = spssaux.VariableDict().expand("gender to salary prevexp to
> minority")
> print vars
> end program.
>
> On Wed, Oct 25, 2017 at 8:56 AM, David Marso &lt;

> david.marso@

> &gt; wrote:
>
>> I have written the following to resolve arbitrary variable lists into
>> expanded form when there is a TO in the specification.  This works but
>> somehow feels inelegant and a bit wonky.
>> Basically wish to use python as a preprocessor for macro invocations
>> where
>> the macros require a !DO block. Yes I have good reasons for doing this.
>> Wait
>> til Friday Fun
>> Thank you in advance.
>>
>> BEGIN PROGRAM.
>> def ResolveVarList (VarList):
>>   import spssaux
>>   x=VarList.split(" ")
>>   i=0
>>   for y in x:
>>     if y=='TO':
>>       x[i]=" ".join([item for item in spssaux.VariableDict().expand(
>> [x[i-1],
>> "TO", x[i+1]])])
>>       x[i-1]=""
>>       x[i+1] =""
>>     i=i+1
>>     q=" ".join(x)
>>   print q
>> END PROGRAM.
>>
>> BEGIN PROGRAM.
>> ResolveVarList (VarList='var1 TO var3 var4 var5 var7 TO var9')
>> END PROGRAM.
>>
>>  var1 var2 var3  var4 var5  var7 var8 var9
>>
>>
>>
>> -----
>> Please reply to the list and not to my personal email.
>> Those desiring my consulting or training services please feel free to
>> email me.
>> ---
>> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos
>> ne forte conculcent eas pedibus suis."
>> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in
>> abyssum?"
>> --
>> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>>

> LISTSERV@.UGA

>  (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
>>
>
>
>
> --
> Jon K Peck

> jkpeck@

>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
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
--
Jon K Peck
[hidden email]

===================== 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