|
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. |
|
|
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]
New Windows 7: Find the right PC for you. Learn more. |
|
|
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]
Express yourself instantly with MSN Messenger! MSN Messenger |
|
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]
Express yourself instantly with MSN Messenger! MSN Messenger |
|
|
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
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]
Express yourself instantly with MSN Messenger! MSN Messenger |
| Free forum by Nabble | Edit this page |
