Sphinx - 自动数据显示 str.__doc__

发布于 2024-12-25 13:21:28 字数 1348 浏览 3 评论 0原文

我正在尝试使用 Sphinx 记录我的 python 代码,但发现记录使用 exec 实例化的一些数据时出现问题;我有一个包含需要实例化的名称和值的表。

因此,在我的代码中,我写了类似的内容:

my_vars = [{'name': 'var1', 'value': 'first'},
           {'name': 'var2', 'value': 'second'}]

for var in my_vars:
    exec("{var[name]} = '{var[value]}'".format(var=var))

问题出在 Sphinx:因为我只想维护我使用 autodata 的源代码,所以我的 .rst 中的相应行> 文件是:

.. autodata:: mymodule.var1

.. autodata:: mymodule.var2

构建时给了我这个:

mymodule.var1 = 'first'
    str(string[, encoding[, errors]]) -> str

    Create a new string object from the given encoded string.
    encoding defaults to the current default string encoding.
    errors can be ‘strict’, ‘replace’ or ‘ignore’ and defaults to ‘strict’.

mymodule.var2 = 'second'
    str(string[, encoding[, errors]]) -> str

    Create a new string object from the given encoded string.
    encoding defaults to the current default string encoding.
    errors can be ‘strict’, ‘replace’ or ‘ignore’ and defaults to ‘strict’.

我认为自动数据会查找 var1.__doc__ 中的文档字符串,并找到 str.__doc__ (这是显示的消息前)。

我真的不知道该怎么做,我正在寻找一种不显示丑陋的文档字符串的方法(但仍然保持 mymodule.var1 = 'first')。

或者也许是更好的方式来显示我自己的文档,例如: var1 is this. (但我不知道把它放在哪里)。

I'm trying to document my python code with Sphinx, but I found a problem documenting some data instantiated with exec; I have a table with names and values that I need to instantiate.

So in my code I wrote something like:

my_vars = [{'name': 'var1', 'value': 'first'},
           {'name': 'var2', 'value': 'second'}]

for var in my_vars:
    exec("{var[name]} = '{var[value]}'".format(var=var))

The problem is with Sphinx: since I'd like to maintain just the source code I used autodata, the corrisponding lines from my .rst file are:

.. autodata:: mymodule.var1

.. autodata:: mymodule.var2

that when built gave me this:

mymodule.var1 = 'first'
    str(string[, encoding[, errors]]) -> str

    Create a new string object from the given encoded string.
    encoding defaults to the current default string encoding.
    errors can be ‘strict’, ‘replace’ or ‘ignore’ and defaults to ‘strict’.

mymodule.var2 = 'second'
    str(string[, encoding[, errors]]) -> str

    Create a new string object from the given encoded string.
    encoding defaults to the current default string encoding.
    errors can be ‘strict’, ‘replace’ or ‘ignore’ and defaults to ‘strict’.

I think autodata goes looking into var1.__doc__ for a doc string and there found str.__doc__ (that is the message shown before).

I really don't know what to do and I'm searching for a way of not showing that ugly doc string (but still maintaining mymodule.var1 = 'first').

Or maybe even better a way to show my own doc, like: var1 is this. (but I wouldn't know where to put it).

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

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

发布评论

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

评论(3

不念旧人 2025-01-01 13:21:28

我的建议是:在模块文档字符串中记录变量,而不是尝试从 autodata 获取可用的内容。

mymodule.py:

""" 
This module is...

Module variables:

* var1: var1 doc
* var2: var2 doc
"""

my_vars = [{'name': 'var1', 'value': 'first'},
           {'name': 'var2', 'value': 'second'}]

for var in my_vars:
    exec("{var[name]} = '{var[value]}'".format(var=var))

...
... 

您还可以使用 信息字段

"""

:var var1: var1 doc
:var var2: var2 doc
"""

这有效,排序的,但输出的格式不如用于记录类变量或函数参数的信息字段那么好。


更新:跟进有关 str 子类化的评论。这对你有用吗?

from collections import UserString   

my_vars = [{'name': 'var1', 'value': 'first', "doc": "var1 docstring"},
           {'name': 'var2', 'value': 'second', "doc": "var2 docstring"}]

for var in my_vars:
    code = """\
{0} = UserString('{1}')
{0}.__doc__ = '{2}'""".format(var["name"], var["value"], var["doc"])
    exec(code)

My suggestion is this: document the variables in the module docstring instead of trying to get something usable from autodata.

mymodule.py:

""" 
This module is...

Module variables:

* var1: var1 doc
* var2: var2 doc
"""

my_vars = [{'name': 'var1', 'value': 'first'},
           {'name': 'var2', 'value': 'second'}]

for var in my_vars:
    exec("{var[name]} = '{var[value]}'".format(var=var))

...
... 

You could also use info fields:

"""

:var var1: var1 doc
:var var2: var2 doc
"""

This works, sort of, but the output is not as nicely formatted as info fields used to document class variables or function parameters.


Update: following up on comments about str subclassing. Does this work for you?

from collections import UserString   

my_vars = [{'name': 'var1', 'value': 'first', "doc": "var1 docstring"},
           {'name': 'var2', 'value': 'second', "doc": "var2 docstring"}]

for var in my_vars:
    code = """\
{0} = UserString('{1}')
{0}.__doc__ = '{2}'""".format(var["name"], var["value"], var["doc"])
    exec(code)
凌乱心跳 2025-01-01 13:21:28

鉴于在这种情况下很难让 sphinx.ext.autodoc 生成所需的文档字符串,因为:

  • 代码是通过 exec 评估的,
  • 这些值可能不允许您覆盖文档字符串

您是否考虑过在 rst 文档本身中对文档进行硬编码?

.. data:: mymodule.var1

   var1 is this

.. data:: mymodule.var2

   var2 is that

Given that in this case is hard to let sphinx.ext.autodoc generate the desired documentation string because:

  • the code is evaluated through exec
  • the values might not let you overwrite the docstring

have you considered hardcoding the documentation in the rst document itself?

.. data:: mymodule.var1

   var1 is this

.. data:: mymodule.var2

   var2 is that
我们的影子 2025-01-01 13:21:28

我意识到你该如何解决它。
像这样编写:

x = 55
"""
x is varibble lala
"""

使用 automodule 指令,Sphinx 将为您制作文档。

I realized how can you fix it.
Write smth like this:

x = 55
"""
x is varibble lala
"""

Use automodule directive and Sphinx will make docs for you.

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