大多数Pythonic方法进行输入验证

2024-04-24 11:52:10 发布

您现在位置:Python中文网/ 问答频道 /正文

用Python进行用户输入验证的最“正确”的Python方法是什么?

我一直在使用以下工具:

while True:
    stuff = input("Please enter foo: ")
    try:
        some_test(stuff)
        print("Thanks.")
        break
    except SomeException:
        print("Invalid input.")

我想这本书很好读,但是我不禁想知道是否有一些内置函数或者我应该使用的东西。


Tags: 工具方法用户testtrueinputfoosome
3条回答

对“用户输入”进行这种验证的最有效方法是捕获适当的异常。

示例:

def get_user_input():
    while True:
        try:
            return int(input("Please enter a number: "))
        except ValueError:
            print("Invalid input. Please try again!")

n = get_user_input()
print("Thanks! You entered: {0:d}".format(n))

允许异常发生在它们所在的位置,并允许它们冒泡而不是隐藏它们,这样您就可以清楚地看到在Python回溯中发生了什么问题。

在这种情况下,验证用户输入——使用Python的Duck Typing并捕获错误。i、 e:如果它像管道一样,那一定是鸭子。(如果它的行为类似于int,那么它必须是int)。

我喜欢decorators将检查与其他输入处理分离开来。

#!/usr/bin/env python

def repeatOnError(*exceptions):
  def checking(function):
    def checked(*args, **kwargs):
      while True:
        try:
          result = function(*args, **kwargs)
        except exceptions as problem:
          print "There was a problem with the input:"
          print problem.__class__.__name__
          print problem
          print "Please repeat!"
        else: 
          return result
    return checked
  return checking

@repeatOnError(ValueError)
def getNumberOfIterations():
  return int(raw_input("Please enter the number of iterations: "))

iterationCounter = getNumberOfIterations()
print "You have chosen", iterationCounter, "iterations."

编辑:

decorator或多或少是现有函数(或方法)的包装器。它接受现有函数(在其@decorator指令下面表示)并返回一个“替换”。在我们的例子中,这个替换在循环中调用原始函数,并捕获执行此操作时发生的任何异常。如果没有异常发生,它只返回原始函数的结果。

有点复杂,但可能很有趣:

import re
from sys import exc_info,excepthook
from traceback import format_exc

def condition1(stuff):
    '''
    stuff must be the string of an integer'''
    try:
        i = int(stuff)
        return True
    except:
        return False

def condition2(stuff):
    '''
    stuff is the string of an integer
    but the integer must be in the range(10,30)'''
    return int(stuff) in xrange(10,30)

regx = re.compile('assert *\( *([_a-z\d]+)')

while True:
    try:
        stuff = raw_input("Please enter foo: ")
        assert(condition1(stuff))
        assert (  condition2(stuff))
        print("Thanks.")
        break
    except AssertionError:
        tbs = format_exc(exc_info()[0])
        funky = globals()[regx.search(tbs).group(1)]
        excepthook(exc_info()[0], funky.func_doc, None)

结果

Please enter foo: g
AssertionError: 
    stuff must be the string of an integer
Please enter foo: 170
AssertionError: 
    stuff is the string of an integer
    but the integer must be in the range(10,30)
Please enter foo: 15
Thanks.

是的。

编辑

我找到了一种简化的方法:

from sys import excepthook

def condition1(stuff):
    '''
    stuff must be the string of an integer'''
    try:
        int(stuff)
        return True
    except:
        return False

def another2(stuff):
    '''
    stuff is the string of an integer
    but the integer must be in the range(10,30)'''
    return int(stuff) in xrange(10,30)

tup = (condition1,another2)

while True:
    try:
        stuff = raw_input("Please enter foo: ")
        for condition in tup:
            assert(condition(stuff))
        print("Thanks.")
        break
    except AssertionError:
        excepthook('AssertionError', condition.func_doc, None)

相关问题 更多 >