一 装饰器
'''
1、什么是装饰器
器指的是工具,而程序中的函数就具备某一功能的工具
装饰指的是为被装饰器对象添加额外功能
就目前的知识来看:
定义装饰器就是定义一个函数,只不过该函数的功能是用来为
其他函数添加额外的功能
其实:
装饰器本身其实可以是任意可调用的对象
被装饰的对象也可以是任意可调用的对象
2、为什么要用装饰器
软件的维护应该遵循开放封闭原则
开放封闭原则指的是:
软件一旦上线运行后对修改源代码是封闭的,对扩展功能的是开放的
这就用到了装饰器
装饰器的实现必须遵循两大原则:
1、不修改被装饰对象的源代码
2、不修改被装饰对象的调用方式
装饰器其实就在遵循1和2原则的前提下为被装饰对象添加新功能
3、如何用装饰器
'''
# import time
#
# def index():
# start=time.time()
# print('welcom to index')
# time.sleep(3)
# stop=time.time()
# print('run time is %s' %(stop-start))
# index()
# import time
#
# def index():
# print('welcom to index')
# time.sleep(3)
#
# def f2():
# print('from f2')
# time.sleep(2)
#
# start=time.time()
# index()
# stop=time.time()
# print('run time is %s' %(stop-start))
#
# start=time.time()
# f2()
# stop=time.time()
# print('run time is %s' %(stop-start))
# import time
#
# def index():
# print('welcom to index')
# time.sleep(3)
#
# def timmer(func):
# start=time.time()
# func()
# stop=time.time()
# print('run time is %s' %(stop-start))
#
# timmer(index)
'''
import time
def index():
print('welcom to index')
time.sleep(3)
def timmer(func): #func=最原始的index
# func=index
def inner():
start=time.time()
func()
stop=time.time()
print('run time is %s' %(stop-start))
return inner
# f=timmer(index)
# f()
# index=timmer(被装饰函数的内存地址)
index=timmer(index) #index=inner
index() #inner()
'''
import time
def index():
print('welcom to index')
time.sleep(3)
def timmer(func):
#func=最原始的index
def wrapper():
start=time.time()
func()
stop=time.time()
print('run time is %s' %(stop - start))
return wrapper
index=timmer(index) #index=wrapper函数的内存地址
index()
二 语法糖
# import time
#
# def index():
# print('welcome to index')
# time.sleep(3)
# return 123
#
# def home(name):
# print('welcome %s to home page' %name)
# time.sleep(2)
#
#
# def timmer(func):
# #func=最原始的index
# def wrapper(*args,**kwargs):
# start=time.time()
# res=func(*args,**kwargs)
# stop=time.time()
# print('run time is %s' %(stop - start))
# return res
# return wrapper
#
#
# index=timmer(index)
# home=timmer(home)
#
# res=index()
# home('egon')
#装饰器语法糖
# 装饰器的语法糖:在被装饰对象正上方单独一行写@装饰器的名字
# 运行原理:
# python解释器一旦运行到@装饰器的名字,就会调用装饰器,然后讲被装饰函数的内存地址当作参数
# 传给装饰器,最后讲装饰器调用的结果辅助给原函数名
#装饰器可以叠加装饰器如果要用 可以在原有装饰器上@一个另外的装饰器语法自下而上,
#执行顺序自上而下
三 无参装饰器
# import time
# def timmer(func):
# #func=最原始的index
# def wrapper(*args,**kwargs):
# start=time.time()
# res=func(*args,**kwargs)
# stop=time.time()
# print('run time is %s' %(stop - start))
# return res
# return wrapper
#
# @timmer # index=timmer(index)
# def index():
# print('welcome to index')
# time.sleep(3)
# return 123
#
# @timmer # home=timmer(home)
# def home(name):
# print('welcome %s to home page' %name)
# time.sleep(2)
#
# res=index()
# home('egon')
def deco(func):
def wrapper(*args,**kwargs):
res=func(*args,**kwargs)
return res
return wrappers
四 有参装饰器
'''
有参装饰器
import time
current_user={'user':None}
def deco(func):
def wrapper(*args,**kwargs):
if current_user['user']:
#已经登陆过
res = func(*args, **kwargs)
return res
user=input('username>>: ').strip()
pwd=input('password>>: ').strip()
if user == 'egon' and pwd == '123':
print('login successful')
# 记录用户登陆状态
current_user['user']=user
res=func(*args,**kwargs)
return res
else:
print('user or password error')
return wrapper
@deco
def index():
print('welcome to index page')
time.sleep(1)
@deco
def home(name):
print('welecome %s to home page' %name)
time.sleep(0.5)
index()
home('egon')
'''
'''
def f1():
x=1
def f2():
def f3():
print(x)
return f3
return f2
f2=f1()
f3=f2()
f3()
'''
import time
current_user={'user':None}
def auth(engine='file'):
def deco(func):
def wrapper(*args,**kwargs):
if current_user['user']:
# print("已经登录")
res = func(*args, **kwargs)
return res
user=input('username>>: ').strip()
pwd=input('password>>: ').strip()
if engine == 'file':
# 基于文件的认证
if user == 'egon' and pwd == '123':
print('login successful')
# 记录用户登陆状态
current_user['user']=user
res=func(*args,**kwargs)
return res
else:
print('user or password error')
elif engine == 'mysql':
print('基于mysql的认证')
elif engine == 'ldap':
print('基于ldap的认证')
else:
print('无法识别认证来源')
return wrapper
return deco
@auth(engine='file') # @deco #index=deco(index) #index=wrapper
def index(name):
print('welcome to index page%s'%name)
time.sleep(1)
@auth(engine='file')
def home(name):
print('welecome %s to home page' %name)
time.sleep(0.5)
index("123")
home('alex')
# from functools import wraps #此模块的导入是可以将装饰器所有的值进行一个赋值操作,可以调用__开头的功能
# def deco(func):
# @wraps()
# def wrapper(*args,**kwargs):
# return func(*args,**kwargs)
# return wrapper()