python 9-5 如何在类中定义装饰器,在包裹函数中就可以持有实例对象,便于修改属性和功能

9-5 如何在类中定义装饰器
解决方案:
为了让装饰器在使用上更加灵活,可以把类的实例方法作为装饰器
此时在包裹函数中就可以持有实例对象,便于修改属性和功能

import logging
from time import  localtime,time,strftime,sleep
class CallingInfo(object):
    def __init__(self,name):
        log = logging.getLogger(name)
        log.setLevel(logging.INFO)
        fh = logging.FileHandler(name + '.log')
        log.addHandler(fh)
        log.info("start...".center(50,'-'))
        self.log = log
        self.formatter = '%(func)s ->[%(time)s - %(used)s - %(ncalls)s -]'

    def info(self,func):
        def wrapper(*args,**kwargs):
            wrapper.ncalls += 1
            lt = localtime()
            start = time()
            res = func(*args,**kwargs)
            used = time() - start
            info = {}
            info['func'] = func.__name__
            info['time'] = strftime('%x %X',lt)
            info['used'] = used
            info['ncalls'] = wrapper.ncalls
            msg = self.formatter % info
            self.log.info(msg)
            return res
        wrapper.ncalls = 0

        return wrapper
    def setFormater(self,formatter):
        self.formatter = formatter

    def turnOn(self):
        log.setLevel(logging.INFO)

    def turnOff(self):
        self.log.setLevel(logging.WARN)



cinfo1 = CallingInfo('mylog1')
cinfo2 = CallingInfo('myLog2')
cinfo1.setFormater('%(func)s ->[%(time)s - %(used)s - %(ncalls)s -]')
cinfo2.turnOff()

@cinfo1.info
def f():
    print "in f"

@cinfo1.info
def g():
    print 'in g'

@cinfo2.info
def h():
    print 'in h'
from random import choice
for _ in xrange(50):
    choice([f,g,h])()
    sleep(choice([0.5,1,1.5]))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值