How can I get the same/to pass parameters between decorator and decorated function?

99封情书 提交于 2019-12-25 17:18:54

问题


In this case I want to pass _source_dir_abs:str into decorator.

I tried to mimic the same process that Flask has for routing to pass parameter from decorator to function it is decorated. But this makes the parameter interpreted as a literal string and not as a variable.

@dec_check_abs("<_source_dir_abs>")
def walk_return_dir_nofolder(_source_dir_abs:str) -> list:
    w = walk(_source_dir_abs)
    d = [d for d, fol, fil in w if len(fol) == 0]
    return d

I tried with @dec_check_abs(_source_dir_abs) it returns an error of NameError: name 'source_dir_abs' is not defined

def dec_check_abs(*args_1):
    def decorator(_func:FT):
        def wrapper(*args_2, **kwargs):
            for i in args_1:
                if not check_abs(i):
                    """Raise warning."""
                    napw()

                    if _func.__annotations__["return"] == bool:
                        return False
                    else:
                        return None

            return _func(*args_2, **kwargs)
        return wrapper
    return decorator

回答1:


You've missed the point of decorators. You don't pass the argument in the decorator declaration; the function returned by that decorator accepts the arguments and can pass it on to the wrapped function if necessary.

So the decorator would look something like this:

def dec_check_abs(func):
    def wrapper(*args):
        if ... do your check on args here...:
            return func(*args)
        else:
            print('No, bad caller.')
    return wrapper

and you would just use it as:

@dec_check_abs
def walk_return_dir_nofolder(_source_dir_abs:str) -> list:
    ...



回答2:


I got my problem answered in a Reddit thread, https://www.reddit.com/r/learnpython/comments/5wnygi/how_can_i_get_the_same_parameters_between/.

Here is my code now.

def dec_check_abs(*params):
    def decorator(func):
        sig = inspect.signature(func)
        def wrapper(*args, **kwargs):
            bound = sig.bind(*args, **kwargs)
            for param in params:
                val = bound.arguments[param]

                if not check_abs(val):
                    """Raise warning."""
                    napw()

                    if sig.return_annotation == bool:
                        return False
                    else:
                        return None

            return func(*args, **kwargs)
        return wrapper
    return decorator

@dec_check_abs("source_dir_abs")
def check_isfolder(source_dir_abs:str) -> bool:
    return os.path.isdir(source_dir_abs)

@dec_check_abs("source_dir_abs", "dest_dir_abs")
def copy_fileorfolder(source_dir_abs:str, dest_dir_abs:str) -> bool:
    shutil.copy2(source_dir_abs, dest_dir_abs)
    return True


来源:https://stackoverflow.com/questions/42510272/how-can-i-get-the-same-to-pass-parameters-between-decorator-and-decorated-functi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!