23

I'm trying to do a doctest. The 'Expected' and 'Got' results are identical, but my doctest still fails. It's failing because there are trailing spaces after x-axis y-axis in the printout which I haven't included in my docstring. How do I include it though? When I insert the spaces manually, and do the test, it runs successfully as long as I keep the cursor there.

x-axis y-axis______________________[cursor here]

However, if I run the test with my cursor somewhere else, then the trailing spaces get removed and the test fails.

I know that sounds really strange, but it is what it is!

This is the code:

import pandas as pd
import doctest


class NewDataStructure(pd.DataFrame):
    """
    >>> arrays = [[1, 1, 2, 2], [10, 20, 10, 20]]
    >>> index = pd.MultiIndex.from_arrays(arrays, names=('x-axis', 'y-axis'))
    >>> data_input = {"Pressure (Pa)": [1+1j, 2+2j, 3+3j, 4+4j],
    ...               "Temperature": [1, 2, 3, 4]}
    >>> new_data_variable = NewDataStructure(data=data_input, index=index, title="Pressures and temperatures")
    >>> print new_data_variable
    New Data Structure Pressures and temperatures:
                   Pressure (Pa)  Temperature
    x-axis y-axis                            
    1      10             (1+1j)            1
           20             (2+2j)            2
    2      10             (3+3j)            3
           20             (4+4j)            4

    """
    def __init__(self, data, index, title):
        super(NewDataStructure, self).__init__(data=data, index=index)
        self.title = title

    def __str__(self):
        return "New Data Structure {}:\n{}".format(self.title, super(NewDataStructure, self).__str__())

doctest.testmod()

Below is my result when it fails. Even on here you should be able to select the area after x-axis y-axis and detect whether there are trailing spaces or not.

Failed example:
    print new_data_variable
Expected:
    New Data Structure Pressures and temperatures:
                   Pressure (Pa)  Temperature
    x-axis y-axis
    1      10             (1+1j)            1
           20             (2+2j)            2
    2      10             (3+3j)            3
           20             (4+4j)            4
Got:
    New Data Structure Pressures and temperatures:
                   Pressure (Pa)  Temperature
    x-axis y-axis                            
    1      10             (1+1j)            1
           20             (2+2j)            2
    2      10             (3+3j)            3
           20             (4+4j)            4
bluprince13
  • 4,607
  • 12
  • 44
  • 91
  • `because the spaces are removed on saving the script.` What? – Mad Physicist Feb 21 '17 at 16:10
  • @MadPhysicist I have corrected my question. – bluprince13 Feb 21 '17 at 16:14
  • is this your own code that makes the table? If so, have it suppress the trailing spaces in the output. – Ned Batchelder Feb 21 '17 at 18:09
  • @NedBatchelder nope, it's the pandas dataframe code that adds the trailing spaces, I think. – bluprince13 Feb 21 '17 at 18:38
  • I don't see how coverage.py would be removing the spaces. If you have a reproducible case, you could open an issue against coverage.py – Ned Batchelder Feb 22 '17 at 00:34
  • @NedBatchelder ah okay, while trying to make a reproducible case, I realised the issue wasn't with coverage.py. I have updated my question. Running the test in itself is removing the trailing spaces. Could someone try this out, and see if the error is reproducible? Could it be something to do with my editor? I'm using PyCharm. – bluprince13 Feb 22 '17 at 09:58

1 Answers1

26

I found a solution, using the normalize white space flag

put it either in the doctest as

>>> print new_data_variable  # doctest: +NORMALIZE_WHITESPACE

or when calling the doctest

doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Copperfield
  • 8,131
  • 3
  • 23
  • 29