Sphinx - 自动数据显示 str.__doc__
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的建议是:在模块文档字符串中记录变量,而不是尝试从
autodata
获取可用的内容。mymodule.py:
您还可以使用 信息字段:
这有效,排序的,但输出的格式不如用于记录类变量或函数参数的信息字段那么好。
更新:跟进有关
str
子类化的评论。这对你有用吗?My suggestion is this: document the variables in the module docstring instead of trying to get something usable from
autodata
.mymodule.py:
You could also use info fields:
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?鉴于在这种情况下很难让
sphinx.ext.autodoc
生成所需的文档字符串,因为:exec
评估的,您是否考虑过在 rst 文档本身中对文档进行硬编码?
Given that in this case is hard to let
sphinx.ext.autodoc
generate the desired documentation string because:exec
have you considered hardcoding the documentation in the
rst
document itself?我意识到你该如何解决它。
像这样编写:
使用 automodule 指令,Sphinx 将为您制作文档。
I realized how can you fix it.
Write smth like this:
Use automodule directive and Sphinx will make docs for you.