Python function doesn't work

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

Python function doesn't work

Ruben Geert van den Berg
Dear all,
 
I tried to write a Python function in order to carry out a program Jon Peck made for me. The program -obviously- works fine but as soon as I try to use it as a function it doesn't function anymore. Could anyone please point out what I'm doing wrong?
 
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

*Function, doesn't work.
 
begin program.
def NoCatFil (maxval):
    import spss, spssaux, spssdata
    vardic=spssaux.VariableDict()
    varcount=len(vardic.variables)
    valuesets=[set() for i in range(varcount)]
    curs=spssdata.Spssdata(convertUserMissing=False)
    for case in curs:
        for i in range(varcount):
            if len (valuesets[i]) <=maxval:
                valuesets[i].add(case[i])
    curs.CClose
    varlistout=[]
    for i in range(varcount):
        if len(valuesets[i]) <=maxval:
            varlistout.append(vardic.variables[i])
end program.
 
begin program.
NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)
end program.
 
*Original, works.

begin program.
import spss, spssaux, spssdata
maxval=4
vardic=spssaux.VariableDict()
varcount=len(vardic.variables)
valuesets=[set() for i in range(varcount)]
curs=spssdata.Spssdata(convertUserMissing=False)
for case in curs:
    for i in range(varcount):
        if len (valuesets[i]) <=maxval:
            valuesets[i].add(case[i])
curs.CClose
varlistout=[]
for i in range(varcount):
    if len(valuesets[i]) <=maxval:
        varlistout.append(vardic.variables[i])
if varlistout:
    print "\n".join(varlistout)
end program.


New Windows 7: Simplify what you do everyday. Find the right PC for you.
Reply | Threaded
Open this post in threaded view
|

Re: Python function doesn't work

Albert-Jan Roskam
Hi Ruben,
 
What traceback are you getting?
Two minor notes: it's unusual to to put the module imports inside a function; also, function names usually start with a lower case letter (upper case is used for classes).

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 9:44 AM

Dear all,
 
I tried to write a Python function in order to carry out a program Jon Peck made for me. The program -obviously- works fine but as soon as I try to use it as a function it doesn't function anymore. Could anyone please point out what I'm doing wrong?
 
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

*Function, doesn't work.
 
begin program.
def NoCatFil (maxval):
    import spss, spssaux, spssdata
    vardic=spssaux.VariableDict()
    varcount=len(vardic.variables)
    valuesets=[set() for i in range(varcount)]
    curs=spssdata.Spssdata(convertUserMissing=False)
    for case in curs:
        for i in range(varcount):
            if len (valuesets[i]) <=maxval:
                valuesets[i].add(case[i])
    curs.CClose
    varlistout=[]
    for i in range(varcount):
        if len(valuesets[i]) <=maxval:
            varlistout.append(vardic.variables[i])
end program.
 
begin program.
NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)
end program.
 
*Original, works.

begin program.
import spss, spssaux, spssdata
maxval=4
vardic=spssaux.VariableDict()
varcount=len(vardic.variables)
valuesets=[set() for i in range(varcount)]
curs=spssdata.Spssdata(convertUserMissing=False)
for case in curs:
    for i in range(varcount):
        if len (valuesets[i]) <=maxval:
            valuesets[i].add(case[i])
curs.CClose
varlistout=[]
for i in range(varcount):
    if len(valuesets[i]) <=maxval:
        varlistout.append(vardic.variables[i])
if varlistout:
    print "\n".join(varlistout)
end program.


New Windows 7: Simplify what you do everyday. Find the right PC for you.

Reply | Threaded
Open this post in threaded view
|

Re: Python function doesn't work

Ruben Geert van den Berg
Dear Albert-Jan,
 
Thanks for your two suggestions, it won't happen again! The traceback I keep getting is:
 

Traceback (most recent call last):

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

NameError: name 'varlistout' is not defined

 
CHEERS!!
 
Ruben

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: Mon, 19 Jul 2010 01:35:29 -0700
From: [hidden email]
Subject: Re: Python function doesn't work
To: [hidden email]

Hi Ruben,
 
What traceback are you getting?
Two minor notes: it's unusual to to put the module imports inside a function; also, function names usually start with a lower case letter (upper case is used for classes).

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 9:44 AM

Dear all,
 
I tried to write a Python function in order to carry out a program Jon Peck made for me. The program -obviously- works fine but as soon as I try to use it as a function it doesn't function anymore. Could anyone please point out what I'm doing wrong?
 
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

*Function, doesn't work.
 
begin program.
def NoCatFil (maxval):
    import spss, spssaux, spssdata
    vardic=spssaux.VariableDict()
    varcount=len(vardic.variables)
    valuesets=[set() for i in range(varcount)]
    curs=spssdata.Spssdata(convertUserMissing=False)
    for case in curs:
        for i in range(varcount):
            if len (valuesets[i]) <=maxval:
                valuesets[i].add(case[i])
    curs.CClose
    varlistout=[]
    for i in range(varcount):
        if len(valuesets[i]) <=maxval:
            varlistout.append(vardic.variables[i])
end program.
 
begin program.
NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)
end program.
 
*Original, works.

begin program.
import spss, spssaux, spssdata
maxval=4
vardic=spssaux.VariableDict()
varcount=len(vardic.variables)
valuesets=[set() for i in range(varcount)]
curs=spssdata.Spssdata(convertUserMissing=False)
for case in curs:
    for i in range(varcount):
        if len (valuesets[i]) <=maxval:
            valuesets[i].add(case[i])
curs.CClose
varlistout=[]
for i in range(varcount):
    if len(valuesets[i]) <=maxval:
        varlistout.append(vardic.variables[i])
if varlistout:
    print "\n".join(varlistout)
end program.


New Windows 7: Simplify what you do everyday. Find the right PC for you.



New Windows 7: Find the right PC for you. Learn more.
Reply | Threaded
Open this post in threaded view
|

Re: Python function doesn't work

Albert-Jan Roskam
Ah, variable 'varlistout' is defined within the scope of the function. It doesn't exist outside the function. So the second program block should be inside the function. You could end the function with: return varlistout (with one indentation). Then you call the function:
varlistout  = NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: Re: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 10:41 AM

Dear Albert-Jan,
 
Thanks for your two suggestions, it won't happen again! The traceback I keep getting is:
 

Traceback (most recent call last):

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

NameError: name 'varlistout' is not defined

 
CHEERS!!
 
Ruben

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: Mon, 19 Jul 2010 01:35:29 -0700
From: [hidden email]
Subject: Re: Python function doesn't work
To: [hidden email]

Hi Ruben,
 
What traceback are you getting?
Two minor notes: it's unusual to to put the module imports inside a function; also, function names usually start with a lower case letter (upper case is used for classes).

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 9:44 AM

Dear all,
 
I tried to write a Python function in order to carry out a program Jon Peck made for me. The program -obviously- works fine but as soon as I try to use it as a function it doesn't function anymore. Could anyone please point out what I'm doing wrong?
 
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

*Function, doesn't work.
 
begin program.
def NoCatFil (maxval):
    import spss, spssaux, spssdata
    vardic=spssaux.VariableDict()
    varcount=len(vardic.variables)
    valuesets=[set() for i in range(varcount)]
    curs=spssdata.Spssdata(convertUserMissing=False)
    for case in curs:
        for i in range(varcount):
            if len (valuesets[i]) <=maxval:
                valuesets[i].add(case[i])
    curs.CClose
    varlistout=[]
    for i in range(varcount):
        if len(valuesets[i]) <=maxval:
            varlistout.append(vardic.variables[i])
end program.
 
begin program.
NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)
end program.
 
*Original, works.

begin program.
import spss, spssaux, spssdata
maxval=4
vardic=spssaux.VariableDict()
varcount=len(vardic.variables)
valuesets=[set() for i in range(varcount)]
curs=spssdata.Spssdata(convertUserMissing=False)
for case in curs:
    for i in range(varcount):
        if len (valuesets[i]) <=maxval:
            valuesets[i].add(case[i])
curs.CClose
varlistout=[]
for i in range(varcount):
    if len(valuesets[i]) <=maxval:
        varlistout.append(vardic.variables[i])
if varlistout:
    print "\n".join(varlistout)
end program.


New Windows 7: Simplify what you do everyday. Find the right PC for you.



New Windows 7: Find the right PC for you. Learn more.

Reply | Threaded
Open this post in threaded view
|

SOLVED: Python function doesn't work

Ruben Geert van den Berg
Oh, is this because 'varlistout' is local (not global) to the function definition? Anyway, the syntax below works exactly as I'd like it to!
 
Thanks a lot, it's very much appreciated!
 
CHEERS!!
 
Ruben

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 spss, spssaux, spssdata
def nocatfilt(maxval):
    vardict = spssaux.VariableDict()
    varcount = len(vardict.variables)
    valuesets = [set() for i in range(varcount)]
    curs = spssdata.Spssdata(vardict.variables)
    for case in curs:
        for i in range(varcount):
            if len(valuesets[i]) <= maxval+1:
                valuesets[i].add(case[i])
    curs.CClose()
    varlistout = []
    for i in range(varcount):
        if len(valuesets[i]) <= maxval:
            varlistout.append(vardict.variables[i])
    return varlistout
end program.
 
begin program.
varlistout=nocatfilt(2)
print "\n".join(varlistout)
end program.



 

Date: Mon, 19 Jul 2010 02:05:35 -0700
From: [hidden email]
Subject: Re: Python function doesn't work
To: [hidden email]

Ah, variable 'varlistout' is defined within the scope of the function. It doesn't exist outside the function. So the second program block should be inside the function. You could end the function with: return varlistout (with one indentation). Then you call the function:
varlistout  = NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: Re: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 10:41 AM

Dear Albert-Jan,
 
Thanks for your two suggestions, it won't happen again! The traceback I keep getting is:
 

Traceback (most recent call last):

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

NameError: name 'varlistout' is not defined

 
CHEERS!!
 
Ruben

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: Mon, 19 Jul 2010 01:35:29 -0700
From: [hidden email]
Subject: Re: Python function doesn't work
To: [hidden email]

Hi Ruben,
 
What traceback are you getting?
Two minor notes: it's unusual to to put the module imports inside a function; also, function names usually start with a lower case letter (upper case is used for classes).

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 9:44 AM

Dear all,
 
I tried to write a Python function in order to carry out a program Jon Peck made for me. The program -obviously- works fine but as soon as I try to use it as a function it doesn't function anymore. Could anyone please point out what I'm doing wrong?
 
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

*Function, doesn't work.
 
begin program.
def NoCatFil (maxval):
    import spss, spssaux, spssdata
    vardic=spssaux.VariableDict()
    varcount=len(vardic.variables)
    valuesets=[set() for i in range(varcount)]
    curs=spssdata.Spssdata(convertUserMissing=False)
    for case in curs:
        for i in range(varcount):
            if len (valuesets[i]) <=maxval:
                valuesets[i].add(case[i])
    curs.CClose
    varlistout=[]
    for i in range(varcount):
        if len(valuesets[i]) <=maxval:
            varlistout.append(vardic.variables[i])
end program.
 
begin program.
NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)
end program.
 
*Original, works.

begin program.
import spss, spssaux, spssdata
maxval=4
vardic=spssaux.VariableDict()
varcount=len(vardic.variables)
valuesets=[set() for i in range(varcount)]
curs=spssdata.Spssdata(convertUserMissing=False)
for case in curs:
    for i in range(varcount):
        if len (valuesets[i]) <=maxval:
            valuesets[i].add(case[i])
curs.CClose
varlistout=[]
for i in range(varcount):
    if len(valuesets[i]) <=maxval:
        varlistout.append(vardic.variables[i])
if varlistout:
    print "\n".join(varlistout)
end program.


New Windows 7: Simplify what you do everyday. Find the right PC for you.



New Windows 7: Find the right PC for you. Learn more.



Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|

SOLVED(2): Python function doesn't work

Ruben Geert van den Berg
In reply to this post by Albert-Jan Roskam
I'm sorry for posting excessively, but I just received an alternative solution from the Python forum, which is to define varlistout as a global variable. This works like a charm as well!
 
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 spss, spssaux, spssdata
def nocatfilt(maxval):
    global varlistout
    vardict = spssaux.VariableDict()
    varcount = len(vardict.variables)
    valuesets = [set() for i in range(varcount)]
    curs = spssdata.Spssdata(vardict.variables)
    for case in curs:
        for i in range(varcount):
            if len(valuesets[i]) <= maxval+1:
                valuesets[i].add(case[i])
    curs.CClose()
    varlistout = []
    for i in range(varcount):
        if len(valuesets[i]) <= maxval:
            varlistout.append(vardict.variables[i])
end program.
 
begin program.
nocatfilt(1)
print "\n".join(varlistout)
end program.

 

Date: Mon, 19 Jul 2010 02:05:35 -0700
From: [hidden email]
Subject: Re: Python function doesn't work
To: [hidden email]

Ah, variable 'varlistout' is defined within the scope of the function. It doesn't exist outside the function. So the second program block should be inside the function. You could end the function with: return varlistout (with one indentation). Then you call the function:
varlistout  = NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: Re: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 10:41 AM

Dear Albert-Jan,
 
Thanks for your two suggestions, it won't happen again! The traceback I keep getting is:
 

Traceback (most recent call last):

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

NameError: name 'varlistout' is not defined

 
CHEERS!!
 
Ruben

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: Mon, 19 Jul 2010 01:35:29 -0700
From: [hidden email]
Subject: Re: Python function doesn't work
To: [hidden email]

Hi Ruben,
 
What traceback are you getting?
Two minor notes: it's unusual to to put the module imports inside a function; also, function names usually start with a lower case letter (upper case is used for classes).

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 9:44 AM

Dear all,
 
I tried to write a Python function in order to carry out a program Jon Peck made for me. The program -obviously- works fine but as soon as I try to use it as a function it doesn't function anymore. Could anyone please point out what I'm doing wrong?
 
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

*Function, doesn't work.
 
begin program.
def NoCatFil (maxval):
    import spss, spssaux, spssdata
    vardic=spssaux.VariableDict()
    varcount=len(vardic.variables)
    valuesets=[set() for i in range(varcount)]
    curs=spssdata.Spssdata(convertUserMissing=False)
    for case in curs:
        for i in range(varcount):
            if len (valuesets[i]) <=maxval:
                valuesets[i].add(case[i])
    curs.CClose
    varlistout=[]
    for i in range(varcount):
        if len(valuesets[i]) <=maxval:
            varlistout.append(vardic.variables[i])
end program.
 
begin program.
NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)
end program.
 
*Original, works.

begin program.
import spss, spssaux, spssdata
maxval=4
vardic=spssaux.VariableDict()
varcount=len(vardic.variables)
valuesets=[set() for i in range(varcount)]
curs=spssdata.Spssdata(convertUserMissing=False)
for case in curs:
    for i in range(varcount):
        if len (valuesets[i]) <=maxval:
            valuesets[i].add(case[i])
curs.CClose
varlistout=[]
for i in range(varcount):
    if len(valuesets[i]) <=maxval:
        varlistout.append(vardic.variables[i])
if varlistout:
    print "\n".join(varlistout)
end program.


New Windows 7: Simplify what you do everyday. Find the right PC for you.



New Windows 7: Find the right PC for you. Learn more.



Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|

Re: SOLVED(2): Python function doesn't work

Albert-Jan Roskam
Hi Ruben,
 
declaring a global variable will work, but using globals is considered to be a biiiiiiig no-no among programmers: http://www.c2.com/cgi/wiki?GlobalVariablesAreBad

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] SOLVED(2): Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 11:40 AM

I'm sorry for posting excessively, but I just received an alternative solution from the Python forum, which is to define varlistout as a global variable. This works like a charm as well!
 
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 spss, spssaux, spssdata
def nocatfilt(maxval):
    global varlistout
    vardict = spssaux.VariableDict()
    varcount = len(vardict.variables)
    valuesets = [set() for i in range(varcount)]
    curs = spssdata.Spssdata(vardict.variables)
    for case in curs:
        for i in range(varcount):
            if len(valuesets[i]) <= maxval+1:
                valuesets[i].add(case[i])
    curs.CClose()
    varlistout = []
    for i in range(varcount):
        if len(valuesets[i]) <= maxval:
            varlistout.append(vardict.variables[i])
end program.
 
begin program.
nocatfilt(1)
print "\n".join(varlistout)
end program.

 

Date: Mon, 19 Jul 2010 02:05:35 -0700
From: [hidden email]
Subject: Re: Python function doesn't work
To: [hidden email]

Ah, variable 'varlistout' is defined within the scope of the function. It doesn't exist outside the function. So the second program block should be inside the function. You could end the function with: return varlistout (with one indentation). Then you call the function:
varlistout  = NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: Re: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 10:41 AM

Dear Albert-Jan,
 
Thanks for your two suggestions, it won't happen again! The traceback I keep getting is:
 

Traceback (most recent call last):

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

NameError: name 'varlistout' is not defined

 
CHEERS!!
 
Ruben

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: Mon, 19 Jul 2010 01:35:29 -0700
From: [hidden email]
Subject: Re: Python function doesn't work
To: [hidden email]

Hi Ruben,
 
What traceback are you getting?
Two minor notes: it's unusual to to put the module imports inside a function; also, function names usually start with a lower case letter (upper case is used for classes).

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 9:44 AM

Dear all,
 
I tried to write a Python function in order to carry out a program Jon Peck made for me. The program -obviously- works fine but as soon as I try to use it as a function it doesn't function anymore. Could anyone please point out what I'm doing wrong?
 
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

*Function, doesn't work.
 
begin program.
def NoCatFil (maxval):
    import spss, spssaux, spssdata
    vardic=spssaux.VariableDict()
    varcount=len(vardic.variables)
    valuesets=[set() for i in range(varcount)]
    curs=spssdata.Spssdata(convertUserMissing=False)
    for case in curs:
        for i in range(varcount):
            if len (valuesets[i]) <=maxval:
                valuesets[i].add(case[i])
    curs.CClose
    varlistout=[]
    for i in range(varcount):
        if len(valuesets[i]) <=maxval:
            varlistout.append(vardic.variables[i])
end program.
 
begin program.
NoCatFil(maxval=4)
if varlistout:
    print "\n".join(varlistout)
end program.
 
*Original, works.

begin program.
import spss, spssaux, spssdata
maxval=4
vardic=spssaux.VariableDict()
varcount=len(vardic.variables)
valuesets=[set() for i in range(varcount)]
curs=spssdata.Spssdata(convertUserMissing=False)
for case in curs:
    for i in range(varcount):
        if len (valuesets[i]) <=maxval:
            valuesets[i].add(case[i])
curs.CClose
varlistout=[]
for i in range(varcount):
    if len(valuesets[i]) <=maxval:
        varlistout.append(vardic.variables[i])
if varlistout:
    print "\n".join(varlistout)
end program.


New Windows 7: Simplify what you do everyday. Find the right PC for you.



New Windows 7: Find the right PC for you. Learn more.



Express yourself instantly with MSN Messenger! MSN Messenger

Reply | Threaded
Open this post in threaded view
|

Re: SOLVED(2): Python function doesn't work

Jon K Peck
In reply to this post by Ruben Geert van den Berg

Making varlistout global will work, but this approach is generally bad design.  It is much better to use the return statement to make the variable available and assign it in the caller.  While global variables are occasionally necessary (which is why the global statement exists, after all), using the return value in this example would be better.

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



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 07/19/2010 03:45 AM
Subject: [SPSSX-L] SOLVED(2): Python function doesn't work
Sent by: "SPSSX(r) Discussion" <[hidden email]>





I'm sorry for posting excessively, but I just received an alternative solution from the Python forum, which is to define varlistout as a global variable. This works like a charm as well!

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 spss, spssaux, spssdata
def nocatfilt(maxval):
   global varlistout
   vardict = spssaux.VariableDict()
   varcount = len(vardict.variables)
   valuesets = [set() for i in range(varcount)]
   curs = spssdata.Spssdata(vardict.variables)
   for case in curs:
       for i in range(varcount):
           if len(valuesets[i]) <= maxval+1:
               valuesets[i].add(case[i])
   curs.CClose()
   varlistout = []
   for i in range(varcount):
       if len(valuesets[i]) <= maxval:
           varlistout.append(vardict.variables[i])
end program.

begin program.
nocatfilt(1)
print "\n".join(varlistout)
end program.



Date: Mon, 19 Jul 2010 02:05:35 -0700
From: [hidden email]
Subject: Re: Python function doesn't work
To: [hidden email]
Ah, variable 'varlistout' is defined within the scope of the function. It doesn't exist outside the function. So the second program block should be inside the function. You could end the function with: return varlistout (with one indentation). Then you call the function:
varlistout  = NoCatFil(maxval=4)
if varlistout:
   print "\n".join(varlistout)


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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:


From: Ruben van den Berg <[hidden email]>
Subject: Re: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 10:41 AM

Dear Albert-Jan,

Thanks for your two suggestions, it won't happen again! The traceback I keep getting is:

Traceback (most recent call last):
File "<string>", line 3, in <module>
NameError: name 'varlistout' is not defined

 
CHEERS!!

Ruben

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: Mon, 19 Jul 2010 01:35:29 -0700
From: [hidden email]
Subject: Re: Python function doesn't work
To: [hidden email]

Hi Ruben,
 
What traceback are you getting?
Two minor notes: it's unusual to to put the module imports inside a function; also, function names usually start with a lower case letter (upper case is used for classes).

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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/19/10, Ruben van den Berg <[hidden email]> wrote:


From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] Python function doesn't work
To: [hidden email]
Date: Monday, July 19, 2010, 9:44 AM

Dear all,

I tried to write a Python function in order to carry out a program Jon Peck made for me. The program -obviously- works fine but as soon as I try to use it as a function it doesn't function anymore. Could anyone please point out what I'm doing wrong?

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

*Function, doesn't work.

begin program.
def NoCatFil (maxval):
   import spss, spssaux, spssdata
   vardic=spssaux.VariableDict()
   varcount=len(vardic.variables)
   valuesets=[set() for i in range(varcount)]
   curs=spssdata.Spssdata(convertUserMissing=False)
   for case in curs:
       for i in range(varcount):
           if len (valuesets[i]) <=maxval:
               valuesets[i].add(case[i])
   curs.CClose
   varlistout=[]
   for i in range(varcount):
       if len(valuesets[i]) <=maxval:
           varlistout.append(vardic.variables[i])
end program.

begin program.
NoCatFil(maxval=4)
if varlistout:
   print "\n".join(varlistout)
end program.

*Original, works.

begin program.
import spss, spssaux, spssdata
maxval=4
vardic=spssaux.VariableDict()
varcount=len(vardic.variables)
valuesets=[set() for i in range(varcount)]
curs=spssdata.Spssdata(convertUserMissing=False)
for case in curs:
   for i in range(varcount):
       if len (valuesets[i]) <=maxval:
           valuesets[i].add(case[i])
curs.CClose
varlistout=[]
for i in range(varcount):
   if len(valuesets[i]) <=maxval:
       varlistout.append(vardic.variables[i])
if varlistout:
   print "\n".join(varlistout)
end program.


New Windows 7: Simplify what you do everyday. Find the right PC for you.




New Windows 7: Find the right PC for you. Learn more.




Express yourself instantly with MSN Messenger! MSN Messenger