VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • 用python 装饰器打log

#! /usr/bin/env python
# coding=utf-8
 
from timeimport time
def logged(when):
    def log(f,*args,**kargs):
        print("called: function:%s,args:%r,kargs:%r"%(f,args,kargs))
    def pre_logged(f):
        def wrapper(*args,**kargs):
            log(f,*args,**kargs)
            return f(*args,**kargs)
    def post_logged(f):
        def wrapped(*args,**kargs):
            now=time()
            try:
                return f(*args,**kargs)
            finally:
                log(f,*args,**kargs)
                print("time delta:%s"%(time()-now))
        return wrapped
    try:
        #从这里开始调用
        return{"pre":pre_logged,"post":post_logged}[when]
    except Exception as e: