如何在doctest中插入尾随空格,以便即使实际结果和预期结果看起来相同也不会失败? - python

我正在尝试做doctest。 “预期”和“获得”的结果相同,但是我的doctest仍然失败。之所以失败,是因为打印输出中x-axis y-axis后面有空格,但我的文档字符串中没有这些空格。我如何包括它呢?当我手动插入空格并进行测试时,只要将光标保持在该位置即可成功运行。

x轴y轴______________________ [此处的光标]

但是,如果我用光标在其他地方运行测试,则尾随空格将被删除,测试将失败。

我知道这听起来真的很奇怪,但这就是事实!

这是代码:

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()

以下是失败时的结果。即使在这里,您也应该能够选择x-axis y-axis之后的区域并检测是否有尾随空格。

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

python大神给出的解决方案

我使用normalize white space flag找到了解决方案

将其放在doctest中作为

    >>> print new_data_variable # doctest: +NORMALIZE_WHITESPACE

或致电doctest时

doctest.testmod( optionflags= doctest.NORMALIZE_WHITESPACE ) 

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

Python numpy数据指针地址无需更改即可更改 - python

编辑经过一些摆弄之后,到目前为止,我已经隔离了以下状态:一维数组在直接输入变量时提供两个不同的地址,而在使用print()时仅提供一个地址2D数组(或矩阵)在直接输入变量时提供三个不同的地址,在使用print()时提供两个地址3D数组在直接输入变量时提供两个不同的地址,而在使用print()时仅给出一个(显然与一维数组相同)像这样:>>> …

在Mac上的终端中停止python - python

在Mac上的终端中使用python,键入ctrl-z 将停止python,但不退出它,给出如下输出:>>> [34]+ Stopped python 如您所见,我已经停止了34个python调用。虽然我可以用>>> exit() 退出python,问题是:是否有一个快捷键可以真正在终端中退出(而不只是停止)python?而…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…