[Tutor] Question about writing to Excel with slavic characters

Walter Prins wprins at gmail.com
Thu Mar 8 17:18:27 CET 2012


Hi Marko,

I'm going out on a limb here as I know next to nothing about either
SPSS or Albert-Jan's wrapper module, and so with that caveat, some
comments/observations:

On 8 March 2012 14:59, Marko Limbek <marko.limbek at valicon.net> wrote:
> I overcame commenting. I managed to read my own file and print it. Now
> I am trying to use method getNumberofVariables()  but unsuccesfully.
> First way
>
> SavReader(savFileName).getNumberofVariables()
>
> gives me this error
>
> Traceback (most recent call last):
>  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
> line 660, in <module>
>    SavReader(savFileName).getNumberofVariables()
> TypeError: getNumberofVariables() takes exactly 3 arguments (1 given)
>
>
>
> Second way
>
> getNumberofVariables(savFileName, fh, spssio)
>
> gives me this error
>
> Traceback (most recent call last):
>  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
> line 660, in <module>
>    getNumberofVariables(savFileName, fh, spssio)
> NameError: name 'getNumberofVariables' is not defined

The short answer:
==============
Don't use getNumberofVariables, use the script as demonstrated at the
bottom of the script in the "if __name__ == "__main__" section.  I
suppose something like this should do (I've modified it slightly to
make it a bit more obvious what's happening):

## ----- Get some basic file info
savFileName = r"C:\Program
Files\IBM\SPSS\Statistics\19\Samples\English\Employee data.sav"
mySavReaderObj = SavReader(savFileName)
numVars, nCases, varNames, varTypes, printTypesFile, printTypeLabels,
varWids = \
    mySavReaderObj.getSavFileInfo()
#Now the number of variables is presumably in numVars...


The longer answer:
==============
Firstly, getNumberofVariables() is defined as a class instance method.
 This means you can only ever call it on an object instance of that
class. It is not a standalone function that can be called freestanding
as you tried to do in your latter attempt.  That is why you got the
"not defined" error -- There is no freestanding function by that name
(even if there happens to be a /method/ by that name inside the
SavReader class.) so Python complained as such.

Your prior attempt, which as you noticed requires to have parameters
fh and spssio supplied appears to me to be effectively somewhat of an
internal/private/helper method that is written in the style of a
function, so consequently the method has no external dependencies on
any state in the object/class and hence has to have the spssio and fh
supplied when it's called.  You can see elsewhere in the class when
this method is called, the object itself keeps track of the fh and the
sspsio and so passes these into the method as required.

>From this you can infer that you'd be able to do the same and thus
successfully call getNumberofVariables() by retrieving the fh and
sspsio from your SavReader object (e.g. what I called mySavReaderObj
above), by passing in mySavReaderObj.fh and mySavREaderObj.spssio when
calling mySavReaderObj.getNumberofVariables().  But, as per the short
answer, you probably don't want to do that.

As an aside it also then follows it would be possible to
rewrite/refactor the getNumberofVariables() method (and indeed several
others) to remove the fh and sspsio parameters, by having them picked
up directly from the object instance when required.   Debateably, it
might be an improvement that makes the code a bit more object oriented
and less surprising to use.

HTH,

Walter


More information about the Tutor mailing list