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?

推荐答案

IPython is extremely useful if you like using interactive sessions. For example for your use-case there is the %save magic command, you just input %save my_useful_session 10-20 23 to save input lines 10 to 20 and 23 to my_useful_session.py (to help with this, every line is prefixed by its number).

Furthermore, the documentation states:

This function uses the same syntax as %history for input ranges, then saves the lines to the filename you specify.

This allows for example, to reference older sessions, such as

%save current_session ~0/
%save previous_session ~1/

Look at the videos on the presentation page to get a quick overview of the features.

Python相关问答推荐

Gekko:Spring-Mass系统的参数识别

如何根据参数推断对象的返回类型?

如何访问所有文件,例如环境变量

使用groupby Pandas的一些操作

在Python中动态计算范围

Stacked bar chart from billrame

numpy.unique如何消除重复列?

搜索按钮不工作,Python tkinter

Python日志(log)模块如何在将消息发送到父日志(log)记录器之前向消息添加类实例变量

如何将一组组合框重置回无 Select tkinter?

为用户输入的整数查找根/幂整数对的Python练习

使用polars. pivot()旋转一个框架(类似于R中的pivot_longer)

Python协议不兼容警告

如何关联来自两个Pandas DataFrame列的列表项?

无法使用请求模块从网页上抓取一些产品的名称

为什么在生成时间序列时,元组索引会超出范围?

如何在基于时间的数据帧中添加计算值

了解如何让库认识到我具有所需的依赖项

IpyWidget Select 框未打开

Pip:卸载`-e`安装过程中安装的所有pkgs