使用“from x import *”记录文件

发布于 2024-12-22 07:47:47 字数 924 浏览 0 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

青瓷清茶倾城歌 2024-12-29 07:47:47

是的,那应该有效。来自文档

在设置了 Members 选项的 automodule 指令中,只有 __module__ 属性等于给定 automodule 的模块名称的模块成员才会被记录。这是为了防止记录导入的类或函数。


更新:

问题似乎是许多 pylab 成员的 __module__ 属性为 None (C/Cython 模块中定义的成员 mtrand,据我所知)。

mtrand 模块是 NumPy 的一部分。在幕后,pylab.beta(以及其他几个函数)是numpy.random.mtrand.RandomState。我可以按如下方式重现文档问题:

使用此源代码 (pylabtest.py)

from pylab import beta

def mzjn(x):
    """mzjn docstring"""
    return x

和此源文档 (pylabtest.rst),

Pylab test
==========

.. automodule:: pylabtest
    :members:

pylabtest.html 中的 Sphinx 输出包括 betamzjn.

但如果

beta.__module__ = "pylab"

添加到 pylabtest.py,则仅记录 mzjn

Yes, that should work. From the documentation:

In an automodule directive with the members option set, only module members whose __module__ attribute is equal to the module name as given to automodule will be documented. This is to prevent documentation of imported classes or functions.


Update:

The problem seems to be that the __module__ attribute of many pylab members is None (the members defined in the C/Cython module mtrand, 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 class numpy.random.mtrand.RandomState. I can reproduce the documentation issue as follows:

With this source (pylabtest.py)

from pylab import beta

def mzjn(x):
    """mzjn docstring"""
    return x

and this source documentation (pylabtest.rst)

Pylab test
==========

.. automodule:: pylabtest
    :members:

the Sphinx output in pylabtest.html includes both beta and mzjn.

But if

beta.__module__ = "pylab"

is added to pylabtest.py, only mzjn is documented.

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