import time
def timer(func):
def deco(*args,**kwargs):
start_time=time.time()
func(*args,**kwargs)
stop_time=time.time()
print("the funce run time is %s" %(stop_time-start_time))
return deco
def test1():
time.sleep(3)
print("in the test1")
@timer #非固定参数name
def test2(name):
time.sleep(3)
print("in the test2",name)
test1=timer(test1)
test1() ##-->deco
test2("alex")
模拟远端登录与本地登录试验:
user,passwd="alex","abc"
def auth(auth_type):
print("auth>>>",auth_type)
def outer_wrapper(func):
def wrapper(*args,**kwargs):
print("*args,**kwargs",*args,**kwargs)
if auth_type=="local":
username=input("Input user: ").strip()
password=input("Input password: ").strip()
if username==user and password==passwd:
print("\033[32;1mUser has passed authentication\033[0m")
res=func(*args,**kwargs)
return res
else:
exit("\033[31;1mIminvalid username or password\033[0m")
elif auth_type=="ldap":
print("不会")
return wrapper
return outer_wrapper
def index():
print("welcome to index page")
@auth(auth_type="local") ##本地验证登录
def home():
print("welcome to home page")
return "from home"