【问题标题】:Sphinx documenting dictionary content (a module constant)Sphinx 记录字典内容(模块常量)
【发布时间】:2018-07-11 23:01:04
【问题描述】:

这和How to document a module constant in Python?有点关系,但不一样。

我在模块中有一个常量(它是一个字典):

possiblestringencodings = dict(
    StringsAsBytes=1,
    ascii=1,
    utf8=1, utf_8=1, U8=1,
    utf16=2, utf_16=2, U16=2, utf_16_be=2, utf_16_le=2,
    utf32=4, utf_32=4, U32=4, utf_32_be=4, utf_32_le=4,
)

readthedocs 页面有 (see autodata docs):

.. autodata:: construct.possiblestringencodings

但是,这会从 dict docstring(它的 ctor)生成 docstring。如何记录该字典的内容,仅使用 Sphinx 记录其项目?

如果有人想测试修补它,只需 fork 存储库并在 docs/ 文件夹中运行“make html”。 https://github.com/construct/construct/blob/1b53d9122a2c652db64c6558d101caee5bbbab3a/construct/core.py#L1280 https://github.com/construct/construct/blob/1b53d9122a2c652db64c6558d101caee5bbbab3a/docs/api/strings.rst

【问题讨论】:

    标签: python dictionary constants python-sphinx


    【解决方案1】:

    字典数据成员没有文档字符串,因此您可以从 dict 类中获取一个。

    在定义前添加一个空的“documentation comment”(或紧随其后的文档字符串),您将只能在输出中获得字典项。

    #:
    possiblestringencodings = dict(
        StringsAsBytes=1,
        ascii=1,
        utf8=1, utf_8=1, U8=1,
        utf16=2, utf_16=2, U16=2, utf_16_be=2, utf_16_le=2,
        utf32=4, utf_32=4, U32=4, utf_32_be=4, utf_32_le=4,
    )
    

    您还需要完全限定“核心”模块:

    .. autodata:: construct.core.possiblestringencodings
    

    【讨论】:

    • 谢谢你的帮助,你做了很多。我安装了 sphinx 并在本地运行“make html”,确实该页面已修复。出于某种原因,它只读取了未通过此修复的文档。你能编辑你的答案,我们都删除它下面的 cmets 吗?
    • 完成,发布到 repo 的链接。
    猜你喜欢
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多