我需要实现很多功能,这些功能都有这样的案例选择:
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')
我通常发现“多案例”模式在词典中表达得更清楚。你知道吗
我在这里假设,对于每种情况,我们都有一个不同的函数要调用,但是如果每个值都是您想要返回的整数,或者任何其他对象,那么模式就可以工作。你知道吗
例如,
CASES
字典清楚而紧凑地向代码的读者发出不同情况的信号。你知道吗另一种方法是使用“请求原谅,而不是允许”的模式。我发现在这个特殊的情况下,这并不像上面说的那么清楚。你知道吗
或者使用dictionary
.get
方法作为另一种方法,但是我认为对于这个特定场景,它没有第一种方法那么清晰。你知道吗使用字典将可能的输入映射到操作,如下所示:
演示:
相关问题 更多 >
编程相关推荐