简体   繁体   English

解析Python模块文档字符串

[英]Parsing Python Module Docstrings

Is it possible to parse module-level docstrings with the AST? 是否可以使用AST解析模块级文档字符串?

I am working on a python documenter here and visiting the module tokens and grabbing the documentation does not yield the module-level docstring. 我正在这里研究一个python记录器并访问模块令牌并获取文档并不会产生模块级文档字符串。 So far, I've had to resort to importing the module and grabbing its __doc__ or using inspect to grab the documentation. 到目前为止,我不得不求助于导入模块并获取其__doc__或使用inspect来获取文档。

I looked into the pydoc module source for clues as to how other documenters parse docstrings, and discovered that pydoc ends up having to do basically the same thing as my documenter in order to grab the module-level strings. 我查看了pydoc模块源代码,了解其他文档管理器如何解析文档字符串,并发现pydoc最终必须与我的文档管理器基本相同才能获取模块级字符串。

Am I missing something? 我错过了什么吗? Is the only way to parse module-level docstrings through actually importing the module, or is it possible to parse the docstrings out of the AST directly? 是通过实际导入模块来解析模块级文档字符串的唯一方法,还是可以直接从AST解析文档字符串?

Maybe I miss-understand the question, but can't you just do this (python 2.7.1)? 也许我想念 - 理解这个问题,但你不能这样做(python 2.7.1)吗?

test file: 测试文件:

"""
DOC STRING!!
"""

def hello():
    'doc string'
    print 'hello'

hello()

Interactive session: 互动环节:

>>> M = ast.parse(''.join(open('test.py')))
>>> ast.get_docstring(M)
'DOC STRING!!'

You can also walk through the ast, looking for the slot the doc string would be in. 您还可以浏览ast,查找doc字符串所在的插槽。

>>> M._fields
('body',)
>>> M.body
[<_ast.Expr object at 0x10e5ac710>, <_ast.FunctionDef object at 0x10e5ac790>, <_ast.Expr object at 0x10e5ac910>]
>>> # doc would be in the first slot
>>> M.body[0]._fields
('value',)
>>> M.body[0].value
<_ast.Str object at 0x10e5ac750>
>>> # it contains a string object, so maybe it's the doc string
>>> M.body[0].value._fields
('s',)
>>> M.body[0].value.s
'\nDOC STRING!!\n'

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

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