在调用方法之前检测方法是否已装饰

cru*_*rky 6 python decorator python-decorators

我编写了一个Python流控制框架,其工作方式非常类似unittest.TestCase:用户创建一个派生自框架类的类,然后编写自定义task_*(self)方法.框架发现它们并运行它们:

###################
# FRAMEWORK LIBRARY
###################
import functools

class SkipTask(BaseException):
    pass

def skip_if(condition):
    def decorator(task):
        @functools.wraps(task)
        def wrapper(self, *args, **kargs):
            if condition(self):
                raise SkipTask()
            return task(self, *args, **kargs)
        return wrapper
    return decorator

class MyFramework(object):
    def run(self):
        print "Starting task"
        try:
            self.task()
        except SkipTask:
            print "Skipped task"
        except Exception:
            print "Failed task"
            raise
        else:
            print "Finished task"

#############
# USER SCRIPT
#############
class MyUserClass(MyFramework):
    skip_flag = True

    @skip_if(lambda self: self.skip_flag)
    def task(self):
        print "Doing something"

if __name__ == '__main__':
    MyUserClass().run()
Run Code Online (Sandbox Code Playgroud)

输出:

Starting task
Skipped task
Run Code Online (Sandbox Code Playgroud)

我想更改框架,以便每当条件@skip_ifTrue,时,包装器不会打印"Starting task".我试过这个,但它不起作用:

def skip_if(condition):
    def decorator(task):
        print "decorating "  + str(task)
        task.__skip_condition = condition
        return task
    return decorator

class MyFramework(object):
    def run(self):
        try:
            if self.task.__skip_condition():
                print "Skipped task"
                return
        except AttributeError:
            print str(self.task) + " is not decorated"
            pass

        print "Starting task"
        try:
            self.task()
        except Exception as e:
            print "Failed task: " + str(e)
            raise
        else:
            print "Finished task"
Run Code Online (Sandbox Code Playgroud)

输出:

decorating <function task at 0x194fcd70>
<bound method MyUserClass.task of <__main__.MyUserClass object at 0x195010d0>> is not decorated
Starting task
Doing something
Finished task
Run Code Online (Sandbox Code Playgroud)

为什么不跳过任务呢?

Mar*_*ers 6

您使用的是双下划线名称,该名称run方法中经历了私有名称修改.

单步执行调试器时,我得到:

AttributeError: "'function' object has no attribute '_MyFramework__skip_condition
Run Code Online (Sandbox Code Playgroud)

这里不要使用双下划线名称; 如果您将函数属性重命名为_skip_condition代码工作(假设您绑定条件函数或self显式传入):

def skip_if(condition):
    def decorator(task):
        print "decorating "  + str(task)
        task._skip_condition = condition
        return task
    return decorator

class MyFramework(object):
    def run(self):
        try:
            if self.task._skip_condition(self):
                print "Skipped task"
                return
        except AttributeError:
            print str(self.task) + " is not decorated"
            pass

        print "Starting task"
        try:
            self.task()
        except Exception as e:
            print "Failed task: " + str(e)
            raise
        else:
            print "Finished task"
Run Code Online (Sandbox Code Playgroud)

通过这些更改,输出变为:

decorating <function task at 0x1071a1b90>
Skipped task
Run Code Online (Sandbox Code Playgroud)