Commits

AJ R committed 1175b06

added test for dates, small modifications wrt date formatting

Comments (0)

Files changed (3)

savReaderWriter/__init__.py

     17: ("SPSS_FMT_E", "E Format- with explicit power of 10"),
     20: ("SPSS_FMT_DATE", "Date format dd-mmm-yyyy"),
     21: ("SPSS_FMT_TIME", "Time format hh:mm:ss.s"),
-    22: ("SPSS_FMT_DATE_TIME", "Date and Time"),
+    22: ("SPSS_FMT_DATETIME", "Date and Time"),
     23: ("SPSS_FMT_ADATE", "Date format dd-mmm-yyyy"),
     24: ("SPSS_FMT_JDATE", "Julian date - yyyyddd"),
     25: ("SPSS_FMT_DTIME", "Date-time dd hh:mm:ss.s"),

savReaderWriter/doc_tests/test_SavReader_dates_elaborate.txt

+##############################################################################
+## Read a file containg all the supported date formats
+##############################################################################
+
+>>> import tempfile
+>>> import os
+>>> import sys
+>>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
+<skipline> ...
+
+
+## first, create a test file
+>>> varNames = ['var_datetime', 'var_wkyr', 'var_date', 'var_qyr', 'var_edate',
+...             'var_sdate', 'var_dtime', 'var_jdate', 'var_month', 'var_moyr',
+...             'var_time', 'var_adate', 'var_wkday']
+>>> varTypes = {v : 0 for v in varNames}
+>>> spssfmts = ['DATETIME40', 'WKYR10', 'DATE10', 'QYR10', 'EDATE10',
+...             'SDATE10', 'DTIME10', 'JDATE10', 'MONTH10', 'MOYR10',
+...             'TIME10', 'ADATE10', 'WKDAY10']
+>>> formats = dict(zip(varNames, spssfmts))
+>>> savFileName = os.path.join(tempfile.gettempdir(), "test_dates.sav")
+>>> records = [['2010-08-11'] * len(varNames), ['1910-01-12'] * len(varNames)]
+>>> with SavWriter(savFileName, varNames, varTypes, formats=formats) as writer:
+...     for record in records:
+...         for pos, value in enumerate(record):
+...             record[pos] = writer.spssDateTime(record[pos], "%Y-%m-%d")
+...         writer.writerow(record)
+
+ 
+## second, read the file back in
+# these are the destination formats that are used internally by savReaderWriter
+# (see __init__.py)
+>>> strftimefmts = ['%Y-%m-%d %H:%M:%S', '%W WK %Y', '%Y-%m-%d', '%m Q %Y', '%Y-%m-%d',
+...                 '%Y-%m-%d', '%d %H:%M:%S', '%Y-%m-%d', '%B', '%B %Y', '%H:%M:%S.%f',
+...                 '%Y-%m-%d', '%A']
+>>> data = SavReader(savFileName)
+>>> with data:
+...     records = data.all()
+...     formats = formats.items()
+...     for n, record in enumerate(records):
+...         print "Record %d".center(79, "-") % n
+...         for i, value in enumerate(record):
+...             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
+DATE10 --> %Y-%m-%d --> 2010-08-11
+QYR10 --> %m Q %Y --> 3 Q 2010
+EDATE10 --> %Y-%m-%d --> 2010-08-11
+SDATE10 --> %Y-%m-%d --> 2010-08-11
+DTIME10 --> %d %H:%M:%S --> 11 00:00:00
+JDATE10 --> %Y-%m-%d --> 2010-08-11
+MONTH10 --> %B --> August
+MOYR10 --> %B %Y --> August 2010
+TIME10 --> %H:%M:%S.%f --> 00:00:00.000000
+ADATE10 --> %Y-%m-%d --> 2010-08-11
+WKDAY10 --> %A --> Wednesday
+-----------------------------------Record 1-----------------------------------
+DATETIME40 --> %Y-%m-%d %H:%M:%S --> 1910-01-12 00:00:00
+WKYR10 --> %W WK %Y --> 02 WK 1910
+DATE10 --> %Y-%m-%d --> 1910-01-12
+QYR10 --> %m Q %Y --> 1 Q 1910
+EDATE10 --> %Y-%m-%d --> 1910-01-12
+SDATE10 --> %Y-%m-%d --> 1910-01-12
+DTIME10 --> %d %H:%M:%S --> 12 00:00:00
+JDATE10 --> %Y-%m-%d --> 1910-01-12
+MONTH10 --> %B --> January
+MOYR10 --> %B %Y --> January 1910
+TIME10 --> %H:%M:%S.%f --> 00:00:00.000000
+ADATE10 --> %Y-%m-%d --> 1910-01-12
+WKDAY10 --> %A --> Wednesday
+
+# clean up
+>>> os.remove(savFileName)

savReaderWriter/header.py

             format_ = format_.upper()
             gotString = isStringVar.match(format_)
             gotAny = isAnyVar.match(format_)
-            msg = ("Unknown format %r for variable %r. " +
+            msg = ("Unknown format %r or invalid width for variable %r. " +
                    "Valid formats are: %s")
-            msg = msg % (", ".join(validValues), format_, varName)
+            msg = msg % (format_, varName, ", ".join(validValues))
             if gotString:
                 printFormat = gotString.group("printFormat")
                 printFormat = reverseFormats.get(printFormat)