Commits

AJ R committed a2dfd33

Modified many doctests for Python 2 and 3

  • Participants
  • Parent commits 865b376

Comments (0)

Files changed (19)

File savReaderWriter/doc_tests/test_SPSSIOError.txt

 ##############################################################################
 
 >>> import os, tempfile, sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
->>> import doctest
+>>> from savReaderWriter import *
 
 >>> badSavFile = os.path.join(tempfile.gettempdir(), "yields_error.sav")
 >>> with open(badSavFile, "wb") as f:

File savReaderWriter/doc_tests/test_SavHeaderReader_dictionary.txt

 ## Print a Python dictionary of the SPSS dictionary (aka header information)
 ##############################################################################
 
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 >>> savFileName = "../savReaderWriter/test_data/Employee data.sav"
 >>> with SavHeaderReader(savFileName) as spssDict: # doctest: +ELLIPSIS
-...     wholeDict = spssDict.dataDictionary()
+...     metadata = spssDict.dataDictionary()
 >>> desired = \
 ...     {'alignments': {'bdate': 'right',
 ...                     'educ': 'right',
 ...                   'prevexp': 0,
 ...                   'salary': 0,
 ...                   'salbegin': 0}}
->>> desired == wholeDict
+>>> desired == metadata
 True

File savReaderWriter/doc_tests/test_SavHeaderReader_report1.txt

 ## Print a report of the header information
 ##############################################################################
 
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from __future__ import print_function
+>>> from savReaderWriter import *
 
 >>> savFileName = "../savReaderWriter/test_data/spssio_test.sav"
->>> with SavHeaderReader(savFileName) as spssDict: # doctest: +ELLIPSIS
-...     print(unicode(spssDict))
-...     wholeDict = spssDict.dataDictionary()
+>>> with SavHeaderReader(savFileName) as header: # doctest: +ELLIPSIS
+...     print(str(header))
 #ALIGNMENTS
 AGE3 -- right
 DATE_ -- left

File savReaderWriter/doc_tests/test_SavHeaderReader_report2.txt

 ## Print a report of the header information
 ##############################################################################
 
->>> import pprint
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from __future__ import print_function
+>>> from savReaderWriter import *
 
 >>> savFileName = "../savReaderWriter/test_data/Employee data.sav"
->>> with SavHeaderReader(savFileName) as spssDict: # doctest: +ELLIPSIS
-...     print(unicode(spssDict))
-...     wholeDict = spssDict.dataDictionary()
+>>> with SavHeaderReader(savFileName) as header: # doctest: +ELLIPSIS
+...     print(str(header))
 #ALIGNMENTS
 salary -- right
 jobcat -- right

File savReaderWriter/doc_tests/test_SavHeaderReader_report_utf8.txt

 
 >>> # -*- coding: utf-8 -*-
 >>> import pprint
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 # Unicode and doctest is a little awkward. This could be done in a better way, I guess
 >>> savFileName = "../savReaderWriter/test_data/greetings.sav"
->>> with SavHeaderReader(savFileName, ioUtf8=True) as spssDict:
-...     pprint.pprint( repr(unicode(spssDict)).split("\n") )  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
+>>> with SavHeaderReader(savFileName, ioUtf8=True) as header:
+...     pprint.pprint( repr(str(header)).split("\n") )  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
 ['u"File \'greetings.sav\' built using SavReaderWriter.py version ... (... ... ... ...:...:... ...)\\n#ALIGNMENTS\\nline -- left\\ngreeting -- left\\nBondjo\\xfb -- left\\n#CASEWEIGHTVAR\\n#COLUMNWIDTHS\\nline -- 8\\ngreeting -- 50\\nBondjo\\xfb -- 20\\n#FILEATTRIBUTES\\n#FILELABEL\\nFile created by user \'Administrator\' at Thu Jan 17 16:35:14 2013\\n#FORMATS\\nline -- F8.2\\ngreeting -- A50\\nBondjo\\xfb -- A20\\n#MEASURELEVELS\\nline -- unknown\\ngreeting -- unknown\\nBondjo\\xfb -- unknown\\n#MISSINGVALUES\\nline: lower -- 0.0\\nline: upper -- 9.0\\nBondjo\\xfb: values -- \\xa1hola! \\n#MULTRESPDEFS\\n#VALUELABELS\\nBondjo\\xfb: Thai -- \\u0e2a\\u0e27\\u0e31\\u0e2a\\u0e14\\u0e35\\n#VARATTRIBUTES\\n#VARLABELS\\ngreeting -- \\u0627\\u0644\\u0633\\u0644\\u0627\\u0645 \\u0639\\u0644\\u064a\\u0643\\u0645\\n#VARNAMES\\nline\\nBondjo\\xfb\\ngreeting\\n#VARROLES\\nline -- input\\ngreeting -- input\\nBondjo\\xfb -- input\\n#VARSETS\\n#VARTYPES\\nline -- 0\\ngreeting -- 50\\nBondjo\\xfb -- 20"']

File savReaderWriter/doc_tests/test_SavHeaderReader_type7.txt

 ## Query type 7 record information of a file
 ##############################################################################
 
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 # e.g. Record subtype 3, which contains 'Release information' is present (True)
 # This file contains release info, but no date variable info, etc. etc.
 >>> savFileName = "../savReaderWriter/test_data/Employee data.sav"
->>> with SavHeaderReader(savFileName) as spssDict:
-...    type7info = spssDict.queryType7
+>>> with SavHeaderReader(savFileName) as header:
+...    type7info = header.queryType7
 >>> desired = \
 ... {3: ('Release information', True),
 ...  4: ('Floating point constants including the system missing value', True),

File savReaderWriter/doc_tests/test_SavHeaderReader_unicode.txt

 ## Print a Python dictionary of the SPSS dictionary (aka header information)
 ##############################################################################
 
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
-
+>>> from savReaderWriter import *
 
 >>> savFileName = "../savReaderWriter/test_data/greetings.sav"
->>> with SavHeaderReader(savFileName, ioUtf8=True) as spssDict: # doctest: +ELLIPSIS
-...     really = spssDict.dataDictionary()
+>>> with SavHeaderReader(savFileName, ioUtf8=True) as header: # doctest: +ELLIPSIS
+...     metadata = header.dataDictionary()
 
 >>> thai = u'\u0e2a\u0e27\u0e31\u0e2a\u0e14\u0e35'
 >>> arabic = (u'\u0627\u0644\u0633\u0644\u0627\u0645 '+ 
 ...                u'line': u'input'},
 ...  u'varSets': {},
 ...  u'varTypes': {u'Bondjo\xfb': 20, u'greeting': 50, u'line': 0}}
->>> desired == really
+>>> desired == metadata
 True
 >>> for k, v in desired.items():
-...     "%s ---> %s" % (k.upper(), v == really[k])
-...     if v != really[k]:
+...     "%s ---> %s" % (k.upper(), v == metadata[k])
+...     if v != metadata[k]:
 ...         print v
-...         print really[k]
+...         print metadata[k]
 u'VALUELABELS ---> True'
 u'CASEWEIGHTVAR ---> True'
 u'VARSETS ---> True'

File savReaderWriter/doc_tests/test_SavReader_dates_elaborate.txt

 ## Read a file containg all the supported date formats
 ##############################################################################
 
+>>> from __future__ import print_function
 >>> import tempfile
 >>> import os
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
-
+>>> from savReaderWriter import *
 
 ## first, create a test file
 >>> varNames = ['var_datetime', 'var_wkyr', 'var_date', 'var_qyr', 'var_edate',
 ...     records = data.all()
 ...     formats = formats.items()
 ...     for n, record in enumerate(records):
-...         print "Record %d".center(79, "-") % n
+...         print("Record %d".center(79, "-") % n)
 ...         for i, value in enumerate(record):
-...             print "%s --> %s --> %s" % (spssfmts[i], strftimefmts[i], value)
+...             print("%s --> %s --> %s" % (spssfmts[i], strftimefmts[i], value))
 -----------------------------------Record 0-----------------------------------
 DATETIME40 --> %Y-%m-%d %H:%M:%S --> 2010-08-11 00:00:00
 WKYR10 --> %W WK %Y --> 32 WK 2010
 ...     #formats = formats.items()
 ...     for n, record in enumerate(records):
 ...         if n >= 2:
-...             print "Record %d".center(79, "-") % n
+...             print("Record %d".center(79, "-") % n)
 ...             for i, value in enumerate(record):
-...                 print "%s --> %s --> %s" % (spssfmts[i], strftimefmts[i], value)
+...                 print("%s --> %s --> %s" % (spssfmts[i], strftimefmts[i], value))
 -----------------------------------Record 2-----------------------------------
 DATETIME40 --> %Y-%m-%d %H:%M:%S --> 999
 WKYR10 --> %W WK %Y --> 999

File savReaderWriter/doc_tests/test_SavReader_file_info.txt

 ## Read a file and get some basic file info
 ##############################################################################
 
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 ## ... Get some basic file info
 >>> savFileName = "../savReaderWriter/test_data/Employee data.sav"

File savReaderWriter/doc_tests/test_SavReader_get.txt

 ## Read a file and do a binary search for records (get)
 ##############################################################################
 
->>> import pprint
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from __future__ import print_function
+>>> from savReaderWriter import *
 
 ## ... Do a binary search for records --> idVar
 >>> savFileName = "../savReaderWriter/test_data/Employee data.sav"
 >>> try:    # doctest: +ELLIPSIS
 ...     reader = SavReader(savFileName, idVar="id")       
-...     print reader.get(4, "not found")            # gets first record where id==4
-...     print reader.get(666, "not found")
-...     print reader.get(474, "not found")
-...     print reader.get(475, "not found")
-...     print reader.get(4, "not found", full=True) # gets all records where id==4
+...     print(reader.get(4, "not found"))            # gets first record where id==4
+...     print(reader.get(666, "not found"))
+...     print(reader.get(474, "not found"))
+...     print(reader.get(475, "not found"))
+...     print(reader.get(4, "not found", full=True)) # gets all records where id==4
 ... finally:
 ...     reader.close()
 ...

File savReaderWriter/doc_tests/test_SavWriter_mode_ab.txt

 ## Append some records to an existing file, using mode="ab"
 ##############################################################################
 
+>>> from __future__ import print_function
 >>> import shutil
 >>> import os
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 ## Append some records to an existing file
 ## First make a copy to work with (append records to)
 ## Demonstrate that the number of records has increased by 100.
 >>> try: # doctest: +ELLIPSIS
 ...     r = SavReader(savFileName)
-...     print len(r) == NROWS_ORIGINAL + NROWS_EXTRA 
+...     print(len(r) == NROWS_ORIGINAL + NROWS_EXTRA)
 ... finally:
 ...     r.close()
 ...

File savReaderWriter/doc_tests/test_SavWriter_mode_cp.txt

 ## Use a nested context manager and mode="cp"
 ##############################################################################
 
+>>> from __future__ import print_function
 >>> import contextlib
 >>> import os
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 ###############
 ## Copy header info from one file to another (Method #1)
 # read the data back in to see if it looks the same
 >>> with SavReader("Employee data_out.sav", returnHeader=True) as r:  # doctest: +ELLIPSIS
 ...     header = r.next()
-...     print header
+...     print(header)
 ...     for line in r:
-...         print line
+...         print(line)
 ['id', 'gender', 'bdate', 'educ', 'jobcat', 'salary', 'salbegin', 'jobtime', 'prevexp', 'minority']
 [1.0, 'm', '1952-02-03', 15.0, 3.0, 57000.0, 27000.0, 98.0, 144.0, 0.0]
 [2.0, 'm', '1958-05-23', 16.0, 1.0, 40200.0, 18750.0, 98.0, 36.0, 0.0]

File savReaderWriter/doc_tests/test_SavWriter_multRespDefs.txt

 
 >>> import os
 >>> import tempfile
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 # preparare some data
 >>> records = [['Test1', 1, 1, 'Yes', 'No'], ['Test2', 2, 1, 'No', 'Yes']]

File savReaderWriter/doc_tests/test_SavWriter_typical.txt

 ## Write a file, typical use
 ##############################################################################
 
+>>> from __future__ import print_function
 >>> import os
 >>> import tempfile
->>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 
 ## ----- Typical use
 >>> with SavWriter(savFileName, varNames, varTypes) as sav:
 ...     for record in records:
 ...         sav.writerow(record)
-...     print "Done! %s" % os.path.basename(sav.savFileName)
+...     print("Done! %s" % os.path.basename(sav.savFileName))
 Done! test.sav
 
 # read it back in
 ... finally:
 ...     reader.close()
 ...
->>> print records_out == records
+>>> print(records_out == records)
 True
 
 # clean up

File savReaderWriter/doc_tests/test_SavWriter_utf8_mode.txt

 >>> import tempfile
 >>> import pprint
 >>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 ## ... check if non-ascii encodings work well
 ## ... notice the use of 'ioUtf8'

File savReaderWriter/doc_tests/test_SavWriter_writerow_date.txt

 ## Write a file, incl. SPSS date values
 ##############################################################################
 
+>>> from __future__ import print_function
 >>> import os
 >>> import tempfile
 >>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 ## ----- Write one row
 # var1 is a 5-char string var, the others are numerical:
 ...     pos = varNames.index("bdate")
 ...     for record in records:
 ...         record[pos] = sav.spssDateTime(record[pos], "%Y-%m-%d")
-...         print record
+...         print(record)
 ...         sav.writerow(record)
-...     print "Done! %s" % os.path.basename(sav.savFileName)
+...     print("Done! %s" % os.path.basename(sav.savFileName))
 ['Test1', 1, 1, 13500864000.0]
 ['Test2', 2, 1, 10326873600.0]
 Done! test.sav

File savReaderWriter/doc_tests/test_SavWriter_writerows_elaborate.txt

 ## Write a file (elaborate example)
 ##############################################################################
 
+>>> from __future__ import print_function
 >>> import os
 >>> import tempfile
 >>> import sys
 ...                missingValues=missingValues, varAttributes=varAttributes,
 ...                fileAttributes=fileAttributes) as sav:
 ...     sav.writerows(records)
-...     print "Done! %s" % os.path.basename(sav.savFileName)
+...     print("Done! %s" % os.path.basename(sav.savFileName))
 Done! employee data_OUT.sav
 
 # read it back in 
 ...
 >>> records_out = [line for line in iter(reader)]
 >>> reader.close()
->>> print records_out == records  # doctest: +NORMALIZE_WHITESPACE
+>>> print(records_out == records)  # doctest: +NORMALIZE_WHITESPACE
 True
 
 # check if the header info is okay

File savReaderWriter/doc_tests/test_SavWriter_writerows_zsav.txt

 ## Write a file, in zsav compressed format
 ##############################################################################
 
+>>> from __future__ import print_function
 >>> import os
 >>> import tempfile
 >>> import sys
->>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
-<skipline> ...
+>>> from savReaderWriter import *
 
 ## ----- Write many rows
-# .zsav = zlib compressed (--> v21 libs)
+# .zsav = zlib compressed (--> >= v21 libs)
 >>> savFileName = os.path.join(tempfile.gettempdir(), "test.zsav")
 >>> records = [['Test1', 1, 1, '2010-08-11'], ['Test2', 2, 1, '1910-01-12']]
 >>> varNames = ['var1', 'var2', 'var3', 'bdate']
 >>> varTypes = {'var1': 50, 'var2': 0, 'var3': 0, 'bdate': 10}
 >>> with SavWriter(savFileName, varNames, varTypes) as sav:
 ...     sav.writerows(records)
-...     print "Done! %s" % os.path.basename(sav.savFileName)
+...     print("Done! %s" % os.path.basename(sav.savFileName))
 Done! test.zsav
 
 # read it back in
 ...
 >>> records_out = [line for line in iter(reader)]
 >>> reader.close()
->>> print records_out == records  # doctest: +NORMALIZE_WHITESPACE
+>>> print(records_out == records)
 True
 
 # clean up

File savReaderWriter/unit_tests/test_SavHeaderReader_dataDict_namedtuple.py

                          b'weightVar': b'nominal'}
         self.assertEqual(self.metadata.measureLevels, measureLevels)
 
-    @unittest.skip("===========> CHECK THIS LATER!")
+    #@unittest.skip("===========> CHECK THIS LATER!")
     def test_missingValues(self):
         sysmis = -1 * sys.float_info.max
         missingValues = {b'AGE2': {},
                          b'someDate': {},
                          b'weightVar': {}}
         miss = self.metadata.missingValues
-        self.assertAlmostEqual(miss[b"Income1"][b"lower"], sysmis)
-        self.assertAlmostEqual(miss[b"Income2"][b"lower"], sysmis)
+        A_VERY_SMALL_NUMBER = 10 ** -50
+        self.assertTrue(miss[b"Income1"][b"lower"] < A_VERY_SMALL_NUMBER)
+        self.assertTrue(miss[b"Income2"][b"lower"] <A_VERY_SMALL_NUMBER)
 
         del miss[b"Income1"][b"lower"]
         del miss[b"Income2"][b"lower"]