2012-07-28 5 views
5

を置き換える:はスフィンクスを持って、私はこれに似ているスフィンクスのコード文書化していドキュメンテーション文字列のテキスト

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() 

を私はChildClassマニュアルを:inherited-membersを追加しましたので、「使用して、この関数を呼び出し/ ParentClass /実行するように私はそこに文を持っています/ generic_fun() "

ドキュメントストリングに何かを入れて、ドキュメント化している実際のクラスに置き換えるような方法はありますか?

私は クラスParentClass(オブジェクト)のようなコードを見てみたいものです。

def __init__(self): 
     pass 

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

のでChildClassセクションでは、Sphinxのドキュメントを読んでいました...使用して/実行/ ChildClass/generic_fun() ...そしてParentClassセクションは/ run/ParentClass/generic_fun()を使って読み込みます...?

理想的には、同じページにドキュメントを載せたいと思っています。そのため、置換文字列はセクションごとに異なります。

答えて

7

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

メッセージを印刷する前に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| replace:: `|class|` 
+0

'get_class_name'はどこに定義されていますか? – mzjn

+0

。これを同じファイルに追加しました。このコードブロックからちょうど分離されました。 –

関連する問題