Python TypeError

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

Python TypeError

Mario Giesel

Hi python fans,

I tried an example from the "Programming with SPSS and Python" handbook but got an error message. Can you help me out?

 

Thanks a lot,

Mario

 

* ==========================================================.

DATA LIST FREE

/ id gender age incat region score1 score2 score3.

BEGIN DATA

1 0 35 3 10 85 76 63

END DATA.

BEGIN PROGRAM PYTHON.

import spss, spssaux

varDict=spssaux.VariableDict()

print "\n".join(varDict.Variables(pattern=r'.*\d$'))

END PROGRAM.

* ==========================================================.

 

Traceback (most recent call last):

File "<string>", line 3, in ?

TypeError: 'list' object is not callable


Mario Giesel
Munich, Germany
Reply | Threaded
Open this post in threaded view
|

Re: Python TypeError

Albert-Jan Roskam
Hi Mario!

I don't know why the example doesn't work. The code below produces the same output.

import spssaux
varDict=spssaux.VariableDict(pattern=r'.*\d$')
for v in varDict:
    print v.VariableName

Cheers!!
Albert-Jan


--- On Thu, 4/2/09, Mario Giesel <[hidden email]> wrote:

> From: Mario Giesel <[hidden email]>
> Subject: Python TypeError
> To: [hidden email]
> Date: Thursday, April 2, 2009, 8:48 AM
>
> Hi python fans,
> I tried an example from the "Programming with SPSS
> and Python" handbook but got an error message. Can you
> help me out?
>
> Thanks a lot,
> Mario
>
> *
> ==========================================================.
> DATA LIST FREE
> / id gender age incat region score1 score2 score3.
> BEGIN DATA
> 1 0 35 3 10 85 76 63
> END DATA.
> BEGIN PROGRAM PYTHON.
> import spss, spssaux
> varDict=spssaux.VariableDict()
> print
> "\n".join(varDict.Variables(pattern=r'.*\d$'))
>
> END PROGRAM.
> *
> ==========================================================.
>
> Traceback (most recent call last):
> File "<string>", line 3, in ?
> TypeError: 'list' object is not
> callable
>
>
>
>

=====================
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: Python TypeError

Peck, Jon
In reply to this post by Mario Giesel

A VariableDict object has a variables property that is a simple list of variable names.  (This is aliased also as Variables.)

The function that allows filtering by pattern, type etc, is variablesf

 

So the line causing the error below should be

print "\n".join(varDict.variablesf(pattern=r'.*\d$'))

That will call the variablesf method and return the filtered list of variables to the join function.  If this example is incorrect in the current edition of the book, we should fix that for the next edition.

 

HTH,

Jon Peck


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Mario Giesel
Sent: Thursday, April 02, 2009 12:49 AM
To: [hidden email]
Subject: [SPSSX-L] Python TypeError

 

Hi python fans,

I tried an example from the "Programming with SPSS and Python" handbook but got an error message. Can you help me out?

 

Thanks a lot,

Mario

 

* ==========================================================.

DATA LIST FREE

/ id gender age incat region score1 score2 score3.

BEGIN DATA

1 0 35 3 10 85 76 63

END DATA.

BEGIN PROGRAM PYTHON.

import spss, spssaux

varDict=spssaux.VariableDict()

print "\n".join(varDict.Variables(pattern=r'.*\d$'))

END PROGRAM.

* ==========================================================.

 

Traceback (most recent call last):

File "<string>", line 3, in ?

TypeError: 'list' object is not callable

 

Reply | Threaded
Open this post in threaded view
|

AW: Python TypeError

Mario Giesel
In reply to this post by Albert-Jan Roskam

Thanks to Albert-Jan and Jon - both approaches work!

 

* The program looks for all variables that end with a digit

and prints their names into the viewer..

 

* ==========================================================.
DATA LIST FREE
/ id gender age incat region score1 score2 score3.
BEGIN DATA
1 0 35 3 10 85 76 63
END DATA.
* ==========================================================.

 

* Jon Peck solution.

BEGIN PROGRAM PYTHON.
import spssaux
varDict=spssaux.VariableDict()
print "\n".join(varDict.variablesf(pattern=r'.*\d$'))

END PROGRAM.

 

* Albert-Jan solution.

BEGIN PROGRAM PYTHON.

import spssaux
varDict=spssaux.VariableDict(pattern=r'.*\d$')
for v in varDict:
print v.VariableName

END PROGRAM.

 


> From: Mario Giesel <[hidden email]>
> Subject: Python TypeError
> To: [hidden email]
> Date: Thursday, April 2, 2009, 8:48 AM
>
> Hi python fans,
> I tried an example from the "Programming with SPSS
> and Python" handbook but got an error message. Can you
> help me out?
>  
> Thanks a lot,
> Mario
>  
> *
> ==========================================================.
> DATA LIST FREE
> / id gender age incat region score1 score2 score3.
> BEGIN DATA
> 1 0 35 3 10 85 76 63
> END DATA.
> BEGIN PROGRAM PYTHON.
> import spss, spssaux
> varDict=spssaux.VariableDict()
> print
> "\n".join(varDict.Variables(pattern=r'.*\d$'))
>
> END PROGRAM.
> *
> ==========================================================.
>  
> Traceback (most recent call last):
> File "<string>", line 3, in ?
> TypeError: 'list' object is not
> callable
>
>
>
>     




Mario Giesel
Munich, Germany