使用 Sphinx 自动记录 Python

发布于 2024-08-02 18:41:51 字数 866 浏览 14 评论 0原文

这是上一个有关 Sphinx 的问题的通用版本

有没有一种方法可以递归地自动记录其中包含类和函数的模块或包?

我认为为每个函数添加 autofunctionautomodule 指令是愚蠢的;必须有一种方法来自动化该过程,否则我根本看不到使用 Sphinx 的意义。

澄清: 而不是: ,这需要我手动剪切和粘贴所有方法名称并相应地更新文档。

.. automodule:: segments.segments

    .. autoclass:: segments.segments.Seg

        .. automethod:: Seg.method_1

        .. automethod:: Seg.method_2

        .. automethod:: Seg.method_3

        .......

        .. automethod:: Seg.method_n

我想要一个类似以下的命令,

.. automodule:: segments.segments

    .. autoclass:: segments.segments.Seg

        .. MAGIC COMMAND: Automatically print the docstrings and signatures 
           of all Seg() methods.

This is a generalized version of a previous question regarding Sphinx.

Is there a way to recursively autodocument modules or packages which contain classes and functions within them?

I think it is silly to add the autofunction or automodule directive for each function; There must be a way to automate the process, otherwise I don't see the point of using Sphinx at all.

Clarification:
Instead of :

.. automodule:: segments.segments

    .. autoclass:: segments.segments.Seg

        .. automethod:: Seg.method_1

        .. automethod:: Seg.method_2

        .. automethod:: Seg.method_3

        .......

        .. automethod:: Seg.method_n

Which requires me to manually cut-and-paste all method names and update the documentation correspondingly, I want to have a command like:

.. automodule:: segments.segments

    .. autoclass:: segments.segments.Seg

        .. MAGIC COMMAND: Automatically print the docstrings and signatures 
           of all Seg() methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

呆橘 2024-08-09 18:41:52

为了使事情变得更容易,您可以使用此脚本(查看页面底部的最新版本):
http://bitbucket.org/birkenfeld/ sphinx/issue/98/add-the-autogenerate-script-to-sphinx

该脚本将解析您的包/模块并生成从文档字符串构建文档所需的所有其余文件。

我是这个剧本的原作者。

更新

此脚本现在是 Sphinx 1.1 的一部分,作为 apidoc

To make things easier you can use this script (look at the bottom of the page for the last version):
http://bitbucket.org/birkenfeld/sphinx/issue/98/add-the-autogenerate-script-to-sphinx

This script will parse your packages/modules and generate all the rest files necessary to build the doc from docstrings.

I'm the original author of this script.

UPDATE

This script is now part of Sphinx 1.1 as apidoc.

森罗 2024-08-09 18:41:52

Etienne 在他的回答中提到的脚本现已作为 sphinx-apidoc 集成到 Sphinx 中。它正是 OP 想要的。它预计在 Sphinx 1.1 中发布,或者可以从 Hg 存储库中获取:

https://bitbucket.org/birkenfeld/ sphinx

它对我来说效果很好。文档如下:

> sphinx-apidoc --help
Usage: sphinx-apidoc-script.py [options] -o <output_path> <module_path>
           [exclude_paths, ...]

Look recursively in <module_path> for Python modules and packages and create
a reST file with automodule directives per package in the <output_path>.

Etienne's script, mentioned in his answer, has now been integrated into Sphinx as sphinx-apidoc. It does exactly what the OP wants. It is slated for release in Sphinx 1.1, or is available from the Hg repo:

https://bitbucket.org/birkenfeld/sphinx

It works beautifully for me. The docs read thus:

> sphinx-apidoc --help
Usage: sphinx-apidoc-script.py [options] -o <output_path> <module_path>
           [exclude_paths, ...]

Look recursively in <module_path> for Python modules and packages and create
a reST file with automodule directives per package in the <output_path>.
岁月蹉跎了容颜 2024-08-09 18:41:52

我认为为每个函数添加 autofunction 或 automodule 指令是愚蠢的;必须有一种方法来自动化该过程,否则我根本看不到使用 Sphinx 的意义。

我建议使用 Epydoc,它专门用于从文档字符串生成文档。

I think it is silly to add the autofunction or automodule directive for each function; There must be a way to automate the process, otherwise I don't see the point of using Sphinx at all.

I would suggest Epydoc, which is specialized at generating documentation from docstrings.

星軌x 2024-08-09 18:41:52

您希望它比仅仅指定 automodule 更简单吗?即使对于大型库,输入所有模块名称也需要 5 分钟的工作量。

这样做的原因是 Sphinx 很难猜测需要记录什么。

您还可以编写 autopackage,它将搜索模块并使用 automodule 指令(如果 automodule 尚未执行此操作)。

You want it more simpler than just specifing automodule? Even for a large library, it's 5min amount of work to type all the module names.

The reason of doing so is because Sphinx hardly guesses what needs to be documented.

You could also write autopackage, that would search for modules and use automodule directive (if automodule does not do that already).

不如归去 2024-08-09 18:41:51

我们使用

.. automodule:: module
   :members:

We use

.. automodule:: module
   :members:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文