2016-04-07 57 views
1

在IPython中,它是相當容易爲用戶定義的對象提供製表完成:簡單地定義一個__dir__方法,返回字符串對象的列表。IPython的自定義標籤完成對用戶的神奇功能

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的外殼,輸入%show TAB觸發沒有(在功能顯示打印報表,該功能甚至沒有叫)。

可能有人點我了關於如何從IPython的啓動文件中定義這樣的用戶魔命令參數完成一些文檔或例子?

謝謝!

回答

1

您可以使用此:

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

%%傾向於是一個神奇的關鍵字

相關問題