当没有跳过输入的选项时,如何绕过 Python3 中的输入提示?

发布于 2025-01-14 00:26:10 字数 585 浏览 3 评论 0原文

使用一个库,当我调用某个函数时,我将调用 foo() ,系统会提示我输入这样的内容:

def foo():
   if input("Proceed?") == 'yes':
        take_action()

我需要传递答案才能继续。

但我希望能够使用默认值循环 foo() 来提供提示。

问题是开发人员没有提供为提示提供默认值的选项。

理想情况下,他们会这样编写 foo()

def foo(default_response=None):
    if default_response == 'yes':
        take_action()
    elif input("Proceed?") == 'yes':
        take_action()

鉴于他们没有提供默认响应的选项,有没有办法让我循环 foo()并在不更改源代码的情况下自动提供输入?

Using a library that when I call a certain function that I will call foo() , I am prompted for input as such:

def foo():
   if input("Proceed?") == 'yes':
        take_action()

I am required to pass in an answer to continue.

But I would like to be able to loop over foo() with a default value to feed the prompt.

The problem is the developers did not provide an option to supply a default value for the prompt.

Ideally they would have written foo() like this:

def foo(default_response=None):
    if default_response == 'yes':
        take_action()
    elif input("Proceed?") == 'yes':
        take_action()

Given that they did not supply an option for a default response, is there a way for me to loop over foo() and provide the input automatically without changing the source code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

噩梦成真你也成魔 2025-01-21 00:26:10

一种解决方案是monkey patch库以临时替换输入功能。例如,如果我有如下所示的模块 foo.py

def take_action():
    print("doing something")


def foo():
    if input("Proceed? ") == "yes":
        take_action()

我可以这样编写 bar.py

import foo
from unittest import mock


def fake_input(default_response=None):
    '''Creates a replacement for the `input` function that will
    return a default response if one was provided.'''

    def _input(prompt):
        return default_response if default_response else input(prompt)

    return _input


with mock.patch("foo.input", new=fake_input("yes")):
    foo.foo()

foo.foo()

如果运行此代码,您将看到第一次调用 foo.foo()
使用默认输入,并在没有提示的情况下继续。第二次通话
to foo.foo() 会正常提示。

One solution is to monkey patch the library to temporarily replace the input function. For example, if I have module foo.py that looks like this:

def take_action():
    print("doing something")


def foo():
    if input("Proceed? ") == "yes":
        take_action()

I can write bar.py like this:

import foo
from unittest import mock


def fake_input(default_response=None):
    '''Creates a replacement for the `input` function that will
    return a default response if one was provided.'''

    def _input(prompt):
        return default_response if default_response else input(prompt)

    return _input


with mock.patch("foo.input", new=fake_input("yes")):
    foo.foo()

foo.foo()

If you run this code, you'll see that the first call to foo.foo()
uses a default input, and proceeds without prompting. The second call
to foo.foo() will prompt normally.

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