Source

savReaderWriter / 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),
...            [""] * len(varNames), [None] * 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
-----------------------------------Record 2-----------------------------------
DATETIME40 --> %Y-%m-%d %H:%M:%S --> None
WKYR10 --> %W WK %Y --> None
DATE10 --> %Y-%m-%d --> None
QYR10 --> %m Q %Y --> None
EDATE10 --> %Y-%m-%d --> None
SDATE10 --> %Y-%m-%d --> None
DTIME10 --> %d %H:%M:%S --> None
JDATE10 --> %Y-%m-%d --> None
MONTH10 --> %B --> None
MOYR10 --> %B %Y --> None
TIME10 --> %H:%M:%S.%f --> None
ADATE10 --> %Y-%m-%d --> None
WKDAY10 --> %A --> None
-----------------------------------Record 3-----------------------------------
DATETIME40 --> %Y-%m-%d %H:%M:%S --> None
WKYR10 --> %W WK %Y --> None
DATE10 --> %Y-%m-%d --> None
QYR10 --> %m Q %Y --> None
EDATE10 --> %Y-%m-%d --> None
SDATE10 --> %Y-%m-%d --> None
DTIME10 --> %d %H:%M:%S --> None
JDATE10 --> %Y-%m-%d --> None
MONTH10 --> %B --> None
MOYR10 --> %B %Y --> None
TIME10 --> %H:%M:%S.%f --> None
ADATE10 --> %Y-%m-%d --> None
WKDAY10 --> %A --> None


## Test if recodeSysmisTo arg does what it is supposed to.
>>> data = SavReader(savFileName, recodeSysmisTo="999")
>>> with data:
...     records = data.all()
...     #formats = formats.items()
...     for n, record in enumerate(records):
...         if n >= 2:
...             print "Record %d".center(79, "-") % n
...             for i, value in enumerate(record):
...                 print "%s --> %s --> %s" % (spssfmts[i], strftimefmts[i], value)
-----------------------------------Record 2-----------------------------------
DATETIME40 --> %Y-%m-%d %H:%M:%S --> 999
WKYR10 --> %W WK %Y --> 999
DATE10 --> %Y-%m-%d --> 999
QYR10 --> %m Q %Y --> 999
EDATE10 --> %Y-%m-%d --> 999
SDATE10 --> %Y-%m-%d --> 999
DTIME10 --> %d %H:%M:%S --> 999
JDATE10 --> %Y-%m-%d --> 999
MONTH10 --> %B --> 999
MOYR10 --> %B %Y --> 999
TIME10 --> %H:%M:%S.%f --> 999
ADATE10 --> %Y-%m-%d --> 999
WKDAY10 --> %A --> 999
-----------------------------------Record 3-----------------------------------
DATETIME40 --> %Y-%m-%d %H:%M:%S --> 999
WKYR10 --> %W WK %Y --> 999
DATE10 --> %Y-%m-%d --> 999
QYR10 --> %m Q %Y --> 999
EDATE10 --> %Y-%m-%d --> 999
SDATE10 --> %Y-%m-%d --> 999
DTIME10 --> %d %H:%M:%S --> 999
JDATE10 --> %Y-%m-%d --> 999
MONTH10 --> %B --> 999
MOYR10 --> %B %Y --> 999
TIME10 --> %H:%M:%S.%f --> 999
ADATE10 --> %Y-%m-%d --> 999
WKDAY10 --> %A --> 999


# clean up
>>> os.remove(savFileName)