# Author:Allister.Liu # !/usr/bin/env python import time """ 装饰器:本质是函数(装饰其它函数),就是为其它函数添加附加功能。 原则: 1、不能修改被装饰的函数的源代码; 2、不能修改被装饰的函数的调用方式 实现装饰器只是储备: 1、函数即“变量”; 2、高阶函数; a、把一个函数名当做实参传递给另一个函数(再不修改源代码的同时,为其它函数附加新功能) b、返回值中包含函数名(不修改函数的调用方式) 3、嵌套函数。 高阶函数 + 嵌套函数 ==》 装饰器 """ # def start(): # print("in the start...") # # def test(func): # start_time = time.time() # func() # stop_time = time.time() # print("the func run time is %5f s" % (stop_time - start_time)) # start() """ 装饰器举例:test函数中,func(可以是一个函数名《函数名对应的是内存地址》)参数被传入函数start,即 test = func = start,其中func()也就是调用被传入的参数func即start() 所以,test()调用了start()的同时,也附加了打印执行时间的功能。 """ # test(start) """ def timmer(func): def warpper(): start_time = time.time() func() stop_time = time.time() print("the func run time is %4f 秒" % (stop_time - start_time)) return warpper def test01(): time.sleep(2) print("in the test01") test01 = timmer(test01) test01() 第一种: """ """ 第二种: @timmer : 等于 test01 = timmer(test01) test01() = timmer() def timmer(func): def warpper(): start_time = time.time() func() stop_time = time.time() print("the func run time is %4f 秒" % (stop_time - start_time)) return warpper @timmer def test01(): time.sleep(2) print("in the test01") test01() """ """ 最终版:适用于所有方法,有参数无参数都可以 """ def timmer(func): def warpper(*args, **kwargs): start_time = time.time() func(*args, **kwargs) stop_time = time.time() print("the func run time is %4f 秒" % (stop_time - start_time)) return warpper @timmer def test01(): time.sleep(2) print("in the test01") test01()
# Author:Allister.Liu # !/usr/bin/env python # -*- coding:utf-8-*- """ 需求:使用装饰器实现登录页面的验证 公司网站:每一个页面当做一个函数,其中主页(index)不需要登录,其余页面都需要登录后才能访问 user, pwd = "Allister", "123456" # 装饰器实现登录验证 def auth(func): def wrapper(*args, **kwargs): username = input("UserName:").strip() password = input("Password:").strip() if username == user and pwd == password: print("user logined sucess!") return func(*args, **kwargs) else: exit("Invalid username or password ") return wrapper # 无需登录 def index(): print("welcome to index page") # 需登录 @auth def manage(): print("welcome to manage page") # 需登录 @auth def bbs(): print("welcome to bbs page") index() manage() bbs() """ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ 需求优化:manage使用local验证,bbs使用ldap验证 """ user, pwd = "Allister", "123456" # 装饰器实现登录验证 def auth(auth_type): def auth_status(func): #manage at 0x000000000413DD08 def wrapper(*args, **kwargs): if auth_type == "local": username = input("UserName:").strip() password = input("Password:").strip() if username == user and pwd == password: print("user logined sucess!") return func(*args, **kwargs) else: exit("Invalid username or password ") elif auth_type == "ldap": print("ldap验证登录...") return wrapper return auth_status # 无需登录 def index(): print("welcome to index page") # 需登录 @auth(auth_type="local") # manage = auth(auth_type="local") :auth_status门牌,也就是manage=auth_status(manage), wrapper门牌 def manage(): print("welcome to manage page") # 需登录 @auth(auth_type="ldap") def bbs(): print("welcome to bbs page") # index() manage() # bbs()
# Author:Allister.Liu def decorator(func): print("decorator") def wrapper(*args, **kwargs): print("wrapper") func() return wrapper @decorator def test(): print("the test function") test()