如何将ipython魔术输出存储到变量中

时间:2022-09-03 14:04:45

I am a python and Ipython beginner. This may be a trivial question. It is probably duplicated with other questions. However I do not know what key words I should search.

我是一个python和Ipython初学者。这可能是一个微不足道的问题。它可能与其他问题重复。但是我不知道应该搜索哪些关键词。

I have already known how to interactive with shell.

我已经知道如何与shell交互。

For example:

In [1]: a = !ls
In [2]: a
        ...same ls result as shell...
In [3]: type(a)
Out[3]: IPython.utils.text.SList

However, how to interactive with Ipython magic?

但是,如何与Ipython魔术互动?

For example

In [1]: a = %history -t 
        ...Ipython result...
In [2]: a
In [3]: type(a)
Out[3]: NoneType

2 个解决方案

#1


For the history command, specifically, the simplest solution is

对于历史命令,具体来说,最简单的解决方案是

In [243]: history -t -f history.txt
In [244]: with open('history.txt') as f:
   .....:     HIST = [l.strip() for l in f]
   .....:     

In [245]: len(HIST)
Out[245]: 258

In [246]: HIST[-1]
Out[246]: "get_ipython().magic(u'history -t -f history.txt')"

In [247]: 

Basically, dump it to a file and read it back in.

基本上,将其转储到文件中并将其读回。

This may seem a kludge, but I suspect it comes from the nature of IPython. It isn't actually an interpreter, but instead is a command line shell for the underlying interpreter. My suspicion is that the magic commands are handled inside IPython and do not go through the normal path of passing the command to the interpreter, capturing the output, and storing it in the command history as Out[n]. So it is not available for recall and assignment.

这可能看起来像是一块垃圾,但我怀疑它来自IPython的本质。它实际上不是解释器,而是底层解释器的命令行shell。我怀疑魔术命令是在IPython中处理的,并且没有通过将命令传递给解释器,捕获输出并将其作为Out [n]存储在命令历史中的正常路径。因此无法召回和分配。

The alternative is that get_ipython().magic simply returns None.

替代方案是get_ipython()。magic只返回None。

Either way, the screen output d=for %history is not available. You have to dump it to a file.

无论哪种方式,屏幕输出d = for%history都不可用。您必须将其转储到文件中。

It seems to vary per magic command. alias, for example, does return the screen output

它似乎因魔法命令而异。例如,别名确实返回屏幕输出

In [288]: a=%alias
Total number of aliases: 17

In [289]: a
Out[289]: 
[('cat', 'cat'),
 ('clear', 'clear'),
 ('cp', 'cp'),
 ('ldir', 'ls -F -G -l %l | grep /$'),
 ('less', 'less'),
 ('lf', 'ls -F -l -G %l | grep ^-'),
 ('lk', 'ls -F -l -G %l | grep ^l'),
 ('ll', 'ls -F -l -G'),
 ('ls', 'ls -F -G'),
 ('lx', 'ls -F -l -G %l | grep ^-..x'),
 ('man', 'man'),
 ('mkdir', 'mkdir'),
 ('more', 'more'),
 ('mv', 'mv'),
 ('rm', 'rm'),
 ('rmdir', 'rmdir'),
 (u'show', u'echo')]

In [290]: 

#2


Im working on an ipython reload project and want to have a quick way to select from previous %run statements. My solution was the following.

我正在进行一个ipython重装项目,并希望能够快速选择以前的%run语句。我的解决方案如下。

import os

histvar = os.popen("ipython -c 'history -g'").read()

#regex match / do stuff here

#1


For the history command, specifically, the simplest solution is

对于历史命令,具体来说,最简单的解决方案是

In [243]: history -t -f history.txt
In [244]: with open('history.txt') as f:
   .....:     HIST = [l.strip() for l in f]
   .....:     

In [245]: len(HIST)
Out[245]: 258

In [246]: HIST[-1]
Out[246]: "get_ipython().magic(u'history -t -f history.txt')"

In [247]: 

Basically, dump it to a file and read it back in.

基本上,将其转储到文件中并将其读回。

This may seem a kludge, but I suspect it comes from the nature of IPython. It isn't actually an interpreter, but instead is a command line shell for the underlying interpreter. My suspicion is that the magic commands are handled inside IPython and do not go through the normal path of passing the command to the interpreter, capturing the output, and storing it in the command history as Out[n]. So it is not available for recall and assignment.

这可能看起来像是一块垃圾,但我怀疑它来自IPython的本质。它实际上不是解释器,而是底层解释器的命令行shell。我怀疑魔术命令是在IPython中处理的,并且没有通过将命令传递给解释器,捕获输出并将其作为Out [n]存储在命令历史中的正常路径。因此无法召回和分配。

The alternative is that get_ipython().magic simply returns None.

替代方案是get_ipython()。magic只返回None。

Either way, the screen output d=for %history is not available. You have to dump it to a file.

无论哪种方式,屏幕输出d = for%history都不可用。您必须将其转储到文件中。

It seems to vary per magic command. alias, for example, does return the screen output

它似乎因魔法命令而异。例如,别名确实返回屏幕输出

In [288]: a=%alias
Total number of aliases: 17

In [289]: a
Out[289]: 
[('cat', 'cat'),
 ('clear', 'clear'),
 ('cp', 'cp'),
 ('ldir', 'ls -F -G -l %l | grep /$'),
 ('less', 'less'),
 ('lf', 'ls -F -l -G %l | grep ^-'),
 ('lk', 'ls -F -l -G %l | grep ^l'),
 ('ll', 'ls -F -l -G'),
 ('ls', 'ls -F -G'),
 ('lx', 'ls -F -l -G %l | grep ^-..x'),
 ('man', 'man'),
 ('mkdir', 'mkdir'),
 ('more', 'more'),
 ('mv', 'mv'),
 ('rm', 'rm'),
 ('rmdir', 'rmdir'),
 (u'show', u'echo')]

In [290]: 

#2


Im working on an ipython reload project and want to have a quick way to select from previous %run statements. My solution was the following.

我正在进行一个ipython重装项目,并希望能够快速选择以前的%run语句。我的解决方案如下。

import os

histvar = os.popen("ipython -c 'history -g'").read()

#regex match / do stuff here