持久的Python命令行历史记录 [英] Persistent Python Command-Line History

查看:212
本文介绍了持久的Python命令行历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够向上箭头"到我在以前的Python解释器中输入的命令.我发现了readline模块,该模块提供的功能类似于:read_history_filewrite_history_fileset_startup_hook.我还不够聪明,无法将其付诸实践,所以有人可以帮忙吗?我对解决方案的想法是:

I'd like to be able to "up-arrow" to commands that I input in a previous Python interpreter. I have found the readline module which offers functions like: read_history_file, write_history_file, and set_startup_hook. I'm not quite savvy enough to put this into practice though, so could someone please help? My thoughts on the solution are:

(1)修改.login PYTHONSTARTUP以运行python脚本. (2)在该python脚本文件中执行以下操作:

(1) Modify .login PYTHONSTARTUP to run a python script. (2) In that python script file do something like:

def command_history_hook():
    import readline
    readline.read_history_file('.python_history')
command_history_hook()

(3)每当解释器退出时,都将历史记录写入文件.我想最好的方法是在启动脚本中定义一个函数,然后使用该函数退出:

(3) Whenever the interpreter exits, write the history to the file. I guess the best way to do this is to define a function in your startup script and exit using that function:

def ex():
    import readline
    readline.write_history_file('.python_history')
    exit()

但是必须用括号退出,这很烦人:ex().是否有一些python sugar可以允许ex(不带括号)运行ex函数?

It's very annoying to have to exit using parentheses, though: ex(). Is there some python sugar that would allow ex (without the parens) to run the ex function?

是否有更好的方法来使历史记录文件每次写入?预先感谢您提供的所有解决方案/建议.

Is there a better way to cause the history file to write each time? Thanks in advance for all solutions/suggestions.

另外,我可以看到两种架构选择.一种选择是拥有统一的命令历史记录.好处是简单(后跟的替代方法会在您的主目录中堆满很多文件.)缺点是,您在单独的终端中运行的解释器将填充彼此的命令历史记录,并且它们将覆盖彼此的历史记录. (这对我来说是可以的,因为我通常有兴趣关闭一个解释器并立即重新打开一个解释器以重新加载模块,在这种情况下,解释器的命令将被写入该文件.)一种可能的解决方案,用于在每个终端上维护单独的历史记录文件是为您创建的每个新终端编写一个环境变量:

Also, there are two architectural choices as I can see. One choice is to have a unified command history. The benefit is simplicity (the alternative that follows litters your home directory with a lot of files.) The disadvantage is that interpreters you run in separate terminals will be populated with each other's command histories, and they will overwrite one another's histories. (this is okay for me since I'm usually interested in closing an interpreter and reopening one immediately to reload modules, and in that case that interpreter's commands will have been written to the file.) One possible solution to maintain separate history files per terminal is to write an environment variable for each new terminal you create:

def random_key()
    ''.join([choice(string.uppercase + string.digits) for i in range(16)])

def command_history_hook():
    import readline
    key = get_env_variable('command_history_key')
    if key:
        readline.read_history_file('.python_history_{0}'.format(key))
    else:
        set_env_variable('command_history_key', random_key())

def ex():
    import readline
    key = get_env_variable('command_history_key')
    if not key:
        set_env_variable('command_history_key', random_key())
    readline.write_history_file('.python_history_{0}'.format(key))
    exit()

通过将随机密钥的长度从16减少到1,可以将目录中乱码的文件数量减少到36,但有可能发生重叠(2.8%的机会).

By decreasing the random key length from 16 to say 1 you could decrease the number of files littering your directories to 36 at the expense of possible (2.8% chance) of overlap.

推荐答案

我认为Python文档中的建议几乎涵盖了您想要的内容.在第13.3节末尾查看示例pystartup文件:

I think the suggestions in the Python documentation pretty much cover what you want. Look at the example pystartup file toward the end of section 13.3:

或查看此页面:

但是,对于提供所有这些功能以及更多功能的即用型交互式外壳,请看一下使用IPython:

But, for an out of the box interactive shell that provides all this and more, take a look at using IPython:

这篇关于持久的Python命令行历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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