简体   繁体   English

Sphinx 自动模块:如何引用同一模块中的类?

[英]Sphinx automodule: how to reference classes in same module?

I am trying to use the sphinx autodoc extension and specifically the automodule directive to automatically generate documentation for django app I am working on.我正在尝试使用 sphinx autodoc扩展,特别是automodule指令为我正在处理的 django 应用程序自动生成文档。 The problem is that I want to create internal references to different classes within the module, without having to use autoclass and autofunction on every single class/function within the project.问题是我想在模块中创建对不同类的内部引用,而不必在项目中的每个类/函数上使用autoclassautofunction For a source file like this:对于这样的源文件:

# source_code.py
class A:
    """docs for A
    """
    pass

class B:
    """docs for B with 
    :ref:`internal reference to A <XXXX-some-reference-to-A-XXXX>`
    """
    pass

I would like to be able to have a sphinx documentation file like this:我希望能够拥有这样的 sphinx 文档文件:

.. automodule: source_code

What reference can I use for XXXX-some-reference-to-A-XXXX?我可以为 XXXX-some-reference-to-A-XXXX 使用什么参考? Is there an easy way to accomplish this?有没有简单的方法来实现这一点? Thanks in advance for your help.在此先感谢您的帮助。

You can reference a class like this:您可以引用这样的类:

class B(object):
    """docs for B with reference to :class:`.A`"""
    pass

Sphinx will intelligently try and figure out what you're referencing. Sphinx 会聪明地尝试找出您所引用的内容。 If there are multiple classes with the name A , you might get a warning, but it should pick up the one in the current module.如果有多个名为A类,您可能会收到警告,但它应该在当前模块中选择一个。

Don't know if I understand the problem but this works flawlessly to me with autodoc, as per Cross-referencing Python objects不知道我是否理解这个问题,但根据交叉引用 Python 对象,这对我来说使用 autodoc 完美无缺

class FlowDirection(GeneralTable):
    '''
    Heat Flow Direction

    :cvar int id: database primary key
    :cvar unicode name: name 
    '''
    def __repr__(self):
        return u'<FlowDirection {0} instance at {1}>'.format(
                self.name, hex(id(self))).encode('utf-8')

    def __unicode__(self):
        return self.name

class AirCavityRes(GeneralTable):
    '''
    Air Cavity :term:`thermal resistance`

    :cvar flow_direction: heat flow direction
        (see :class:`FlowDirection`)
    :cvar int id: database primary key
    :cvar int if_fd: database foreign key to :class:`FlowDirection`
    :cvar float res: :term:`thermal resistance`
    :cvar float thick: thickness
    '''
    def __repr__(self):
        return u'<AirCavityRes {0} instance at {1}>'.format(
                self.res, hex(id(self)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM