python-装饰器

#!/usr/local/bin/python3
# -*- coding:utf-8 -*-

import time
'''
#--------------------level 1---------------------#初始
def decorator(func):
    start_time=time.time()
    func()
    stop_time=time.time()
    print('time:%s'%(stop_time-start_time))

def test1():
    time.sleep(2)
    print('in the test1')

def test2():
    time.sleep(2)
    print('in the test1')

decorator(test1)
decorator(test2)

#--------------------level 2---------------------#改进
def timmer(func):       #timmer(test1)  func=test1
    def decorator():
        start_time = time.time()
        func()
        stop_time = time.time()
        print('time:%s' % (stop_time - start_time))
    return decorator

def test1():
    time.sleep(2)
    print('in the test1')

def test2():
    time.sleep(2)
    print('in the test1')

test1=timmer(test1)
test1()
test2=timmer(test2)
test2()

#---------------------level 3----------------------#引入装饰器符号@
def timmer(func):       #timmer(test1)  func=test1
    def decorator():
        start_time = time.time()
        func()
        stop_time = time.time()
        print('time:%s' % (stop_time - start_time))
    return decorator
@timmer     #相当于 test1=timmer(test1),这句代码执行完成后,test1已经变成了 decorator
def test1():
    time.sleep(2)
    print('in the test1')
@timmer
def test2():
    time.sleep(2)
    print('in the test1')

test1()
test2()

#--------------------level 4---------------------#新增功能:既可以传递参数,也可以不传递参数
def timmer(func):       #timmer(test1)  func=test1
    def decorator(*args,**kwargs):
        start_time = time.time()
        func(*args,**kwargs)
        stop_time = time.time()
        print('time:%s' % (stop_time - start_time))
    return decorator
@timmer     #相当于 test1=timmer(test1),这句代码执行完成后,test1已经变成了 decorator
def test1():
    time.sleep(2)
    print('in the test1')
@timmer
def test2():
    time.sleep(2)
    print('in the test1')
@timmer
def test3(name):
    print('name:',name)
test1()
test2()
test3("zhangyu")

#--------------------level 5---------------------#终极版(含参数的装饰器)
user = "zhangyu"
password = "332281"
def auth(auth_type):
    print("auth func:",auth_type)
    def out_wrapper(func):
        def wrapper(*args, **kwargs):
            print("wrapper:",*args,**kwargs)
            if auth_type=="local":
                user_name = input("name:-->")
                passwd = input("passwd:-->")
                if user_name == user and password == passwd:
                    print("user has passed")
                    res = func(*args, **kwargs)
                    print("---after---")
                    return res
                else:
                    print("invalid input")
                    exit()
            elif auth_type=="ldap":
                print("nonono")
                exit()
        return wrapper  # 注意:此处返回的应该是一个函数wrapper,而不是函数值wrapper()
    return out_wrapper
def index():
    print("welcome to index page")
@auth(auth_type="local")    #home=auth(
def home(): #此处home相当于调用wrapper  home=auth(home),return wrapper
    print("welcome to home page")
    return "from home"
@auth(auth_type="ldap")
def bbs():
    print("welcome to bbs page")
index()
print(home())
bbs()
'''

  

posted @ 2018-05-16 16:40  Octopuslnlzy  阅读(158)  评论(0编辑  收藏  举报