我正在 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
to the ChildClass
文档,所以我在那里有类似“使用 /run/ParentClass/generic_fun() 调用此函数”之类的语句。
有没有办法可以在文档字符串中放入诸如 之类的内容,Sphinx 会将其替换为它正在记录的实际类?
我想让代码看起来像:
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()(...)"?
理想情况下,我希望文档位于同一页面上,因此不同部分的替换字符串会有所不同。
我在寻找其他东西的同时找到了一种方法来做到这一点。
在打印消息之前,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|`
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)