使用“from x import *”记录文件
sphinx 的 .. automodule:: 和其他自动功能可以用于记录包含 from x import *
语句的模块,而不包含来自导入模块的所有文档吗?
编辑: 根据 mzjn 的观点,只要导入方法的 __module__
属性与模块名称不同,就不应该记录它们。然而,对于我的一些模块来说,它们是。
我的 MLE 只是一个文件 test_doc.py
文件,其中包含以下行:
from pylab import *
和文档:
.. automodule:: agpy.test_doc
:members:
如果我在 test_doc.py
中包含此行:
print "beta.__module__:",beta.__module__
我会得到预期结果:
beta.__module__: None
Any知道发生了什么事吗?我可能把 conf.py
搞砸了吗?
编辑:根据 mzjn 的答案,一种解决方法是更改具有 __module__==None
的函数的 __module__
属性:
import pylab
from pylab import *
for k,v in pylab.__dict__.iteritems():
if hasattr(v,'__module__'):
if v.__module__ is None:
locals()[k].__module__ = 'pylab'
Can sphinx's .. automodule:: and other automatic features be used to document modules that include from x import *
statements without including all of the documentation from imported modules?
EDIT:
As per mzjn's point, as long as the imported methods' __module__
attribute are not the same as the module name, they shouldn't be documented. However, for some of my modules, they are.
my MLE is just a file test_doc.py
file with the following line:
from pylab import *
and the documentation:
.. automodule:: agpy.test_doc
:members:
If I include this line in test_doc.py
:
print "beta.__module__:",beta.__module__
I get the expected result:
beta.__module__: None
Any idea what's going on? Could I have screwed something up in conf.py
?
EDIT: A workaround, as per mzjn's answer, to change the __module__
attribute of those functions that have __module__==None
:
import pylab
from pylab import *
for k,v in pylab.__dict__.iteritems():
if hasattr(v,'__module__'):
if v.__module__ is None:
locals()[k].__module__ = 'pylab'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,那应该有效。来自文档:
更新:
问题似乎是许多
pylab
成员的__module__
属性为None
(C/Cython 模块中定义的成员mtrand
,据我所知)。mtrand 模块是 NumPy 的一部分。在幕后,
pylab.beta
(以及其他几个函数)是numpy.random.mtrand.RandomState
。我可以按如下方式重现文档问题:使用此源代码 (pylabtest.py)
和此源文档 (pylabtest.rst),
pylabtest.html 中的 Sphinx 输出包括
beta
和mzjn.
但如果
添加到 pylabtest.py,则仅记录
mzjn
。Yes, that should work. From the documentation:
Update:
The problem seems to be that the
__module__
attribute of manypylab
members isNone
(the members defined in the C/Cython modulemtrand
, as far as I can tell).The
mtrand
module is part of NumPy. Behind the scenes,pylab.beta
(and several other functions) is a method of the classnumpy.random.mtrand.RandomState
. I can reproduce the documentation issue as follows:With this source (pylabtest.py)
and this source documentation (pylabtest.rst)
the Sphinx output in pylabtest.html includes both
beta
andmzjn
.But if
is added to pylabtest.py, only
mzjn
is documented.