登录并打印业务时间(装饰器练习)


#简单版调用函数
1.写一个不同打印时间
import time
def log_1():
start_time=time.time()
time.sleep(1)
print('welcome to the page log_1')
end_time=time.time()
spend_time=end_time-start_time
print('spend%s'%spend_time)
log_1()

def log_2():
start_time=time.time()
time.sleep(2)
print('welcome to the page log_2')
end_time=time.time()
spend_time=end_time-start_time
print('spend%s'%spend_time)
log_2()

#简单版改成内嵌函数
import time
def common(f):
def inner():
start_time = time.time()
f()
end_time = time.time()
spend_time = end_time - start_time
print('spend%s' % spend_time)
return inner
def log_1():
time.sleep(1)
print('welcome to the page log_1')
log_1=common(log_1)
log_1()

def log_2():
time.sleep(2)
print('welcome to the page log_2')
log_2=common(log_2)
log_2()
#正规简单版装饰器函数
import time
def common(f):
def inner():
start_time = time.time()
f()
end_time = time.time()
spend_time = end_time - start_time
print('spend%s' % spend_time)
return inner
@common#=[log_1=common(log_1)]
def log_1():
time.sleep(1)
print('welcome to the page log_1')
log_1()

@common#=[log_2=common(log_2)]
def log_2():
time.sleep(2)
print('welcome to the page log_2')
log_2()
#中级版装饰器:加上用户认证
import time
user_status = False
def common(f):
def inner():
user='wangzhen'#定义用户名
pwd = '123456'#定义密码
global user_status#此步操作相当于局部作用域已经知道user_status是False
if user_status is False:
username=input('请输入姓名:')
password=input('请输入密码:')
if username ==user and password ==pwd:
start_time = time.time()
f()
end_time = time.time()
spend_time = end_time - start_time
print('spend%s' % spend_time)
user_status = True #但是在函数中声明此变量为全局变量,再次修改user_status
else:
print('输入错误')
else:
start_time = time.time()
f()
end_time = time.time()
spend_time = end_time - start_time
print('spend%s' % spend_time)
return inner

@common #=[log_1=common(log_1)]
def log_1():
time.sleep(5)
print('welcome to the page log_1,hahaha')
log_1()

@common#=[log_2=common(log_2)]
def log_2():
time.sleep(2)
print('welcome to the page log_2')
log_2()


#小高级版:小高级版与高级版实现的功能是一样的,存在不同点是:小高级版如果改动代码,则不涉及的模块都要跟着改代码,主要是outer(log_1)和outer(log_2)是死的,不涉及的模块都要跟着变,修改
成高版本后,被调用的模块就不用变了,依然是调用之前的简单版
#再加上用户认证,从文件获取用户名和密码
import time,os #引入系统已经规定好的模块
user_status = False#用户登录状态默认为未登录
file =os.path.exists('user_info.txt')#判断该文件是否存在
if file is True:#如果存在接下来就读取文件,并将文件转换成字典
file_read = open('user_info.txt', 'r+', encoding='utf8')
file_read2 = file_read.read().strip()#去除空格和特殊符号的读取
user_info=eval(file_read2)#文件转换成字典
file_read.close()#该函数中目前只能用该种读取+关闭,使用with open始终报错,不知道啥
else:
file= open('user_info.txt', 'w', encoding='utf8')#文件不存在就写文件
choice = input('是否注册用户[y/n]')#是否愿意注册
if choice =='y':
name =input('请输入姓名:')
password = input('请输入密码:')
user_info={'name':name,'password':password}#这一步很巧妙,不然无法取值,也可以使用列表,但是没有字典方便
f_write =str(user_info)#将字典转换成字符串
file.write(f_write)#写入到文件
file.close()
def outer(f):#装饰器函数内容函数
user=user_info['name']#取字典的值
pwd = user_info['password']
global user_status#此步操作相当于局部作用域已经知道user_status是False
if user_status is False:#判断有没有登录系统
username=input('请输入姓名:')
password=input('请输入密码:')
if username ==user and password ==pwd:#登录系统后的操作

start_time = time.time()
f()#次函数为闭包
end_time = time.time()
spend_time = end_time - start_time
print('spend%s' % spend_time)
user_status = True#在函数中已经声明此变量为全局变量,再次修改user_status,这样达到下次登录不需要再登录的业务

else:
print('输入错误')
else:#不用登录后,可以直接打印业务数据
start_time = time.time()
f()
end_time = time.time()
spend_time = end_time - start_time
print('spend%s' % spend_time)

def log_1():
time.sleep(5)
print('welcome to the page log_1,hahaha')
outer(log_1)

def log_2():
time.sleep(2)
print('welcome to the page log_2')
outer(log_2)
#高级版:小高级版与高级版实现的功能是一样的,存在不同点是:小高级版如果改动代码,则不涉及的模块都要跟着改代码,主要是outer(log_1)和outer(log_2)是死的,不涉及的模块都要跟着变,修改
成以下版本后,被调用的模块就不用变了,依然是调用之前的简单版
三层函数
#再加上用户认证,从文件获取用户名和密码
import time
user_status = False
def common(f):#该初必须要带参数,而且是函数,这样才能返回函数
def inner():#此处是否需要参数,取决于被调用的函数需不需要参数
user='wangzhen'
pwd = '123456'
global user_status#此步操作相当于局部作用域已经知道user_status是False
if user_status is False:
username=input('请输入姓名:')
password=input('请输入密码:')
if username ==user and password ==pwd:
start_time = time.time()
f()#若次函数需要参数,则上一层别调用的函数要带参数,才可能形成闭包
end_time = time.time()
spend_time = end_time - start_time
print('spend%s' % spend_time)
user_status = True #但是在函数中声明此变量为全局变量,再次修改user_status
else:
print('输入错误')
else:
start_time = time.time()
f()
end_time = time.time()
spend_time = end_time - start_time
print('spend%s' % spend_time)
return inner

@common #=[log_1=common(log_1)]
def log_1():
time.sleep(5)
print('welcome to the page log_1,hahaha')
log_1()#此处是否有参数取决于f()是否需要参数

@common#=[log_2=common(log_2)]
def log_2():
time.sleep(2)
print('welcome to the page log_2')
log_2()
#与以下作比较三层函数调用
import time

def show_time(f):
def inner(*args):#inner是一个闭包,引用外部的f函数
start = time.time()
f(*args)
end = time.time()
print('spend %s' % (end - start))
return inner
@show_time #=[add=show_time(add)],此部分就是装饰器,实际就是赋值,业务线上的函数依然不动,后台通过赋值来实现
def add(*args):
sum =0
for i in args:
sum+=i
print(sum)
time.sleep(2)
add(1,2,3,45)#此add相当于inner函数的调用

装饰器加参数
import time
def logger(flag):
def show_time(f):
def inner(*args):#inner是一个闭包,引用外部的f函数
start = time.time()
f(*args)
end = time.time()
print('spend %s' % (end - start))
if flag =='true':
print('日志记录')
return inner
return show_time
@logger('true') #=[@show_time],返回show_time的地址且还要执行show_time,=[add=show_time(add)],但是此时@show_time可以使用flag变量,利用闭包
#原因是因为:logger('true')=return show_time
def add(*args):
sum =0
for i in args:
sum+=i
print(sum)
time.sleep(2)
add(1,2,3,45)#此add相当于inner函数的调用
posted @ 2019-06-27 16:59  是破折号还是波折号  阅读(42)  评论(0)    收藏  举报