3.23 decorator training exercises ---

 

First, with time statistics and authentication function decorator

Write a function (function of time by performing time.sleep (n) analog)

Write decorator, as a function of time together with statistical functions

Write decorator, as a function of plus certified function

 

Import Time
 DEF Timmer (FUNC):
     # FUNC = Py 
    DEF wrapper (* args, ** kwargs): 
        Start = time.time ()          # stamp. From the Unix operating system for the first time in 1970 (unix first year) and now the number of seconds this time of 
        RES = FUNC (* args, ** kwargs) 
        End = time.time ()
         Print ( " running time " , END- Start)
         return RES
     return wrapper 

DEF auth (FUNC):
     DEF wrapper (* args, ** kwargs): 
        username = the iNPUT ( " Please enter your user name:" ) 
        Pwd = the INPUT ( " Please enter your password: " )
         IF username == " fishball "  and pwd == " 188 741 " :
             Print ( " successful landing program is starting!! " ) 
            RES = FUNC (* args, ** kwargs )
             return RES
         the else :
             Print ( " ! password or account error, login failed " )
     return wrapper 

@auth        #Step2: Py = the auth (Timmer <local> .wrapper.) 
@timmer      # Step1: Timmer <local> .wrapper Timmer memory address = (Py). 
DEF Py (name): 
    the time.sleep ( . 3 )
     Print (F " Welcome {name} be Py! " ) 

Py ( " fishball " )

 

 

Second, the authentication function decorators (with login timeout verification)

Write decorator, multiple authentication functions plus functions (from the user's account password file) require a login is successful, the follow-up functions are no longer need to enter a user name and password

Note: read a file from the string dictionary can be used eval ( '{ "name": "egon", "password": "123"}') turn into a dictionary format

Plus authentication for multiple functions, requires a successful login, without having to repeat the login within the timeout period, exceeding the timeout, you must log in again

Import Time
 Import os
 DEF permission (FUNC):
     DEF wrapper (* args, ** kwargs): 

        # If logined.txt exist, you need to open logined.txt, remove the timestamp stored inside 
        # does not exist, then go directly to the login function 
        IF os.path.exists (R & lt " logined.txt " ): 
            with Open ( " logined.txt " , MODE = " RT " , encoding = " UTF-. 8 " ) AS F: 
                login_time = reached, f.read () 
                login_time = float (login_time) 
                now= Time.time () 

                # compare then stored timestamp and time difference now, if exceeded 60 seconds, the time-out, you need to log in again 
                IF now - login_time <= 60 : 
                    RES = FUNC (* args, ** kwargs)
                     return RES
                 the else :
                     Print ( " login timeout! " )
         the while True: 
            username = the iNPUT ( " Please enter your username (q: quit): " )
             IF username == " Q "  or username == " q " :
                break
            with open("db.txt",mode="rt",encoding="utf-8") as f:
                for line in f:
                    name,pwd,account = line.strip().split(":")
                    if name == username:
                        while True:
                            password = input("请输入密码(q:rename):")
                            if password == "q"  Or password == " Q " :
                                 BREAK 
                            IF password == pwd:
                                 Print ( " ! Successful landing " ) 

                                # login is successful, the current time is written 
                                with Open ( " logined.txt " , the MODE = " wt " , encoding = " UTF-. 8 " ) AS F1: 
                                    f1.write (F " {the time.time ()} ")
                                res = func(*args, **kwargs)
                                 return RES
                             the else :
                                 Print ( " Wrong password, please re-enter! " )
                         BREAK 
                the else :
                     Print ( " account does not exist, please re-enter " )
     return wrapper 

@permission 
DEF Py (name): 
    the time.sleep ( 3 )
     Print ( F " Welcome {name} for Py! " ) 

@permission 
DEF KY (name): 
    the time.sleep ( . 3)
     Print (F " Welcome {name} for Py! " ) 

Func_dic = { " . 1 " :( " Py function " , Py), " 2 " :( " KY function " , KY), " 0 " :( " Exit " , Exit)} 

the while True:
     Print ( " Function Store " .center (50, " - ")) 

    # According to the dictionary print user interface 
    for Key in func_dic:
        Print (Key, func_dic [Key] [0])
     Print ( " of The End " .center (50, " - " )) 

    cmd (INPUT = " Function Code Enter required: " )
     # determines whether the digital input command 
    IF cmd.isdigit ():
         IF cmd in func_dic: 

            # If cmd is 0, exit the program, the login account should withdraw together, so deleting logined.txt 
            IF cmd == " 0 " : 
                os.remove ( " logined.txt " ) 
            func_dic [cmd] [ . 1] (. 1 )
         the else:
             Print (F " code {cmd} does not exist " )
     the else :
         Print ( " Please enter the number! " ) 
    INPUT ( " Press Enter to continue. " )

 

Guess you like

Origin www.cnblogs.com/zhubincheng/p/12555480.html