在Python中将文本插入raw_input(以避免readine)的跨平台方法
我有一个应用程序(CLI),其中包含编辑帐户信息的功能。它通过提出问题并将旧值放入答案中以使其可编辑来实现此目的。目前我正在使用 readline 模块来执行此操作。我想要另一种方法来做同样的事情来避免这个模块(我想允许应用程序在 Windows 以及 GNU/Linux 任何运行 python 的操作系统上运行所有功能) 。
我最初在以下网站上找到了以下代码(我对其进行了一些修改以适应函数),但由于该线程已有 4 年历史,我想我应该在这里询问。 http://bytes.com/topic/python/answers/ 471407-default-editable-string-raw_input
import readline
def editInput(question, old_value):
readline.set_startup_hook(lambda: readline.insert_text(old_value))
try:
new_value = raw_input(question)
finally:
readline.set_startup_hook(None)
return new_value
editInput('What\'s the answer? ', '32')
更新: 我不一定需要 readline 的替代方案(例如 PyReadline)。我只需要相同的结果。我更新了问题,提到我不一定需要它在 Windows 和 GNU/Linux 上运行,而是在 python 支持的任何操作系统上运行。所以基本上,只使用非常基本的函数(例如sys.stdin等)
I have an application (CLI) which includes the feature of editing account information. It does this by asking a question and putting in the old value in the answer so that it is editable. Currently I'm using the readline
module to do this. I'd like another way of doing the same thing that avoids this module (I want to allow the app to run with all features on Windows as well as GNU/Linux any operating system that python runs on).
I originally found the following code (I modified it a bit to fit into a function) at the following website but since that thread is 4 years old I figured I'd ask here. http://bytes.com/topic/python/answers/471407-default-editable-string-raw_input
import readline
def editInput(question, old_value):
readline.set_startup_hook(lambda: readline.insert_text(old_value))
try:
new_value = raw_input(question)
finally:
readline.set_startup_hook(None)
return new_value
editInput('What\'s the answer? ', '32')
UPDATE: I don't necessarily need an alternative for readline (such as PyReadline). I just need the same result. I updated the question to mention that I don't necessarily need it to run on Windows and GNU/Linux but on any OS supported by python. So basically, only use very basic functions (such as sys.stdin, etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
行编辑功能绝非微不足道,难以重复。例如,只是诸如“读取下一个击键而不回显”之类的功能(甚至在您开始解释该击键的含义之前,以便重新定位光标并更改屏幕外观以及文本行的记住内容)正在编辑)不能简单地以跨平台方式完成:您需要 Windows 上的
msvcrt
功能和 Unix-y 系统上的curses
功能 - 以及您对其工作的要求在 Python 支持的任何操作系统上,这个问题都显得巨大,并且无法满足。在考虑答案之前,您需要非常严格地界定它必须运行的操作系统/平台集,以及它必须绝对提供的行编辑功能的子集。如果您无法界定这些集合,那么答案很简单:您所要求的过于笼统,根本不可能。
Line editing functionality is far from trivial to duplicate. For example, just a functionality such as "read the next keystroke without echoing" (even before you start intepreting the meaning of that keystroke in order to reposition the cursor and alter the on-screen appearance as well as the remembered contents of the text line being edited) cannot be done simply in a cross-platform way: you need
msvcrt
functionality on Windows andcurses
functionality on Unix-y systems -- and your demand that it works on any OS supported by Python looms huge, and impossible to satisfy.You need to delimit very strictly the set of operating systems / platforms on which it must run, and the subset of line editing functionality it must absolutely provide, before an answer can be considered. If you just can't delimit those sets, then the answer is easy: what you're asking for is, in its excessive generality, simply impossible.
为什么不使用 PyReadline ? IPython 使用它来实现或多或少相同的功能,并且得到它们的良好支持。
事实上,从头开始。我刚刚尝试过,但不起作用。 pyreadline 可能不支持
set_startup_hook
。Why not use PyReadline? It is used by IPython for more or less the same functionality and is well supported by them.
Actually, scratch that. I have just tried it and it doesn't work. Likely, pyreadline doesn't support
set_startup_hook
.