• python装饰器在类中的实现


    class Test():
        xx = False
    
        def __init__(self):
            pass
    
        def test(func):
            def wrapper(self, *args, **kwargs):
                print(self.xx)
                return func(self, *args, **kwargs)
                
            return wrapper
    
        @test
        def test_a(self,a,b):
            print(f'ok,{a} {b}')
    
    if __name__=='__main__':
        t=Test()
        t.test_a('a','ab')

     新写的例子,封装了一个调用API,需要保存用户调用的一些信息

    def log(func):
        @functools.wraps(func)
        def wrapper(self, *args, **kwargs):
            start = time.time()
            result, success, err = None, None, None
            try:
                result = func(self, *args, **kwargs)
                success = True
            except Exception as e:
                err = e
            end = time.time()
            log_dict = {'tableName': args[0],
                        'operation': func.__name__,
                        'startTime': time.strftime(time_format, time.localtime(start)),
                        'endTime': time.strftime(time_format, time.localtime(end)),
                        'elapsedTime': int(end - start),
                        'success': success,
                        'errMsg': err,
                        'account': self.account}
            print(json.dumps(log_dict))
            return result
  • 相关阅读:
    Linxu指令--date,cal
    Linux指令--diff
    Linux指令--ln
    Linux指令--rcp,scp
    Linux指令--telnet
    Linux指令--traceroute,netstat,ss
    Linux指令--ping
    Linux指令--route
    Linux指令--ifconfig
    Linux指令--性能监控和优化命令相关指令
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/12401944.html
Copyright © 2020-2023  润新知