#no-pass versionImport TimedefTimer (func):#The function test is passed as a variable to the Func defconut (): Start_time=time.time () func () Stop_time=time.time ()Print('The func run time is%s'% (stop_time-start_time)) returnConut#@timer # equivalent to Test=timer (test)defTest (): Time.sleep (3) Print('In the test') Test=Timer (test) test ()#There is a pass versionImport TimedefTimer (func):#The function test is passed as a variable to the Func defConut (*args,**Kwargs): Start_time=time.time () func (*args,**Kwargs) Stop_time=time.time ()Print('The func run time is%s'% (stop_time-start_time)) returnConut@timer#equivalent to Test=timer (test)defTest (Name,age): Time.sleep (3) Print('%s is%s'%(name,age))#test=timer (test)Test'Bruce', 22)#Premium EditionImportTimename,password='Bruce','123456'defAuth (auth_type):defOut_wrapper (func):defWrapper (*args,**Kwargs):ifAuth_type = ='Local': Username=input ('User:'). Strip () passwd=input ('Passwd:'). Strip ()ifUsername = = Name andpasswd = =Password:Print('\033[32;1M Certification Success! \033[32;0m') ret= Func (*args,**Kwargs)returnretElse: Exit ()elifAuth_type = ='LDAP': Print('No, no! ') returnwrapperreturnOut_wrapper@auth (Auth_type='LDAP')defindex ():Print('welcom to index page') @auth (Auth_type='Local')defHome ():Print('welcom to home page') returnHomeindex () Home ()
Classroom Practice-python Decorators