如何让 IPython 按类别组织制表符补全的可能性?

发布于 2024-07-12 09:27:57 字数 576 浏览 5 评论 0原文

当一个对象有数百个方法时,制表符补全很难使用。 通常,有趣的方法是由被检查对象的类而不是其基类定义或重写的方法。

如何让 IPython 将其选项卡完成可能性分组,以便首先检查对象的类中定义的方法和属性,然后是基类中的方法和属性?

看起来未记录的 inspect.classify_class_attrs(cls) 函数和 inspect.getmro(cls) 为我提供了我需要的大部分信息(这些最初是为了实现 python 的帮助(对象)功能)。

默认情况下,readline 按字母顺序显示补全,但用于显示补全的函数可以替换为 ctypes 或 Python 2.6 及更高版本中包含的 readline 模块。 我已经覆盖了 readline 的完成显示,并且效果很好。

现在我需要的只是一种方法,将每个类的信息(来自上面的 inspect.*)与每个实例的信息合并,按方法解析顺序对结果进行排序,漂亮的打印和分页。

为了额外的好处,最好存储所选的自动完成,并在下次尝试对同一对象进行自动完成时首先显示最受欢迎的选择。

When an object has hundreds of methods, tab completion is hard to use. More often than not the interesting methods are the ones defined or overridden by the inspected object's class and not its base classes.

How can I get IPython to group its tab completion possibilities so the methods and properties defined in the inspected object's class come first, followed by those in base classes?

It looks like the undocumented inspect.classify_class_attrs(cls) function along with inspect.getmro(cls) give me most of the information I need (these were originally written to implement python's help(object) feature).

By default readline displays completions alphabetically, but the function used to display completions can be replaced with ctypes or the readline module included with Python 2.6 and above. I've overridden readline's completions display and it works great.

Now all I need is a method to merge per-class information (from inspect.* per above) with per-instance information, sort the results by method resolution order, pretty print and paginate.

For extra credit, it would be great to store the chosen autocompletion, and display the most popular choices first next time autocomplete is attempted on the same object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幻梦 2024-07-19 09:27:57

由于我还没有使用 Python 2.6 或 3.0,并且没有 readline.set_completion_display_matches_hook(),我可以使用 ctypes 来设置 completion_display_func,如下所示:

from ctypes import *

rl = cdll.LoadLibrary('libreadline.so')

def completion_display_func(matches, num_matches, max_length):
    print "Hello from Python"
    for i in range(num_matches):
        print matches[i]

COMPLETION_DISPLAY_FUNC = CFUNCTYPE(None, POINTER(c_char_p), c_int, c_int)
hook = COMPLETION_DISPLAY_FUNC(completion_display_func)
ptr = c_void_p.in_dll(rl, 'rl_completion_display_matches_hook')
ptr.value = cast(hook, c_void_p).value

现在,当我按'tab' 来完成,我自己的函数打印完成列表。 这样就回答了“如何更改 readline 显示完成的方式”的问题。

Since I am not using Python 2.6 or 3.0 yet and don't have readline.set_completion_display_matches_hook(), I can use ctypes to set completion_display_func like so:

from ctypes import *

rl = cdll.LoadLibrary('libreadline.so')

def completion_display_func(matches, num_matches, max_length):
    print "Hello from Python"
    for i in range(num_matches):
        print matches[i]

COMPLETION_DISPLAY_FUNC = CFUNCTYPE(None, POINTER(c_char_p), c_int, c_int)
hook = COMPLETION_DISPLAY_FUNC(completion_display_func)
ptr = c_void_p.in_dll(rl, 'rl_completion_display_matches_hook')
ptr.value = cast(hook, c_void_p).value

Now, when I press 'tab' to complete, my own function prints the list of completions. So that answers the question 'how do I change the way readline displays completions'.

吖咩 2024-07-19 09:27:57

我认为这并不容易实现。 Ipython 中没有任何机制可以在任何情况下执行它。

最初我以为你可以修改 Ipython 的源代码来更改顺序(例如通过更改 genutils.py 中的 dir2() 函数)。 然而,它看起来像 readline 按字母顺序对您传递给它的完成进行排序,因此这行不通(至少在没有更多努力的情况下),尽管您可能可以完全排除基类上的方法。

I don't think this can be accomplished easily. There's no mechanism in Ipython to perform it in any case.

Initially I had thought you could modify Ipython's source to change the order (eg by changing the dir2() function in genutils.py). However it looks like readline alphabetically sorts the completions you pass to it, so this won't work (at least not without a lot more effort), though you could perhaps exclude methods on the base class completely.

腻橙味 2024-07-19 09:27:57

看起来我可以使用 readline.set_completion_display_matches_hook([function]) (Python 2.6 中的新功能)来显示结果。 完成器将像往常一样返回可能性列表,但也会在适用的情况下存储 inspect.classify_class_attrs(cls) 的结果。 completion_display_matches_hook 必须保存对完成程序的引用,以检索最新的完成列表以及我正在查找的分类信息,因为仅在其参数中接收匹配名称列表。 然后钩子以令人愉悦的方式显示完成列表。

It looks like I can use readline.set_completion_display_matches_hook([function]) (new in Python 2.6) to display the results. The completer would return a list of possibilities as usual, but would also store the results of inspect.classify_class_attrs(cls) where applicable. The completion_display_matches_hook would have to hold a reference to the completer to retrieve the most recent list of completions plus the classification information I am looking for because only receives a list of match names in its arguments. Then the hook displays the list of completions in a pleasing way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文