【发布时间】:2014-03-06 00:33:10
【问题描述】:
我在 Python 中有很多函数类型:
def foobar(one, two):
"""
My function.
:param int one: My one argument.
:param int two: My two argument.
:rtype: Something nice.
"""
return 100 + one + two
而且我需要解析文档字符串以获得类似于以下内容的字典:
{
'sdesc' : 'My function.',
'params' : [('one', 'My one argument.'), ('two', 'My two argument.')],
'rtype' : 'Something nice.'
}
我可以使用sphinx.util.docstrings.prepare_docstring如下:
>>> prepare_docstring(foobar.__doc__)
['My function.', ':param int one: My one argument.', ':param int two: My two argument.', ':rtype: Something nice.', '']
我可以创建自己的解析器,也许对参数和 rtype 使用正则表达式等等。
但是有没有更好的方法或更好的方法呢? sphinx.ext.autodoc 是怎么做到的?有关如何解析此类文档字符串的任何其他建议?
【问题讨论】:
-
由于Sphinx是开源的,你可以找到autodoc模块here。
标签: python python-sphinx docstring