Doctest:识别预期输出和实际输出之间差异的最佳方法是什么?

Doctest: What is the best way to identify differences between expected and actual output?

有人可以帮助我了解在执行 doctest 时如何识别实际输出和预期输出之间的差异吗?在下面,预期和实际输出看起来完全相同。

我很确定有一些白色的 space 错误,因为当我在 doctest 中添加 optionflags= doctest.NORMALIZE_WHITESPACE 时,测试成功了。是否有另一个标志可以帮助用户三角测量这种白色 space 错误的位置?

import datetime
import numpy as np
import pandas as pd
import yfinance as yf

def id_maribozu(df, tol = 0.0025):
    '''
    >>> df = yf.download('aapl', start = '2021-05-17', end = '2021-06-09', progress=False)
    >>> id_maribozu(df)
                  Open    High     Low   Close  Adj Close    Volume  mbzu  mbzu_stop
    Date
    2021-05-17  126.82  126.93  125.17  126.27     125.90  74244600     0       0.00
    2021-05-18  126.56  126.99  124.78  124.85     124.48  63342900     0       0.00
    2021-05-19  123.16  124.92  122.86  124.69     124.32  92612000     1     122.86
    2021-05-20  125.23  127.72  125.10  127.31     126.93  76857100     0       0.00
    2021-05-21  127.82  128.00  125.21  125.43     125.06  79295400    -1     128.00
    2021-05-24  126.01  127.94  125.94  127.10     126.72  63092900     0       0.00
    2021-05-25  127.82  128.32  126.32  126.90     126.53  72009500     0       0.00
    2021-05-26  126.96  127.39  126.42  126.85     126.48  56575900     0       0.00
    2021-05-27  126.44  127.64  125.08  125.28     124.91  94625600     0       0.00
    2021-05-28  125.57  125.80  124.55  124.61     124.24  71311100    -1     125.80
    2021-06-01  125.08  125.35  123.94  124.28     123.91  67637100     0       0.00
    2021-06-02  124.28  125.24  124.05  125.06     124.69  59278900     1     124.05
    2021-06-03  124.68  124.85  123.13  123.54     123.18  76229200     0       0.00
    2021-06-04  124.07  126.16  123.85  125.89     125.52  75169300     1     123.85
    2021-06-07  126.17  126.32  124.83  125.90     125.53  71057600     0       0.00
    2021-06-08  126.60  128.46  126.21  126.74     126.37  74403800     0       0.00
    '''
    bull_mbzu = (abs(df['Close'] / df['High'] - 1) < 0.0025) & (abs(df['Open'] / df['Low'] - 1) < tol)
    bear_mbzu = (abs(df['Open'] / df['High'] - 1) < 0.0025) & (abs(df['Close'] / df['Low'] - 1) < tol)
    df['mbzu'] = np.where(bull_mbzu, 1, np.where(bear_mbzu, -1, 0))
    df.loc[:,'mbzu_stop'] = np.where(bull_mbzu, df['Low'], np.where(bear_mbzu, df['High'], 0))
    print(df.round(2))

if __name__ == "__main__":
    import doctest
    doctest.testmod()
>>> %run id_maribozu.py
**********************************************************************
File "/Volumes/Data/TechnicalAnalysis/Candlesticks/Maribozu/id_maribozu.py", line 10, in __main__.id_maribozu
Failed example:
    id_maribozu(df)
Expected:
                  Open    High     Low   Close  Adj Close    Volume  mbzu  mbzu_stop
    Date
    2021-05-17  126.82  126.93  125.17  126.27     125.90  74244600     0       0.00
    2021-05-18  126.56  126.99  124.78  124.85     124.48  63342900     0       0.00
    2021-05-19  123.16  124.92  122.86  124.69     124.32  92612000     1     122.86
    2021-05-20  125.23  127.72  125.10  127.31     126.93  76857100     0       0.00
    2021-05-21  127.82  128.00  125.21  125.43     125.06  79295400    -1     128.00
    2021-05-24  126.01  127.94  125.94  127.10     126.72  63092900     0       0.00
    2021-05-25  127.82  128.32  126.32  126.90     126.53  72009500     0       0.00
    2021-05-26  126.96  127.39  126.42  126.85     126.48  56575900     0       0.00
    2021-05-27  126.44  127.64  125.08  125.28     124.91  94625600     0       0.00
    2021-05-28  125.57  125.80  124.55  124.61     124.24  71311100    -1     125.80
    2021-06-01  125.08  125.35  123.94  124.28     123.91  67637100     0       0.00
    2021-06-02  124.28  125.24  124.05  125.06     124.69  59278900     1     124.05
    2021-06-03  124.68  124.85  123.13  123.54     123.18  76229200     0       0.00
    2021-06-04  124.07  126.16  123.85  125.89     125.52  75169300     1     123.85
    2021-06-07  126.17  126.32  124.83  125.90     125.53  71057600     0       0.00
    2021-06-08  126.60  128.46  126.21  126.74     126.37  74403800     0       0.00
Got:
                  Open    High     Low   Close  Adj Close    Volume  mbzu  mbzu_stop
    Date
    2021-05-17  126.82  126.93  125.17  126.27     125.90  74244600     0       0.00
    2021-05-18  126.56  126.99  124.78  124.85     124.48  63342900     0       0.00
    2021-05-19  123.16  124.92  122.86  124.69     124.32  92612000     1     122.86
    2021-05-20  125.23  127.72  125.10  127.31     126.93  76857100     0       0.00
    2021-05-21  127.82  128.00  125.21  125.43     125.06  79295400    -1     128.00
    2021-05-24  126.01  127.94  125.94  127.10     126.72  63092900     0       0.00
    2021-05-25  127.82  128.32  126.32  126.90     126.53  72009500     0       0.00
    2021-05-26  126.96  127.39  126.42  126.85     126.48  56575900     0       0.00
    2021-05-27  126.44  127.64  125.08  125.28     124.91  94625600     0       0.00
    2021-05-28  125.57  125.80  124.55  124.61     124.24  71311100    -1     125.80
    2021-06-01  125.08  125.35  123.94  124.28     123.91  67637100     0       0.00
    2021-06-02  124.28  125.24  124.05  125.06     124.69  59278900     1     124.05
    2021-06-03  124.68  124.85  123.13  123.54     123.18  76229200     0       0.00
    2021-06-04  124.07  126.16  123.85  125.89     125.52  75169300     1     123.85
    2021-06-07  126.17  126.32  124.83  125.90     125.53  71057600     0       0.00
    2021-06-08  126.60  128.46  126.21  126.74     126.37  74403800     0       0.00
**********************************************************************
1 items had failures:
   1 of   2 in __main__.id_maribozu
***Test Failed*** 1 failures.

尝试使用标志 REPORT_NDIFF。文档说,“这是标记行内和行间差异的唯一方法”,这将有助于识别任何空白问题,例如:

"""
>>> print('Hello goodbye')
Hello goodbye\r
"""
Failed example:
    print('Hello goodbye')
Differences (ndiff with -expected +actual):
    - Hello goodbye
    ?              -
    + Hello goodbye

请注意return实际上并没有出现在这里。