简体   繁体   English

如何让IPython按类组织标签完成的可能性?

[英]How do I make IPython organize tab completion possibilities by class?

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? 如何让IPython对其选项卡完成的可能性进行分组,以便首先在被检查对象的类中定义的方法和属性,然后是基类中的方法和属性?

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). 它看起来像未记录的inspect.classify_class_attrs(cls)函数以及inspect.getmro(cls)给我提供了我需要的大部分信息(这些信息最初用于实现python的help(object)功能)。

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. 默认情况下,readline按字母顺序显示完成,但用于显示完成的函数可以用ctypes或Python 2.6及更高版本中包含的readline模块替换。 I've overridden readline's completions display and it works great. 我已经覆盖了readline的完成显示,效果很好。

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. 现在我需要的是一种方法来合并每个类信息(来自上面的inspect.* )和每个实例信息,按方法分辨率顺序排序结果,漂亮的打印和分页。

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. 对于额外的功劳,最好存储所选的自动完成,并在下次尝试对同一对象进行自动完成时首先显示最常用的选项。

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: 由于我还没有使用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

Now, when I press 'tab' to complete, my own function prints the list of completions. 现在,当我按“tab”完成时,我自己的功能会打印完成列表。 So that answers the question 'how do I change the way readline displays completions'. 因此,它回答了“如何更改readline显示完成的方式”的问题。

I don't think this can be accomplished easily. 我认为这不容易实现。 There's no mechanism in Ipython to perform it in any case. 在任何情况下,Ipython都没有机制来执行它。

Initially I had thought you could modify Ipython's source to change the order (eg by changing the dir2() function in genutils.py). 最初我以为你可以修改Ipython的源来改变顺序(例如通过改变genutils.py中的dir2()函数)。 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. 但是它看起来像readline的字母顺序排列你传递给它的落成量,所以这是不行的(至少在没有更多的精力 ),但你也许可以排除在基类中的方法完全。

It looks like I can use readline.set_completion_display_matches_hook([function]) (new in Python 2.6) to display the results. 看起来我可以使用readline.set_completion_display_matches_hook([function]) (Python 2.6中的新增readline.set_completion_display_matches_hook([function]) )来显示结果。 The completer would return a list of possibilities as usual, but would also store the results of inspect.classify_class_attrs(cls) where applicable. 完成者将像往常一样返回一系列可能性,但也会在适用的情况下存储inspect.classify_class_attrs(cls)的结果。 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. completion_display_matches_hook必须保存对完成者的引用,以检索最新的完成列表以及我正在寻找的分类信息,因为它只在其参数中接收匹配名称列表。 Then the hook displays the list of completions in a pleasing way. 然后钩子以令人愉悦的方式显示完成列表。

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

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