首页 > 代码库 > 装饰作业

装饰作业

技术分享

1.

import time
def timmer(func):
def wrapper(*arg,**kwargs):
start_time = time.time()
res=func(*arg,**kwargs)
end_time = time.time()
print("运行时间:",end_time-start_time)
return res
return wrapper
@timmer
def my_max(x,y):
time.sleep(1)
max = x if x>y else y
return max

my_max(3,5)
print(my_max(3,5))

技术分享

2.

 

def auth2(auth_type):
def auth(func):
def warpper(*args,**kwargs):
if auth_type == ‘ldap‘:
flag = True
while flag:
with open("lock", "r",encoding="utf8") as f_read, open("account1", "r",encoding="utf8") as f_write:
username = input("input your username:").strip()
r=eval(f_write.read())
if username in r :
m = f_read.read()
f = eval(m)
if f[username] == 3:
print("username is locked")
else:
while flag:
password = input("input your password")
if str(r[username]) == password:
print("successful")
func()
flag = False
break
else:
with open("lock", "w",encoding="utf8") as f_read:
number = int(f[username])
number += 1
f[username] = number
f_read.write(str(f))
if number == 3:
print("lock")
flag = False
else:
print("input right username")
else:
print("other")
return warpper
return auth


@auth2(auth_type=‘ldap‘)
def tell():
print("welcome to oldboy")

tell()

装饰作业