IPython自定义选项卡完成用户魔术功能

IPython自定义选项卡完成用户魔术功能,python,ipython,tab-completion,ipython-magic,Python,Ipython,Tab Completion,Ipython Magic,在IPython中,为用户定义的对象提供制表符补全是相当容易的:只需定义一个\uuuu dir\uuu方法,将字符串列表返回给对象 IPython还为我们提供了一种使用方便的register\u line\u magic实用程序定义自定义魔术函数的方法。在一些~/.ipython/profile\u default/startup/magictest.py: from IPython.core.magic import register_line_magic @register_line_ma

在IPython中,为用户定义的对象提供制表符补全是相当容易的:只需定义一个
\uuuu dir\uuu
方法,将字符串列表返回给对象

IPython还为我们提供了一种使用方便的
register\u line\u magic
实用程序定义自定义魔术函数的方法。在一些
~/.ipython/profile\u default/startup/magictest.py

from IPython.core.magic import register_line_magic

@register_line_magic
def show(dataType):
    # do something depending on the given `dataType` value
现在我的问题是:如何提供自动完成这个神奇的功能

根据,我们应该研究一个魔术函数补全符的例子,例如
%reset
,'%cd',等等

但是,在与定义my magic函数的启动文件相同的启动文件中,以下代码不起作用:

from IPython.core.interactiveshell import InteractiveShell

def show_complete():
     return ['dbs', 'databases', 'collections']

InteractiveShell._instance.set_hook(
    'complete_command', show_complete, str_key='%show')
在IPython shell中,键入
%show TAB
不会触发任何内容(函数中的print语句显示函数甚至没有被调用)

有人能给我指出一些文档或示例,说明如何从Ipython启动文件中定义此类用户magic命令参数吗

谢谢

您可以使用:

def load_ipython_extension(ipython):
    def apt_completers(self, event):
        """ This should return a list of strings with possible completions.

        Note that all the included strings that don't start with event.symbol
        are removed, in order to not confuse readline.
        """

        return ['update', 'upgrade', 'install', 'remove']

    ipython.set_hook('complete_command', apt_completers, re_key = '%%apt')

%%apt是一个神奇的关键字

键入
%%apt foo
,然后点击tab将产生如下事件:
名称空间(命令='%%apt',行='%%apt foo',符号='foo',文本直到光标='%%apt foo')
.Tx!这对我很有用,但是我也总是得到这些补全:
,如果不在或
-默认选项中?有没有办法压制他们?