Skip to main content
 首页 » 编程设计

python之使用装饰器处理函数参数

2024年10月24日12小虾米

在核心,我想做的是采用一些看起来像这个未修饰的验证函数的函数:

def f(k: bool): 
    def g(n): 
        # check that n is valid 
        return n 
    return g 

并使它们看起来像这样修饰的验证函数:

@k 
def f(): 
    def g(n): 
        # check that n is valid 
        return n 
    return g 

这里的想法是 k 描述了所有实现函数的相同功能。

具体来说,这些函数都返回与 voluptuous validation framework 一起使用的“验证”函数.所以 f() 类型的所有函数都返回一个稍后由 Schema() 执行的函数。 k 实际上是 allow_none,也就是说一个标志,用于确定 None 值是否正常。一个非常简单的例子可能是这个示例使用代码:

x = "Some input value." 
y = None 
input_validator = Schema(f(allow_none=True)) 
x = input_validator(x)  # succeeds, returning x 
y = input_validator(y)  # succeeds, returning None 
input_validator_no_none = Schema(f(allow_none=False)) 
x = input_validator(x)  # succeeds, returning x 
y = input_validator(y)  # raises an Invalid 

在不更改示例使用代码的情况下 我试图通过将未修饰的验证函数更改为修饰的验证函数来获得相同的结果。举一个具体的例子,改变这个:

def valid_identifier(allow_none: bool=True): 
    min_range = Range(min=1) 
    validator = Any(All(int, min_range), All(Coerce(int), min_range)) 
    return Any(validator, None) if allow_none else validator 

对此:

@allow_none(default=True) 
def valid_identifier(): 
    min_range = Range(min=1) 
    return Any(All(int, min_range), All(Coerce(int), min_range)) 

这两者返回的函数应该是等价的。

我试图写的是这个,利用 decorator图书馆:

from decorator import decorator 
 
@decorator 
def allow_none(default: bool=True): 
    def decorate_validator(wrapped_validator, allow_none: bool=default): 
        @wraps(wrapped_validator) 
        def validator_allowing_none(*args, **kwargs): 
            if allow_none: 
                return Any(None, wrapped_validator) 
            else: 
                return wrapped_validator(*args, **kwargs) 
        return validator_allowing_none 
    return decorate_validator 

我有一个 unittest.TestCase 来测试它是否按预期工作:

@allow_none() 
def test_wrapped_func(): 
    return Schema(str) 
 
class TestAllowNone(unittest.TestCase): 
 
    def test_allow_none__success(self): 
        test_string = "blah" 
 
        validation_function = test_wrapped_func(allow_none=False) 
        self.assertEqual(test_string, validation_function(test_string)) 
        self.assertEqual(None, validation_function(None)) 

但我的测试返回以下失败:

    def validate_callable(path, data): 
        try: 
>           return schema(data) 
E           TypeError: test_wrapped_func() takes 0 positional arguments but 1 was given 

我试过调试这个,但无法让调试器真正进入装饰。我怀疑是因为命名问题,例如 this (very lengthy) blog post series 中提出的,test_wrapped_func 没有正确设置它的参数列表,因此装饰器甚至从未执行过,但它也可能完全是其他东西。

我尝试了一些其他变体。通过从 @allow_none 中删除函数括号:

@allow_none 
def test_wrapped_func(): 
    return Schema(str) 

我得到一个不同的错误:

>       validation_function = test_wrapped_func(allow_none=False) 
E       TypeError: test_wrapped_func() got an unexpected keyword argument 'allow_none' 

删除 @decorator 失败:

>       validation_function = test_wrapped_func(allow_none=False) 
E       TypeError: decorate_validator() missing 1 required positional argument: 'wrapped_validator' 

这是有道理的,因为 @allow_none 接受一个参数,因此逻辑上需要括号。替换它们会给出原始错误。

装饰器很微妙,我显然在这里遗漏了一些东西。这类似于柯里化(Currying)函数,但效果不佳。关于如何实现,我缺少什么?

请您参考如下方法:

我认为您将 allow_none=default 参数置于错误的嵌套级别。它应该在最里面的函数(包装器)上,而不是装饰器(中间层)。

尝试这样的事情:

def allow_none(default=True):    # this is the decorator factory 
    def decorator(validator):    # this is the decorator 
        @wraps(validator) 
        def wrapper(*args, allow_none=default, **kwargs):    # this is the wrapper 
            if allow_none: 
                return Any(None, validator) 
            else: 
                return validator(*args, **kwargs) 
        return wrapper 
    return decorator 

如果你不需要设置默认值,你可以摆脱最外层的嵌套,只在包装函数中将默认值设为常量(或者如果你的调用者总是传递一个值,则忽略它) .请注意,正如我在上面所写的那样,包装器的 allow_none 参数是一个仅限关键字的参数。如果您想将它作为位置参数传递,您可以将它移到 *args 之前,但这要求它是第一个位置参数,从 API 的角度来看这可能是不可取的。更复杂的解决方案可能是可能的,但对于这个答案来说有点过分了。