灵虚御风
醉饮千觞不知愁,忘川来生空余恨!

导航

 
import time


user_dic = {'is_login':None}


def outter(func): # func = 最原始的login函数的内存地址
def get_time(*args, **kwargs): # args = ('egon',) kwargs = {}
start = time.time()
res = func(*args, **kwargs) # 最原始的login函数的内存地址() 直接调用 func('egon')
end = time.time()
print('func run time:%s'%(end-start))
return res
return get_time

def login_auth2(data_source,x,t):
# data_source = 'file'
def login_auth(func):
# func = index
def inner(*args,**kwargs): # 这里的参数是跟被装饰函数的参数一一对应
if user_dic['is_login']:
res = func(*args, **kwargs)
return res
else:
if data_source == 'file':
username = input('please input your username>>>:').strip()
password = input('please input your password>>>:').strip()
if username == 'jason' and password == '123':
user_dic['is_login'] = True
res = func(*args,**kwargs)
return res
else:
print('username or password error')
elif data_source == 'MySQL':
print('from MySQL')
elif data_source == 'ldap':
print('ldap')
else:
print('暂无该数据来源')
return inner
return login_auth

# 装饰器在装饰的时候 顺序从下往上
# 装饰器在执行的时候 顺序从上往下
# res = login_auth2('MySQL')
@login_auth2('file',1,2) # index = login_auth(get_time) index = inner函数的内存地址
# @login_auth
@outter # get_time = outter(index) index是最原始的index
def index():
time.sleep(1)
print('index')
return 'index'
index()
posted on 2022-03-24 14:59  没有如果,只看将来  阅读(15)  评论(0编辑  收藏  举报