編輯器練習

import time
def timer(func): # timer(tes1)  func=tes1
   
def deco(*args,**kwargs):       #多少個參數都匹配
       
start_time=time.time()
        func(*args,**kwargs)  
#run tes1() #匹配deco的參數 多少個參數都匹配
       
stop_time=time.time()
       
print("the func run time is %s" %(stop_time-start_time))
   
return deco

@
timer # tes1=timer(tes1)
def tes1():
    time.sleep(
1)
   
print('in the test1')
@
timer
def tes2(*args,**kwargs):
    time.sleep(
1)
   
print('in the test2')
tes1()
tes2(
"leiwenbin",22)

 

#

import time
user,password =
'alex','abc123'
def auth(auth_type):
   
print("auth func",auth_type)
   
def outer_wrappaer(func):
       
def wrapper(*args,**kwargs):
           
print("auth func",*args,**kwargs)
           
if auth_type == "local":
                username =
input("Username:").strip()
                passwd =
input("Password:").strip()
               
if user == username and passwd == password:
                   
print("User has passwd authentication")
                    res = func(*args, **kwargs) 
# from home
                   
print("----after authentication")
                   
return res
               
else:
                   
exit("Invalid username or passoword")
           
elif auth_type == "ldap":
               
print("ldap驗證...")

       
return wrapper
   
return outer_wrappaer
def index():
   
print("welcome to index page")
@
auth(auth_type="local"# home=wrapper()
def home():
   
print("welcome to home page")
   
return "from home"
@auth(auth_type="ldap")
def bbs():
   
print("welcome to bbs page")

index()
print(home()) #wrappaer
bbs()

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章