在Python Sphinx中,有没有办法隐藏autodoc设置代码?

时间:2015-08-13 20:38:38

标签: python python-sphinx doctest autodoc

我正在使用Sphinx autodoc来记录我的Python代码。我的文档中的示例包含:

  • 不感兴趣的设置代码我不想在HTML文档中呈现
  • 应出现在文档中的代码

例如:

Then you process file `example.txt` with `frobnicate()`:

    >>> with open('example.txt', 'w') as f:
    ...     _ = f.write("first boring line\\n")
    ...     _ = f.write("second boring line\\n")

    >>> frobnicate('example.txt')
    True

我希望Sphinx autodoc隐藏不感兴趣的设置代码,以便Sphinx呈现的HTML输出仅包含:

Then you process file example.txt with frobnicate():

    >>> frobnicate('example.txt')
    True

显然我仍然希望doctest像往常一样执行整个例子。

1 个答案:

答案 0 :(得分:2)

您可以将不感兴趣的设置代码放在testsetup块中。

.. testsetup:: *

   with open('example.txt', 'w') as f:
       _ = f.write("first boring line\\n")
       _ = f.write("second boring line\\n")

Then you process file `example.txt` with `frobnicate()`:

>>> frobnicate('example.txt')
True