简体   繁体   English

restructuredText,docstring和python交互式shell

[英]restructuredText, docstring and python interactive shell

I am using reStructuredText to document my code, so as to get nice offline HTML pages by means of epydoc. 我使用reStructuredText来记录我的代码,以便通过epydoc获得不错的离线HTML页面。

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. 唯一的缺点是当我使用Python交互式shell时,help()函数不解析文档字符串中的reST元数据,而是显示整个事物。

Is there a way to have help() to do some minimal parsing of the docstrings? 有没有办法让help()对文档字符串进行一些最小的解析?

I don't expect rendering of italic fonts or hyperlinks, but at least some minimal cleanup to increase readbility. 我不希望渲染斜体字体或超链接,但至少进行一些最小的清理以提高可读性。

The help() function gets added to the builtin namespace by the site module, which you can customize by creating a sitecustomize.py module somewhere on your path (apparently it's usually kept in site-packages). help()函数由site模块添加到内置命名空间,您可以通过在路径上的某个位置创建sitecustomize.py模块来自定义(显然它通常保存在site-packages中)。

Then in the sitecustomize.py file you add whatever customizations you want. 然后在sitecustomize.py文件中添加所需的任何自定义项。

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: 如果要更改help()函数本身的(明显)行为,请将help()函数包装在装饰器中,如:

def help_wrapper(func):
    def inner(*args):
        results = func(*args)
        return your_cleanup_function_here(results)
help = help_wrapper(help)

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. 我个人更喜欢稍微不同的解决方案,因为没有人知道你的清理功能将如何帮助输出不是用RestructuredText编写的。

So I would just create a wrapper function: 所以我只想创建一个包装函数:

def my_help(*args):
    return your_cleanup_function_here(help(*args))

This way you still have access to the original help() function if you need it. 这样,如果需要,您仍然可以访问原始的help()函数。

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. CAVEAT:在sitecustomize.py中执行操作时要小心,因为无论你在这里做什么都可能会影响整个解释器会话(以及每个解释器会话),这有时会导致意想不到的后果。

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

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