码迷,mamicode.com
首页 > 其他好文 > 详细

定义用户可调整属性的装饰器

时间:2020-02-03 19:08:56      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:partial   efault   lse   The   main   运行   mes   定义   specified   

编写一个包装函数的装饰器函数,但具有用户可调整的属性,这些属性可用于在运行时控制装饰器的行为。

示例如下:

from functools import wraps, partial
import logging


# Utility decorator to attach a function as an attribute of obj
def attach_wrapper(obj, func=None):
    if func is None:
        return partial(attach_wrapper, obj)
    setattr(obj, func.__name__, func)
    return func


def logged(level, name=None, message=None):
    '''
    Add logging to a function. level is the logging level, name is the logger name, and message is the log message. If name and message aren't specified, they default to the function's module and name.
    '''
    def decorate(func):
        logname = name if name else func.__module__
        log = logging.getLogger(logname)
        logmsg = message if message else func.__name__
        
        @wraps(func)
        def wrapper(*args, **kwargs):
            log.log(level, logmsg)
            return func(*args, **kwargs)

        # Attach setter functions
        @attach_wrapper(wrapper)
        def set_level(newlevel):
            nonlocal level
            level = newlevel
        
        @attach_wrapper(wrapper)
        def set_message(newmsg):
            nonlocal logmsg
            logmsg = newmsg

        return wrapper
    return decorate


>>> import logging
>>> logging.basicConfig(level=logging.DEBUG) 
>>> add(2, 3)
DEBUG:__main__:add
5
>>> # Change the log message
>>> add.set_message('Add called') 
>>> add(2, 3)
DEBUG:__main__:Add called
5
>>> # Change the log level
>>> add.set_level(logging.WARNING) 
>>> add(2, 3)
WARNING:__main__:Add called
5
>>>

定义用户可调整属性的装饰器

标签:partial   efault   lse   The   main   运行   mes   定义   specified   

原文地址:https://www.cnblogs.com/jeffrey-yang/p/12256544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!