Commits

AJ R  committed 5ed168e

Added test for custom error, improved documentation

  • Participants
  • Parent commits af3d65f

Comments (0)

Files changed (5)

File savReaderWriter/__init__.py

 from savWriter import *
 from savHeaderReader import *
 
-__all__ = ["SavReader", "SavWriter", "SavHeaderReader"]
+__all__ = ["SavReader", "SavWriter", "SavHeaderReader", "retcodes"]

File savReaderWriter/doc_tests/test_SPSSIOError.txt

+##############################################################################
+## Check if the custom error class does its job
+##############################################################################
+
+>>> import os, tempfile, sys
+>>> sys.stdout.write('<skipline> '); from savReaderWriter import * # doctest: +ELLIPSIS
+<skipline> ...
+>>> import doctest
+
+>>> badSavFile = os.path.join(tempfile.gettempdir(), "yields_error.sav")
+>>> with open(badSavFile, "wb") as f:
+...     line = 79 * "*" + "\n"
+...     for i in range(1000):
+...         f.write(line)
+>>> with SavReader(badSavFile) as reader:
+...     for line in reader:
+...        pass   # doctest:+ELLIPSIS
+Traceback (most recent call last):
+  File "<stdin>", line 1, in ?
+SPSSIOError: Problem opening file '...yields_error.sav' in mode 'rb' [SPSS_INVALID_FILE]
+
+

File savReaderWriter/documentation/formats.xls

Binary file added.

File savReaderWriter/documentation/index.rst

 ``savReaderWriter`` maps both of these formats to a regular alphanumeric string format.
 **Numerical** data formats include: default numeric (``F``) format, scientific notation (``E``), percent (``PCT``), dollar (``DOLLAR``), decimal comma (``COMMA``), decimal dot (``DOT``), zero-padded (``N``). 
 
-``savReaderWriter.SavReader`` formats the ``N`` as a zero-padded version, but does no formatting for the other formats. Formatting implies a lot of additional processing time and e.g. appending a percent sign to a value (``PCT`` format) renders it useless for calculations. Format names are followed by total width (w) and an optional number of decimal positions (d). For example, a format of ``F5.2`` represents a numeric value with a total width of 5, including two decimal positions and a decimal indicator.
+``savReaderWriter.SavReader`` formats the ``N`` as a zero-padded version, but does no formatting for the other formats. Formatting implies a lot of additional processing time and e.g. appending a percent sign to a value (``PCT`` format) renders it useless for calculations. Format names are followed by total width (w) and an optional number of decimal positions (d). For example, a format of ``F5.2`` represents a numeric value with a total width of 5, including two decimal positions and a decimal indicator. A complete list of all the available formats is shown below. Most of these will not be formatted at all by ``savReader``.
 
+.. exceltable:: string and numerical formats in SPSS and ``savReaderWriter`` 
+   :file: ./formats.xls
+   :header: 1
+   :selection: A1:B36
+
+Date formats
+-------------
 Date formats are another group of numerical formats. SPSS stores dates as the number of seconds since midnight, Oct 14, 1582 (the beginning of the Gregorian calendar). The user can make these seconds understandable by giving them a print and/or write format (usually these are set at the same time using the ``FORMATS`` command). Examples of such display formats include ``ADATE`` (American date) and European date, for *mmddyyyy*- and *ddmmyyyy*-style display formats in the SPSS data editor, respectively. ``savReaderWriter`` deliberately does *not* honour these different formats, but tries to convert them to the more practical (sortable) and less ambibiguous ISO 8601 format (*yyyymmdd*). The table below shows how ``savReaderWriter`` converts SPSS dates.
 
 With ``savReaderWriter.SavWriter`` a Python date string value (e.g. "2010-10-25") can be converted to an SPSS Gregorian date (i.e., just a whole bunch of seconds) by using e.g.::
    :header: 1
    :selection: A1:I25
 *Note.*
-[1] ISO 8601 format dates are used wherever possible, e.g. mmddyyyy (ADATE) and ddmmyyyy (EDATE) is not maintained.
+[1] ISO 8601 format dates are used wherever possible, e.g. mmddyyyy (``ADATE``) and ddmmyyyy (``EDATE``) is not maintained.
 [2] Months are converted to quarters using a simple lookup table
 [3] http://docs.python.org/2/library/datetime.html
 [4] ftp://public.dhe.ibm.com/software/analytics/spss/documentation/statistics/20.0/en/client/Manuals/IBM_SPSS_Statistics_Command_Syntax_Reference.pdf

File savReaderWriter/error.py

 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+import os
 import warnings
-from savReaderWriter import *
+from savReaderWriter import retcodes
 
 class SPSSIOError(Exception):
     """
     pass
 
 # Warnings are usually harmless!
-SAVRW_DISPLAY_WARNS = bool(os.environ.get("SAVRW_DISPLAY_WARNS"))
+env = os.environ.get("SAVRW_DISPLAY_WARNS")
+SAVRW_DISPLAY_WARNS = bool(int(env)) if str(env).isdigit() else False
 action = "default" if SAVRW_DISPLAY_WARNS else "ignore"
 warnings.simplefilter(action, SPSSIOWarning)