按键绑定,以便按顺序交互式地执行来自Python解释器历史记录的命令? [英] Key binding to interactively execute commands from Python interpreter history in order?

查看:104
本文介绍了按键绑定,以便按顺序交互式地执行来自Python解释器历史记录的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我会在终端中运行Python交互式提示,然后导入新模块并测试其功能,从而在开发Python模块时对其进行测试.当然,由于我的代码正在开发中,因此存在错误,并且需要频繁地重新启动解释器.当我在重新启动之前只执行了几次解释器行时,这并不会太痛苦:解释器重新启动时我的键序列看起来像Up Up Enter Up Up Enter ...,但是将其推断为5个或更多要重复的语句,这会变得很严重痛苦的!

I sometimes test Python modules as I develop them by running a Python interactive prompt in a terminal, importing my new module and testing out the functionality. Of course, since my code is in development there are bugs, and frequent restarts of the interpreter are required. This isn't too painful when I've only executed a couple of interpreter lines before restarting: my key sequence when the interpreter restart looks like Up Up Enter Up Up Enter... but extrapolate it to 5 or more statements to be repeated and it gets seriously painful!

当然,我可以将测试代码放入用python -i执行的脚本中,但这是一项从头开始的活动,似乎对于打开文本编辑器而言似乎不是超出阈值" :) m真正需要解决的是bash shell中的Ctrl-r行为:在bash中依次执行10个命令序列涉及在历史记录中查找该命令(对于搜索,重复UpCtrl-r -两者都在Python中工作)解释程序外壳程序),然后只需按十次Ctrl-o键.我最喜欢的bash shell功能之一.

Of course I could put my test code into a script which I execute with python -i, but this is such a scratch activity that it doesn't seem quite "above threshold" for opening a text editor :) What I'm really pining for is the Ctrl-r behaviour from the bash shell: executing a sequence of 10 commands in sequence in bash involves finding the command in history (repeated Up or Ctrl-r for a search -- both work in the Python interpreter shell) and then just pressing Ctrl-o ten times. One of my favourite bash shell features.

问题在于,尽管许多其他的readline绑定功能(如Ctrl-aCtrl-eCtrl-rCtrl-s)在Python解释器中起作用,但Ctrl-o却没有.尽管也许 readline 模块可用于将此功能添加到python提示符.有什么建议吗?

The problem is that while lots of other readline binding functionality like Ctrl-a, Ctrl-e, Ctrl-r, and Ctrl-s work in the Python interpreter, Ctrl-o does not. I've not been able to find any references to this online, although perhaps the readline module can be used to add this functionality to the python prompt. Any suggestions?

是的,我知道使用交互式解释器并不是一种可以扩展到几行之外的开发方法!但是对于小型测试来说很方便,并且IMO的交互性可以帮助确定正在开发的API是自然,方便还是过于繁琐.因此,请限制对以下问题的技术性问题的答案:是否可以使readline历史记录步进在python中起作用,而不是一个人是否应该选择(有时)以这种方式工作的偏见!

Yes, I know that using the interactive interpreter is not a development methodology that scales beyond a few lines! But it is convenient for small tests, and IMO the interactiveness can help to work out whether a developing API is natural and convenient, or too heavy. So please confine the answers to the technical question of whether readline history-stepping can be made to work in python, rather than the side-opinion of whether one should or shouldn't choose to (sometimes) work this way!

自发布以来,我意识到我已经在使用readline模块来使某些Python解释器历史记录功能正常工作.但是,即使我将readline.parse_and_bind("Control-o: operate-and-get-next")放在PYTHONSTARTUP文件中,似乎也不支持Ctrl-o绑定到operate-and-get-next readline命令.

Since posting I realised that I am already using the readline module to make some Python interpreter history functions work. But the Ctrl-o binding to the operate-and-get-next readline command doesn't seem to be supported, even if I put readline.parse_and_bind("Control-o: operate-and-get-next") in my PYTHONSTARTUP file.

推荐答案

在python-ideas列表上进行了一些讨论之后,回答了我自己的问题:尽管某些readline文档中的信息矛盾,实际上operate-and-get-next函数实际上是定义为readline的bash扩展,而不是核心readline.

Answering my own question, after some discussion on the python-ideas list: despite contradictory information in some readline documentation it seems that the operate-and-get-next function is in fact defined as a bash extension to readline, not by core readline.

所以这就是为什么Ctrl-o在Python解释器会话中导入readline模块时,或者在尝试手动强制此绑定时,都不会表现出默认的预期行为:该功能在绑定的readline库中不存在

So that's why Ctrl-o neither behaves as hoped by default when importing the readline module in a Python interpreter session, nor when attempting to manually force this binding: the function doesn't exist in the readline library to be bound.

Google搜索显示 https://bugs.launchpad.net/ipython/+bug/382638 ,GNU readline维护人员在其上给出了 not 不将此功能添加到核心readline的理由,并说应由调用应用程序实现.他还说它的实现并不复杂",尽管我不知道如何(或者是否有可能)将其作为readline模块行为的纯Python扩展来实现.

A Google search reveals https://bugs.launchpad.net/ipython/+bug/382638, on which the GNU readline maintainer gives reasons for not adding this functionality to core readline and says that it should be implemented by the calling application. He also says "its implementation is not complicated", although it's not obvious to me how (or whether it's even possible) to do this as a pure Python extension to the readline module behaviour.

所以不,目前这是不可能的,除非bash的operate-and-get-next函数在Python readline模块或解释器本身中明确实现.

So no, this is not possible at the moment, unless the operate-and-get-next function from bash is explicitly implemented in the Python readline module or in the interpreter itself.

这篇关于按键绑定,以便按顺序交互式地执行来自Python解释器历史记录的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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