一:编写函数,(函数执行的时间用time.sleep(n)模拟)
import time
def my_index(x,y):
time.sleep(3)
print('{},{},welcome to my index page'.format(x,y)
return 100
二:编写装饰器,为函数加上统计时间的功能
import time
def timmer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
res = func(*args, **kwargs)
end_time = time.time()
print('run time is {} s.'.format(end_time - start_time))
return res
return wrapper
@timmer
def my_index(x, y):
time.sleep(3)
print('{}, {}, welcome to my index.'.format(x,y))
return 100
res = my_index('egon','tank')
print(res)
三:编写装饰器,为函数加上认证的功能
import time
def auth(func):
def wrapper(*args,**kwargs):
inp_name = input('请输入你的账号:').strip()
inp_pwd = input('请输入你的密码:').strip()
if inp_name == 'vincent' and inp_pwd == '666':
res = func(*args,**kwargs)
return res
else:
print('账号或密码错误。')
return wrapper
@auth
def my_index(x,y):
time.sleep(3)
print('{}, {},welcome to my index.'.format(x,y))
return 100
my_index('egon','tank')
四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码;
注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式
def auth(func):
def wrapper(*args,**kwargs):
global login_status
if not login_status:
inp_name = input('请输入你的账号:').strip()
inp_pwd = input('请输入你的密码:').strip()
with open('db.txt','rt',encoding='utf8') as f:
for line in f:
user_info = eval(line.strip())
if inp_name == user_info.get('name') and inp_pwd == user_info.get('password'):
print('登录成功。')
login_status = 1
res = func(*args,**kwargs)
return res
else:
print('账号或密码错误,登录失败。')
return
res = func(*args,**kwargs)
return res
return wrapper
import time
@auth
def index(x,y):
time.sleep(3)
print('{}, {},from index page.'.format(x,y))
return 'return from index'
@auth
def home(name):
time.sleep(2)
print('{} from home page.'.format(name))
return 'return from home'
login_status = 0
index('张三','李四')
home('王五')
index('张三','李四')
五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录
def auth(func):
def wrapper(*args,**kwargs):
global login_status,login_time
if not login_status:
inp_name = input('请输入你的账号:').strip()
inp_pwd = input('请输入你的密码:').strip()
with open('db.txt','rt',encoding='utf8') as f:
for line in f:
user_info = eval(line.strip())
if inp_name == user_info.get('name') and inp_pwd == user_info.get('password'):
print('登录成功。')
login_status = 1
login_time = time.time()
res = func(*args,**kwargs)
return res
else:
print('账号或密码错误,登录失败。')
return
else:
now_time = time.time()
if now_time - login_time <= 10:
login_status = 1
login_time = time.time()
res = func(*args, **kwargs)
return res
else:
login_status = 0
inp_name = input('请输入你的账号:').strip()
inp_pwd = input('请输入你的密码:').strip()
with open('db.txt', 'rt', encoding='utf8') as f:
for line in f:
user_info = eval(line.strip())
if inp_name == user_info.get('name') and inp_pwd == user_info.get('password'):
print('登录成功。')
login_status = 1
login_time = time.time()
res = func(*args, **kwargs)
return res
else:
print('账号或密码错误,登录失败。')
return
return wrapper
import time
@auth
def index(x,y):
time.sleep(3)
print('{}, {},from index page.'.format(x,y))
return 'return from index'
@auth
def home(name):
time.sleep(2)
print('{} from home page.'.format(name))
return 'return from home'
login_status = 0
login_time = None
index('张三','李四')
time.sleep(5)
home('王五')