Possible issue with SavWriter(mode='cp', refSavFileName='master.sav')

Issue #59 new
c odonnell created an issue
File "savReaderWriter/generic.py", line 194, in openSavFile
    with open(savFileName, mode) as f:
ValueError: mode string must begin with one of 'r', 'w', 'a' or 'U', not 'cp'

Attempted to addressed that concern with the following:

-        with open(savFileName, mode) as f:
+        savFileMode = "wb" if mode == b"cp" else mode
+        with open(savFileName, savFileMode) as f:
            fd = f.fileno()

But the problem just moves a little further away since varNames is undefined at line 208...

  File "savReaderWriter/savWriter.py", line 208, in __init__
    self.myStruct = self.getStruct(self.varTypes, self.varNames, self.mode)
  File "savReaderWriter/generic.py", line 341, in getStruct
    for varName in varNames:

If I am understanding how refSavFileName works, I would assume varNames should come from the reference file, not the method args.

Comments (3)

  1. Albert-Jan Roskam repo owner

    Hi,

    This feature has always been a bit problematic. Did you try specifying the model with a b" prefix? (b"cp"). I hope I can find some time to look at this in the weekend (I am writing this from my phone). Here is the link to the unittests, which I am all skipping: https://bitbucket.org/fomcl/savreaderwriter/src/0fe15099eec1d6d19d97167de6f430e108d77209/savReaderWriter/unit_tests/test_SavWriter_mode_cp.py?at=master&fileviewer=file-view-default

    Regards, Albert-Jan

  2. c odonnell reporter

    Hello Albert-Jan,

    Looking at line 194 of generic.py you can see the call to Python open() would fail as open() doesn't know what to do with a mode of 'cp'. The patch corrects that.

    The second issue may be one of my own misunderstanding, or misuse of, the 'cp' mode.

    SavWriter(savFileName, varNames, varTypes, mode='cp', refSavFileName='master.sav')
    

    It seems redundant to have to specify varNames and varTypes when using 'cp' since that should be handled by the underlying call to spssio.spssOpenWriteCopy(). From the spssio docs:

    Copying a dictionary Developers can open a new file for output and initialize its dictionary from that of an existing file. The function, spssOpenWriteCopy , that implements this feature is a slight extension of spssOpenWrite . It is useful when the dictionary or data of an existing file is to be modified or all of its data is to be replaced.

    Best,

    Chuck

  3. Log in to comment