解析 Python 模块文档字符串
是否可以使用 AST 解析模块级文档字符串?
我正在开发一个 python 文档编写器 here 并访问模块令牌并获取文档不会产生模块 -级别文档字符串。到目前为止,我不得不导入模块并获取其 __doc__ 或使用 inspect
来获取文档。
我研究了 pydoc 模块 源代码以获取有关其他文档编写人员如何解析文档字符串的线索,并发现pydoc 最终必须做与我的文档编写器基本相同的事情才能获取模块级字符串。
我错过了什么吗?是通过实际导入模块来解析模块级文档字符串的唯一方法,还是可以直接从 AST 中解析文档字符串?
Is it possible to parse module-level docstrings with the 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. So far, I've had to resort to importing the module and grabbing its __doc__
or using inspect
to grab the documentation.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许我错过了理解这个问题,但是你不能这样做吗(python 2.7.1)?
测试文件:
交互式会话:
您还可以浏览 ast,查找文档字符串所在的插槽。
Maybe I miss-understand the question, but can't you just do this (python 2.7.1)?
test file:
Interactive session:
You can also walk through the ast, looking for the slot the doc string would be in.