restructedText、文档字符串和 python 交互式 shell
我正在使用 reStructuredText 来记录我的代码,以便通过 epydoc 获得漂亮的离线 HTML 页面。
结果非常出色。唯一的缺点是,当我使用 Python 交互式 shell 时,help() 函数不会解析文档字符串中的其余元数据,而是按原样显示整个内容。
有没有办法让 help() 对文档字符串进行一些最小的解析?
我不期望渲染斜体字体或超链接,但至少进行一些最小的清理以提高可读性。
I am using reStructuredText to document my code, so as to get nice offline HTML pages by means of epydoc.
Results are brilliant. The only drawback is that when I use the Python interactive shell, the help() function does not parse the reST metadata in the documentation strings, and instead it displays the whole thing as it is.
Is there a way to have help() to do some minimal parsing of the docstrings?
I don't expect rendering of italic fonts or hyperlinks, but at least some minimal cleanup to increase readbility.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
help()
函数由site
模块添加到内置命名空间,您可以通过在某个位置创建sitecustomize.py
模块来自定义该模块你的路径(显然它通常保存在站点包中)。然后在
sitecustomize.py
文件中添加您想要的任何自定义内容。您可以通过多种方式处理此问题:
如果您想更改
help()
函数本身的(明显)行为,请将帮助函数包装在装饰器中,例如:我个人更喜欢解决方案略有不同,因为不知道清理函数将做什么来帮助输出不是用RestructedText编写的内容。
因此,我只需创建一个包装函数:
这样,如果需要,您仍然可以访问原始的
help()
函数。注意:在 sitecustomize.py 中执行操作时要小心,因为您在这里所做的任何操作都可能会影响整个解释器会话(以及每个解释器会话),这有时会导致意想不到的后果。
The
help()
function gets added to the builtin namespace by thesite
module, which you can customize by creating asitecustomize.py
module somewhere on your path (apparently it's usually kept in site-packages).Then in the
sitecustomize.py
file you add whatever customizations you want.You could handle this a couple of ways:
If you want to change the (apparent) behavior of the
help()
function itself, wrap the help function in a decorator, something like:I would personally prefer a slightly different solution, because there's no telling what your cleanup function will do to help output that isn't written in RestructuredText.
So I would just create a wrapper function:
This way you still have access to the original
help()
function if you need it.CAVEAT: be cautious when doing things in sitecustomize.py, as whatever you do here will likely affect your entire interpreter session (and every interpreter session), which can sometimes lead to unintended consequences.