【问题标题】:Is there a Python equivalent to HighLine?是否有与 HighLine 等效的 Python?
【发布时间】:2013-02-04 10:55:51
【问题描述】:

HighLine 是一个用于简化控制台输入和输出的 Ruby 库。它提供了允许您请求输入并对其进行验证的方法。有没有在 Python 中提供类似功能的东西?

要显示 HighLine 的作用,请参见以下示例:

require 'highline/import'

input = ask("Yes or no? ") do |q|
  q.responses[:not_valid] = "Answer y or n for yes or no"
  q.default = 'y'
  q.validate = /\A[yn]\Z/i
end

它会询问“是或否?”并让用户输入一些内容。只要用户不输入 y 或 n(不区分大小写),它就会打印“Answer y or n for yes or no”并让用户再次输入答案。此外,如果用户按 Enter,则默认为 y。最后,完成后,输入将存储在input 中。这是用户首先输入“EH???”的示例结果然后是“y”:

是还是不是? |是|诶??? 回答 y 或 n 表示是或否 ?是的

在 Python 中是否有类似的简单方法可以做到这一点?

【问题讨论】:

  • 它看起来并不难实现。这只是一个正则表达式。请参阅re 模块。
  • 您在 Python 中的确切示例可在 Sergii Boiko's github 上找到
  • @BurhanKhalid 如果它回答了问题,您可能需要将其详细说明为答案。
  • stackoverflow.com/a/2827094/789593中描述了类似的方法

标签: python ruby console highline


【解决方案1】:

以下应该对你有类似的作用,尽管它与在 Ruby 中的询问风格并不完全相同。

class ValidInput(object):
    def __init__(self,prompt,default="",regex_validate="",
             invalid_response="",correct_response=""):
        self.prompt=prompt
        self.default=default
        self.regex_validate=regex_validate
        self.invalid_response=invalid_response
        self.correct_response=correct_response
    def ask(self):
        fin=""
        while True:
            v_in=raw_input(self.prompt)
            if re.match(v_in,self.regex_validate):
                fin=v_in
                print self.correct_response
                break
            else:
                print self.invalid_response
                if self.default=="break":
                      break
                continue
        return fin

你会像这样使用它:

my_input=ValidInput("My prompt (Y/N): ",regex_validate="your regex matching string here",
                    invalid_response="The response to be printed when it does not match the regex",
                    correct_response="The response to be printed when it is matched to the regex.")

my_input.ask()

【讨论】:

  • 你好像忘记了import recontinue 有必要吗?为什么if self.default=="break": \ break 之前有print self.invalid_response?您没有使用 Python 3 的任何特殊原因?
  • 你为什么用re.match而不是re.search(之前的only match the first character of a string)?此外,您似乎以错误的顺序将参数放置在 re.match 中。另外,我认为当一个模块可以做一个类时,我不认为它是有保证的。特别是当一个模块会使它的语义更简单时。此外,默认机制不适用于您的实现。此外,不需要 correct_response。为了解决这一切,我发布了an answer
【解决方案2】:

您可以使用 Python 3 模块 cliask。该模块的灵感来自the answer of IT Ninja,修复了some deficiencies in it,并允许通过正则表达式、谓词、元组或列表进行验证。

获取模块最简单的方法是通过pip 安装它(其他安装方式请参见readme):

sudo pip install cliask

然后您可以通过如下示例中的导入来使用该模块:

import cliask

yn = cliask.agree('Yes or no? ',
                  default='y')
animal = cliask.ask('Cow or cat? ',
                    validator=('cow', 'cat'),
                    invalid_response='You must say cow or cat')

print(yn)
print(animal)

下面是运行示例时会话的外观:

是还是不是? |是|诶??? 请输入“是”或“否” 是还是不是? |是|是的 牛还是猫?兔子 你必须说牛或猫 牛还是猫?猫 真的 猫

【讨论】:

  • 不错!我强烈推荐这个而不是我的,我或多或少只是提供了一个基本示例,说明您如何实现这一点。
猜你喜欢
  • 2012-11-21
  • 2012-08-26
  • 1970-01-01
  • 2013-06-07
  • 2011-07-14
  • 2010-12-19
  • 2011-02-18
  • 2015-07-28
  • 1970-01-01
相关资源
最近更新 更多