[英]Python decorator to find key exist, and pass it as argument
I was wondering if I could use a decorator to see if an input to a function is: 我想知道是否可以使用装饰器查看函数的输入是否为:
example: 例:
@get_values(['username', 'password'])
def log_me_in(username, password)
# login logic
if username == password:
return True
return False
ways to call it: 调用方式:
log_me_in(username = 10, password = 10)
>>>> True
log_me_in(10, 10)
>>>> True
log_me_in({'username': 10, 'password': 10})
>>>> True
log_me_in({'username': 10, 'password': 10, 'something': 10})
>>>> True
log_me_in({'username': 10, 'something': 10})
>>>> EXCEPTION
log_me_in({})
>>>> EXCEPTION
log_me_in([])
>>>> EXCEPTION
If you want to pass the values as positional arguments (according to the positions in the params list passed to the decorator): 如果要将值作为位置参数传递(根据传递给装饰器的params列表中的位置):
def getvalues(params):
getter = itemgetter(*params)
def deco(func):
@wraps(func)
def wrapper(*args, **kwargs):
if len(args) == 1 and not kwargs and isinstance(args[0], Mapping):
return func(*getter(args[0]))
else:
return func(*args, **kwargs)
return wrapper
return deco
If you want to pass them as keyword arguments instead: 如果您想将它们作为关键字参数传递:
def getvalues(params):
def deco(func):
@wraps(func)
def wrapper(*args, **kwargs):
if len(args) == 1 and not kwargs and isinstance(args[0], Mapping):
return func(**{key: value for key, value in args[0].items()
if key in params})
else:
return func(*args, **kwargs)
return wrapper
return deco
I had to invent my own rules to fill in all of the gaps in the specification, but I think they're somewhat reasonable. 我必须发明自己的规则来填补规范中的所有空白,但我认为它们有些合理。
Also, as I mentioned in a comment, I think it would be friendlier if getvalues
took its the parameter names as separate arguments instead of a list. 而且,正如我在评论中提到的那样,我认为如果getvalues
将其参数名称作为单独的参数而不是列表来使用,将会更加友好。 To fix that, just change the first line to def getvalues(*params):
. 要解决此问题,只需将第一行更改为def getvalues(*params):
It is possible, yes. 有可能,是的。 But is it necessary? 但是有必要吗? You could get a similar result by passing in keyword arguments with **
: 通过使用**
传递关键字参数,可以得到类似的结果:
>>> log_me_in(**{'username': 10, 'password': 10})
True
The one difference is that passing something
would not be allowed. 一个区别是不允许传递something
。 Python errors out on unrecognized keyword arguments. Python错误出现在无法识别的关键字参数上。 See: 看到:
>>> log_me_in(username=10, password=10)
True
>>> log_me_in(10, 10)
True
>>> log_me_in(**{'username': 10, 'password': 10})
True
>>> log_me_in(**{'username': 10, 'password': 10, 'something': 10})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: log_me_in() got an unexpected keyword argument 'something'
>>> log_me_in(**{'username': 10, 'something': 10})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: log_me_in() got an unexpected keyword argument 'something'
>>> log_me_in(**{})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: log_me_in() missing 2 required positional arguments: 'username' and 'password'
>>> log_me_in(*[])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: log_me_in() missing 2 required positional arguments: 'username' and 'password'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.