6

これに似たSphinxのコードを文書化しています:

class ParentClass(object):
    
    def __init__(self):
        pass

    def generic_fun(self):
        """Call this function using /run/ParentClass/generic_fun()"""
        do_stuff()

class ChildClass(ParentClass):
    
    def specific_fun(self):
        """Call this function using /run/ChildClass/specific_fun()"""
        do_other_stuff()

ドキュメントに を追加した:inherited-membersのでChildClass、「/run/ParentClass/generic_fun() を使用してこの関数を呼び出す」などのステートメントが含まれています。

Sphinx が文書化する実際のクラスに置き換える <class_name> のようなものを docstring に入れる方法はありますか?

コードを次のようにしたいと思います。

class ParentClass(object):
    
    def __init__(self):
        pass

    def generic_fun(self):
        """Call this function using /run/<class_name>/generic_fun()"""
        do_stuff()

したがって、ChildClass セクションでは、Sphinx ドキュメントは「(...) using /run/ChildClass/generic_fun()(...)」と読み、ParentClass セクションは「(...) using /run/ParentClass/」と読みます。 generic_fun()(...)"?

理想的には、ドキュメントを同じページに配置したいので、置換文字列はセクションごとに異なります。

4

1 に答える 1

8

他のものを見ながら、これを行う方法を考え出しました。

メッセージを出力する前に autodoc が呼び出す関数があります。このコードを conf.py ファイルに追加しました。

def get_class_name(full_module_name):
    """
    Pull out the class name from the full_module_name
    """
    #split the full_module_name by "."'s
    return full_module_name.split('.')[-1]

def process_docstring(app, what, name, obj, options, lines):
    classname = get_class_name(name)

    # loop through each line in the docstring and replace |class| with
    # the classname
    for i in xrange(len(lines)):
        lines[i] = lines[i].replace('|class|', classname)

def setup(app):
    app.connect('autodoc-process-docstring', process_docstring)

| を使いたい トークンですが、グローバル置換用に予約されています。最初のファイルに次の行を追加することで、これを回避しました (つまり、コードは |class| を |class| に置き換えます)。

.. |class| replace:: `|class|`
于 2012-07-31T18:19:29.730 に答える