【问题标题】:In Python Sphinx, is there a way to hide autodoc setup code?在 Python Sphinx 中,有没有办法隐藏 autodoc 设置代码?
【发布时间】:2015-08-13 20:38:37
【问题描述】:

我正在使用 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 自动文档隐藏无趣的设置代码,以便从 Sphinx 呈现的 HTML 输出仅包含:

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

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

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

【问题讨论】:

    标签: python python-sphinx doctest autodoc


    【解决方案1】:

    您可以将无趣的设置代码放在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
    

    【讨论】:

    • 我必须在设置行的开头使用>>>(但仍打算使用设置行)才能使其正常工作
    猜你喜欢
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多