I find myself frequently using Python's interpreter to work with databases, files, etc -- basically a lot of manual formatting of semi-structured data. I don't properly save and clean up the useful bits as often as I would like. Is there a way to save my input into the shell (db connections, variable assignments, little for loops and bits of logic) -- some history of the interactive session? If I use something like script I get too much stdout noise. I don't really need to pickle all the objects -- though if there is a solution that does that, it would be OK. Ideally I would just be left with a script that ran as the one I created interactively, and I could just delete the bits I didn't need. Is there a package that does this, or a DIY approach?


当前回答

%history命令非常棒,但不幸的是,它不能让您将%paste 'd保存到sesh中。要做到这一点,我认为你必须在开头执行%logstart(尽管我还没有确认这是有效的)。

我喜欢做的是

%history -o -n -p -f filename.txt

它将在每个输入(o, n和p选项)之前保存输出,行号和'>>>'。请在这里查看文档中的%history。

其他回答

除了IPython之外,类似的实用程序bpython还有一个“将您输入的代码保存到文件中”的特性

有%history魔法用于打印和保存输入历史记录(可选的还有输出)。

将当前会话存储到my_history.py文件中:

>>> %hist -f my_history.py

History IPython存储您输入的命令和它产生的结果。您可以使用上下方向键轻松地浏览以前的命令,或者以更复杂的方式访问历史记录。

您可以使用%history神奇函数来检查过去的输入和输出。以前会话的输入历史保存在数据库中,可以配置IPython以保存输出历史。

其他几个神奇的功能可以使用您的输入历史,包括%编辑,%重新运行,%召回,%宏,%保存和%pastebin。你可以使用标准格式来引用行:

%pastebin 3 18-20 ~1/1-5

这将占用当前会话中的第3行和第18 - 20行,以及前一会话中的第1-5行。

看到%历史吗?查看Docstring和更多示例。

另外,一定要探索%store magic在IPython中实现变量轻量级持久性的功能。

在IPython的数据库中存储变量、别名和宏。

d = {'a': 1, 'b': 2}
%store d  # stores the variable
del d

%store -r d  # Refresh the variable from IPython's database.
>>> d
{'a': 1, 'b': 2}

要在启动时自动恢复存储的变量,ipython_config.py中的specificc . storemagic .autorestore = True。

就Linux而言,人们可以使用脚本命令来记录整个会话。它是util-linux包的一部分,所以应该在大多数Linux系统上。你可以创建一个别名或函数,调用script -c python,并保存到typescript文件中。例如,这里有一个这样的文件的再版。

$ cat typescript                                                                                                      
Script started on Sat 14 May 2016 08:30:08 AM MDT
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'Hello Pythonic World'
Hello Pythonic World
>>> 

Script done on Sat 14 May 2016 08:30:42 AM MDT

这里的一个小缺点是,脚本会记录所有内容,甚至换行,无论何时点击退格等等。所以你可能想使用col来清理输出(参见这篇关于Unix&Linux Stackexchange的文章)。

对于那些使用spacemacs和python层附带的ipython的人来说,save magic会创建大量不必要的输出,因为不断的自动补全命令在后台工作,例如:

len(all_suffixes)
';'.join(__PYTHON_EL_get_completions('''len'''))
';'.join(__PYTHON_EL_get_completions('''all_substa'''))
len(all_substantives_w_suffixes)
';'.join(__PYTHON_EL_get_completions('''len'''))
';'.join(__PYTHON_EL_get_completions('''all'''))
';'.join(__PYTHON_EL_get_completions('''all_'''))
';'.join(__PYTHON_EL_get_completions('''all_w'''))
';'.join(__PYTHON_EL_get_completions('''all_wo'''))
';'.join(__PYTHON_EL_get_completions('''all_wor'''))
';'.join(__PYTHON_EL_get_completions('''all_word'''))
';'.join(__PYTHON_EL_get_completions('''all_words'''))
len(all_words_w_logograms)
len(all_verbs)

为了避免这种情况,只需保存ipython缓冲区,就像通常保存任何其他:spc f一样

保存XUbuntu的输入输出:

在XWindows中,从Xfce终端应用程序运行iPython 单击顶部菜单栏中的“终端”,在下拉菜单中查找保存内容

我发现这节省了输入和输出,一直追溯到我打开终端的时候。这不是特定于ipython的,它与ssh会话或从终端窗口运行的其他任务一起工作。