在IPython中如何为%magics创建别名?

时间:2022-04-23 07:21:31

Let's say I want to create the alias %xed for %edit -x. How would I do it?

假设我想为%edit -x创建别名%xed。我该怎么办?

2 个解决方案

#1


2  

Update: The first response( below) does not accept parameters. So put this snippet at the end of the ipy_user_conf.py file ( it is in your home directory ).

更新:第一个响应(下面)不接受参数。因此,将此代码段放在ipy_user_conf.py文件的末尾(它位于您的主目录中)。

def ed_xed(self,arg):
    ip = self.api
    return ip.magic.im_class.magic_edit(ip.IP," -x %s "%arg)

ip.expose_magic('xed',ed_xed)

Before update: Does it has to be %magic? You can use the macro and store magic to reproduce this behavior without the magic %.

更新之前:它必须是%魔法吗?你可以使用宏和存储魔法来重现这种行为,而不需要魔法%。

In [5]: %edit -x
In [6]: macro xed 5
In [7]: store xed
In [8]: xed

for magic alias from the documentation ( %magic? ):

来自文档的魔术别名(%magic?):

You can also define your own aliased names for magic functions. In your ipythonrc file, placing a line like:

您还可以为魔术函数定义自己的别名。在你的ipythonrc文件中,放置一行如下:

execute IPYTHON.magic_pf = IPYTHON.magic_profile

执行IPYTHON.magic_pf = IPYTHON.magic_profile

will define %pf as a new name for %profile.

将%pf定义为%profile的新名称。

But I don't know how too add the parameter.

但我不知道如何添加参数。

#2


3  

The answer given above uses the old magic system. get_ipython().expose_magic is dead. You now just import and use decorators for all this.

上面给出的答案使用旧的魔法系统。 get_ipython()。expose_magic已经死了。您现在只需导入并使用装饰器。

See here for more details.

有关详细信息,请参见此处

#1


2  

Update: The first response( below) does not accept parameters. So put this snippet at the end of the ipy_user_conf.py file ( it is in your home directory ).

更新:第一个响应(下面)不接受参数。因此,将此代码段放在ipy_user_conf.py文件的末尾(它位于您的主目录中)。

def ed_xed(self,arg):
    ip = self.api
    return ip.magic.im_class.magic_edit(ip.IP," -x %s "%arg)

ip.expose_magic('xed',ed_xed)

Before update: Does it has to be %magic? You can use the macro and store magic to reproduce this behavior without the magic %.

更新之前:它必须是%魔法吗?你可以使用宏和存储魔法来重现这种行为,而不需要魔法%。

In [5]: %edit -x
In [6]: macro xed 5
In [7]: store xed
In [8]: xed

for magic alias from the documentation ( %magic? ):

来自文档的魔术别名(%magic?):

You can also define your own aliased names for magic functions. In your ipythonrc file, placing a line like:

您还可以为魔术函数定义自己的别名。在你的ipythonrc文件中,放置一行如下:

execute IPYTHON.magic_pf = IPYTHON.magic_profile

执行IPYTHON.magic_pf = IPYTHON.magic_profile

will define %pf as a new name for %profile.

将%pf定义为%profile的新名称。

But I don't know how too add the parameter.

但我不知道如何添加参数。

#2


3  

The answer given above uses the old magic system. get_ipython().expose_magic is dead. You now just import and use decorators for all this.

上面给出的答案使用旧的魔法系统。 get_ipython()。expose_magic已经死了。您现在只需导入并使用装饰器。

See here for more details.

有关详细信息,请参见此处