如何使IPython按类组织制表符补全的可能性? [英] How do I make IPython organize tab completion possibilities by class?

查看:68
本文介绍了如何使IPython按类组织制表符补全的可能性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我如何让IPython对其制表符完成可能性进行分组,以便首先在被检查对象的类中定义的方法和属性,然后在基类中进行定义?

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?

似乎未公开的inspect.classify_class_attrs(cls)函数与inspect.getmro(cls)一起为我提供了我所需的大部分信息(这些信息最初是为实现python的help(object)功能而编写的.)

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).

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

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.

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

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.

推荐答案

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

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

现在,当我按"tab"键完成时,我自己的函数会打印完成列表.这样就回答了如何更改阅读行显示补全的方式"这个问题.

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'.

这篇关于如何使IPython按类组织制表符补全的可能性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆