当输入非法时,如何输出合法参数列表?

2024-10-06 13:01:53 发布

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

我需要实现很多功能,这些功能都有这样的案例选择:

def foo1(bar1):
    if bar1 == 'A':
        do something
    elif bar1 == 'B':
        do something
    elif ...
    ...
    else:
        raise ValueError('legal input of bar1 should be {}'.format(list_of_bar))

def foo2(bar2):
    if bar2 == 'A':
        do something
    elif bar2 == 'B':
        do something
    elif ...
    ...
    else:
        raise ValueError('legal input of bar2 should be {}'.format(list_of_bar))

'''

按照“不要自己重复”的说法,有没有办法避免重复最后一步提出错误并打印正确论点的清单?我以为装饰师会这么做,但不知道怎么做。提前谢谢。你知道吗

更新

我用inspect模块实现了它。但我还是希望能得到一些建议或更好的解决办法

import inspect
from functools import wraps
import re

def test_none(func):
    _code = inspect.getsource(func)
    _list = re.findall(r'if (\w+) == (\w+)', _code)
    assert all(_list[0][0] == name for name, case in _list)
    _arg = _list[0][0]
    _case = tuple(case for name, case in _list)

    @wraps(func)
    def wrapper(*args, **kwargs):
        results = func(*args, **kwargs)
        if results is None:
            raise ValueError(
                    'Legal value of \'{arg}\' should be anyone of {case}'.format(
                    arg=_arg, case=_case))
        return results
    return wrapper

@test_none
def foo(bar):
    if bar == 0:
        return 1
    elif bar == 1:
        return 2

测试示例:

foo(3)
ValueError: Legal value of 'bar' should be anyone of ('0', '1')

Tags: ofifdefbarbedosomethinglist
2条回答

我通常发现“多案例”模式在词典中表达得更清楚。你知道吗

我在这里假设,对于每种情况,我们都有一个不同的函数要调用,但是如果每个值都是您想要返回的整数,或者任何其他对象,那么模式就可以工作。你知道吗

例如,CASES字典清楚而紧凑地向代码的读者发出不同情况的信号。你知道吗

CASES = {
    'A': do_something,
    'B': do_something_else,
    ...
}

def foo(bar):
    if bar not in CASES:
        raise ValueError('legal input of bar should be {}'.format(list_of_bar))

    # do stuff
    CASES[bar]()

另一种方法是使用“请求原谅,而不是允许”的模式。我发现在这个特殊的情况下,这并不像上面说的那么清楚。你知道吗

def foo(bar):
    try:
        func = CASES[bar]
    except KeyError:
        raise ValueError(...)
    # do stuff
    func()

或者使用dictionary.get方法作为另一种方法,但是我认为对于这个特定场景,它没有第一种方法那么清晰。你知道吗

def foo(bar):
    func = CASES.get(bar)
    if func is None:
         raise ValueError(...)
    # do stuff
    func()

使用字典将可能的输入映射到操作,如下所示:

def foo(bar):
    def a():
        print('a():')

    def b():
        print('b():')

    def c():
        print('c():')

    actions = {'A': a, 'B': b, 'C': c}
    if bar in actions:
        actions[bar]()
    else:
        raise ValueError('legal input of bar should be {}'.format(sorted(actions.keys())))

演示:

>>> foo('A')
a():
>>> foo('Z')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "x.py", line 15, in foo
    raise ValueError('legal input of bar should be {}'.format(sorted(actions.keys())))
ValueError: legal input of bar should be ['A', 'B', 'C']

相关问题 更多 >