码迷,mamicode.com
首页 > 其他好文 > 详细

装饰作业

时间:2017-04-11 16:57:02      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:ges   strip   imp   bre   ldap   tar   username   user   pre   

技术分享

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()

装饰作业

标签:ges   strip   imp   bre   ldap   tar   username   user   pre   

原文地址:http://www.cnblogs.com/panyouming/p/6690675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!