mbf.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:mbf 作者: Oliver2213 项目源码 文件源码
def timer(self, *t_args, **t_kwargs):
        """Method that returns a decorator to automatically set up a timer and associate it with a function to run at the specified time
        Code in this function gets executed immediately, not when the associated function runs.
        It dynamically accepts arguments and passes them off to a 'Timer' class instance
        """
        def decorator(timer_function):
            """This takes the timer's associated function as the only argument
            It defines the decorator and wrapper, as well as setting up the 'Timer' object and associating it with the decorated function.
            """
            if 'name' not in t_kwargs: # No name for this timer was provided; use function name
                t_kwargs['name'] = timer_function.__name__
            # Create an instance of the 'Timer' class
            new_timer = Timer(self.scheduler, *t_args, **t_kwargs)  # provide a reffrence to the scheduler and all wrapper arguments to this instance
            def wrapper(*args, **kwargs):
                """This function is what will be called in place of the decorated function;
                It takes the arguments given to it and passes them on to the provided function.
                """
                r = timer_function(*args, **kwargs) # call the original trigger function
                return r
            new_timer.add_function(wrapper) # Associate the wrapper with the timer object
            # add the timer to an internal list
            self.timers.append(new_timer)
            return wrapper
        return decorator
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号