3

IPython では、ユーザー定義オブジェクトにタブ補完を提供するのはかなり簡単__dir__です。文字列のリストをオブジェクトに返すメソッドを定義するだけです。

register_line_magicIPython は、便利なユーティリティを使用して独自のカスタム マジック関数を定義する方法も提供します。いくつかで~/.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

今私の質問は: この魔法の関数にオートコンプリートを提供するにはどうすればよいですか?

この電子メールによると、「%cd」IPython.core.interactiveshell.InteractiveShell.init_completer()などのマジック関数コンプリータの例を調べる必要があります...%reset

ただし、私のマジック関数が定義されているものと同じスタートアップ ファイルでは、次のコードは機能しませんでした。

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しても何もトリガーされません (関数内の print ステートメントは、関数が呼び出されていないことを示しています)。

このようなユーザー マジック コマンド パラメーターの補完を Ipython スタートアップ ファイル内から定義する方法について、ドキュメントや例を誰かに教えてもらえますか?

ありがとう!

4

1 に答える 1

4

これを使用できます:

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 は魔法のキーワードです

于 2017-01-19T08:58:17.573 に答える