在Python中将文本插入raw_input(以避免readine)的跨平台方法

发布于 2024-09-11 10:36:15 字数 943 浏览 3 评论 0原文

我有一个应用程序(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥醉 2024-09-18 10:36:15

行编辑功能绝非微不足道,难以重复。例如,只是诸如“读取下一个击键而不回显”之类的功能(甚至在您开始解释该击键的含义之前,以便重新定位光标并更改屏幕外观以及文本行的记住内容)正在编辑)不能简单地以跨平台方式完成:您需要 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 and curses 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.

蔚蓝源自深海 2024-09-18 10:36:15

为什么不使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文