One: Write the function (the time of the function execution is random)
Two: Write the adorner, add the function of statistic time
Import TimedefTimmer (func):defWrapper (*args,**Kwargs): Start=time.time () Res= Func (*args,**Kwargs) Stop=time.time ()Print('run time is%s'% (Stop-start)) returnResreturnWrapper@timmerdefindex ():Print('Welcome to Index') Time.sleep (3) return123@timmerdefHome (name):Print('Welcome%s to home page'%name) Time.sleep (2) Res=index () Home ('Egon')View Code
Three: Writing adorners, adding authentication functions to functions
Import TimedefAuth (func):defWrapper (*args,**Kwargs): Tag=True whileTAG:NAME_INP= Input ('Username>>:'). Strip () Pwd_inp= Input ('Password>>:') with open ('DB','RT', encoding='Utf-8') as F: forLineinchF:line= Line.strip ('\ n'). Split (':') ifNAME_INP = = Line[0] andPWD_INP = = Line[1]: Print('Certification Success') Tag=False Break Else: Print('Authentication failed') Res=func (*args,**Kwargs)returnResreturnWrapper@auth#Index=auth (Index)defindex ():Print('Welcome to Index') Time.sleep (3) return123@auth#Home=auth (Home)defHome (name):Print('Welcome%s to home page'%name) Time.sleep (2) Res=index () Home ('Egon')View Code
Four: To write the adorner, for a number of functions and authentication function (the user's account password from the file), required to log on successfully once, subsequent functions do not need to enter the user name and password
Note: Reading a dictionary from a file as a string can be converted to a dictionary format using eval (' {' name ': ' Egon ', ' Password ': ' 123 '} ')
db='DB'Login_status={'User': None,'Status': False}defAuth (auth_type='file'): defAuth2 (func):defWrapper (*args,**Kwargs):iflogin_status['User'] andlogin_status['Status']: returnFunc (*args,**Kwargs)ifAuth_type = ='file': With open (db,encoding='Utf-8') as F:dic=eval (f.read ()) name=input ('Username:'). Strip () password=input ('Password:'). Strip ()ifNameinchDic andPassword = =Dic[name]: login_status['User']=name login_status['Status']=True Res=func (*args,**Kwargs)returnResElse: Print('username or password error') elifAuth_type = ='SQL': Pass Else: Pass returnwrapperreturnAuth2@auth ()defindex ():Print('Index') @auth (Auth_type='file')defHome (name):Print('Welcome%s to Home'%name) index () Home ('Egon')View Code
Five: Write adorners, add authentication function to multiple functions, require login to succeed once, do not need to repeat login in timeout period, exceed timeout time, you must log in again
ImportTime,randomuser={'User': None,'Login_time': None,'Timeout': 0.000003,}defTimmer (func):defWrapper (*args,**Kwargs): S1=time.time () Res=func (*args,**Kwargs) S2=time.time ()Print('%s'% (s2-s1)) returnResreturnwrapperdefAuth (func):defWrapper (*args,**Kwargs):ifuser['User']: Timeout=time.time ()-user['Login_time'] ifTimeout < user['Timeout']: returnFunc (*args,**Kwargs) name=input ('Name>>:'). Strip () password=input ('Password>>:'). Strip ()ifName = ='Egon' andPassword = ='123': user['User']=name user['Login_time']=time.time () Res=func (*args,**Kwargs)returnResreturnWrapper@authdefindex (): Time.sleep (Random.randrange (3)) Print('Welcome to Index') @authdefHome (name): Time.sleep (Random.randrange (3)) Print('Welcome%s to Home'%name) index () Home ('Egon')View Code
Python-code-12