>> import doctest >>> doctest.ELLIPSIS_MARKER = -6ren">
gpt4 book ai didi

python - doctest 在省略号后忽略行的前面

转载 作者:行者123 更新时间:2023-12-04 08:04:04 27 4
gpt4 key购买 nike

文档似乎不太清楚如何解决以下问题......

def test():
"""
>>> import doctest
>>> doctest.ELLIPSIS_MARKER = '<ignore>'
>>> import pandas as pd
>>> raise pd.errors.InvalidIndexError # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
Traceback (most recent call last):
<ignore>
<ignore>InvalidIndexError
"""

import doctest
doctest.run_docstring_examples(test, globals())
这可以正常工作,但不能解决 <ignore>InvalidIndexError 前面的通配符。
def test():
"""
>>> import doctest
>>> doctest.ELLIPSIS_MARKER = '<ignore>'
>>> import pandas as pd
>>> raise pd.errors.InvalidIndexError # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
Traceback (most recent call last):
<ignore>
pandas.errors.InvalidIndexError
"""

import doctest
doctest.run_docstring_examples(test, globals())
注意 Pandas 版本是 1.1.3引用
  • https://docs.python.org/3/library/doctest.html#option-flags
  • How enable ellipsis when calling Python doctest?
  • Can I have an ellipsis at the beginning of the line in a Python doctest?
  • 最佳答案

    doctest要求异常(exception)以某种方式看待。来自 the docs :

    Each line of the traceback stack (if present) must be indented further than the first line of the example, or start with a non-alphanumeric character. The first line following the traceback header indented the same and starting with an alphanumeric is taken to be the start of the exception detail.


    (加粗体)
    这意味着如果您制作 ELLIPSIS_MARKER以字母数字开头,它将正常工作。这是一个使用 re.error 的示例:
    def test():
    """
    >>> import doctest
    >>> doctest.ELLIPSIS_MARKER = 'MODULE.'
    >>> import re
    >>> raise re.error(None) # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    MODULE.error: None
    """
  • 顺便一提:

    Note that tracebacks are treated very specially. In particular, in the rewritten example, the use of ... is independent of doctest's ELLIPSIS option. The ellipsis in that example could be left out, or could just as well be three (or three hundred) commas or digits, or an indented transcript of a Monty Python skit.



  • 对于上下文,这是一个使用两个省略号的示例,无一异常(exception):
    def test():
    r"""
    >>> print('foo\nbar\nbaz') # doctest: +ELLIPSIS
    foo
    ...
    ...
    """

    也就是说, IGNORE_EXCEPTION_DETAIL 可能是更好的解决方案。 (我自己才知道的。)

    When specified, an example that expects an exception passes if an exception of the expected type is raised, even if the exception detail does not match. For example, an example expecting ValueError: 42 will pass if the actual exception raised is ValueError: 3*14, but will fail, e.g., if TypeError is raised.

    It will also ignore the module name used in Python 3 doctest reports.


    (加粗体)
    例如:
    def test():
    """
    >>> import re
    >>> raise re.error(None) # doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
    ...
    error: foobar
    """
    请注意,此示例中忽略了异常模块和异常详细信息。这是故意的,以显示此解决方案的副作用。

    关于python - doctest 在省略号后忽略行的前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66306988/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com