Python/Doctest Doctest复杂字符串

2024-05-15 09:53:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个学生,我在做一个作业,我应该对所有的内部功能进行修改。我的函数使用复杂字符串并返回复杂字符串,所以我不知道如何做。例如,我的函数“ProcessImports()”可以采用字符串:

%@import
blahblah
%@

然后返回:

^{pr2}$

我该怎么做医生检查?我还没有看到任何不返回类/结构、数字或其他简单表示形式的doctest示例(例如,“\n”似乎不起作用)。在

以下是函数ProcessImports(): http://pastebin.com/3JjnyKjK

任何帮助都将不胜感激!在

编辑:可能要忽略顶部尝试的doctest。那只是我在想办法弄清楚我是否能成功,但却失败了。在


Tags: 函数字符串import功能示例作业数字结构
1条回答
网友
1楼 · 发布于 2024-05-15 09:53:33

下面的例子展示了一种成功的方法;请记住,doctest只需要“看起来像”一个解释会话:

from textwrap import dedent

def function(s):
    """Testing doctests.

    >>> print function('''%@import
    ... blahblah
    ... %@''')
    <BLANKLINE>
    \\begin{shadedquoteBlueBar}
    \\fontsize{9pt}{9pt}
    \\begin{Verbatim}
    blahblah}
    \\end{Verbatim}
    \\end{shadedquoteBlueBar}
    \\noindent

    """
    s = dedent(r"""
               \begin{shadedquoteBlueBar}
               \fontsize{9pt}{9pt}
               \begin{Verbatim}
               blahblah}
               \end{Verbatim}
               \end{shadedquoteBlueBar}
               \noindent""")
    return s

注意docstring中函数输入的连续字符...。在

相关问题 更多 >