【发布时间】:2016-04-07 14:25:52
【问题描述】:
在 IPython 中,为用户定义的对象提供制表符补全相当容易:只需定义一个 __dir__ 方法,该方法将字符串列表返回给对象。
IPython 还为我们提供了一种使用方便的register_line_magic 实用程序来定义我们自己的自定义魔术函数的方法。在某些~/.ipython/profile_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
现在我的问题是:如何为这个神奇的功能提供自动完成功能?
根据this email,应该查看IPython.core.interactiveshell.InteractiveShell.init_completer() 以获取诸如%reset、'%cd' 等魔术函数完成器的示例...
但是,在与我的魔术函数定义相同的启动文件中,以下代码不起作用:
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 不会触发任何事情(函数中的打印语句表明该函数甚至没有被调用)。
有人可以指出一些关于如何从 Ipython 启动文件中定义此类用户魔术命令参数完成的文档或示例吗?
谢谢!
【问题讨论】:
标签: python ipython tab-completion ipython-magic