如何将IPython历史记录到文本文件? [英] How to log IPython history to text file?

查看:210
本文介绍了如何将IPython历史记录到文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IPython 文档中进行了搜索和拖曳之后,一些代码,我似乎无法弄清楚是否可以存储命令历史记录(而不是输出日志)到文本文件,而不是SQLite数据库。 ipython --help-all 似乎表明该选项不存在。

After some searching and trawling through the IPython documentation and some code, I can't seem to figure out whether it's possible to store the command history (not the output log) to a text file rather than an SQLite database. ipython --help-all seems to indicate that this option doesn't exist.

这很好用于控制常用命令的版本,例如 .bash_history

This would be very nice for version controlling frequently used commands like in .bash_history.

编辑工作解决方案基于@minrk的答案。

Edit: Working solution based on @minrk's answer.

推荐答案

您可以模拟bash的通过在您的启动脚本之一中添加此行为(例如 $(ipython查找配置文件)/startup/log_history.py

You can emulate bash's behavior by adding this in one of your startup scripts (e.g. $(ipython locate profile)/startup/log_history.py:

import atexit
import os

ip = get_ipython()
LIMIT = 1000 # limit the size of the history

def save_history():
    """save the IPython history to a plaintext file"""
    histfile = os.path.join(ip.profile_dir.location, "history.txt")
    print("Saving plaintext history to %s" % histfile)
    lines = []
    # get previous lines
    # this is only necessary because we truncate the history,
    # otherwise we chould just open with mode='a'
    if os.path.exists(histfile):
        with open(histfile, 'r') as f:
            lines = f.readlines()

    # add any new lines from this session
    lines.extend(record[2] + '\n' for record in ip.history_manager.get_range())

    with open(histfile, 'w') as f:
        # limit to LIMIT entries
        f.writelines(lines[-LIMIT:])

# do the save at exit
atexit.register(save_history)

请注意,这会模拟bash / readline历史记录行为,因为它将在解释器崩溃等情况下失败。

Note that this emulates the bash/readline history behavior in that it will fail on an interpreter crash, etc.

要点

如果您真正想要的是只有一些手动的喜欢的命令可用于readline(完成,^ R搜索等),您可以对其进行版本控制,则此启动文件将允许您自己维护该文件,而这纯粹是除了IPython的实际命令历史记录之外,

If what you actually want is to just have a few manual favorite commands available to readline (completion, ^R search, etc.) that you can version control, this startup file will allow you to maintain that file yourself, which will be purely in addition to the actual command history of IPython:

import os

ip = get_ipython()

favfile = "readline_favorites"

def load_readline_favorites():
    """load profile_dir/readline_favorites into the readline history"""
    path = os.path.join(ip.profile_dir.location, favfile)
    if not os.path.exists(path):
        return

    with open(path) as f:
        for line in f:
            ip.readline.add_history(line.rstrip('\n'))

if ip.has_readline:
    load_readline_favorites()

将此内容拖放到您的 profile_default / startup / 目录中,然后编辑 profile_default / readline_favorites 或其他e您希望保留该文件,并且该文件将在每个IPython会话中显示在readline补全中。

Drop this in your profile_default/startup/ dir, and edit profile_default/readline_favorites, or anywhere you prefer to keep that file, and it will show up in readline completions, etc. on every IPython session.

这篇关于如何将IPython历史记录到文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆