python decorator simulating Jingdong landing

Keywords: Python

Requirement:

1. Three pages: home, book and finance
2. There are two ways to log in: the main page and bookstore page use JD account to log in, and the financial page uses wechat account to log in
2. Input: 1. Enter the main page, and so on; input: 2. Enter the bookstore page; input: 3. Enter the financial page
3. When entering the page, check whether there is login. If the login interface is not called, use the account password to log in. In any interface, just log in once
4. The account information is saved in the file
5. In any interface, enter Q to exit the program

Source code:

login_status = False  # Logon status

with open('Account information', 'r') as f_read_self:
    jingdong = f_read_self.readline().strip()
    jingdong = eval(jingdong)   # Built-in function eval()To convert a string into a dictionary
    for n, m in jingdong.items():
        JD_name = n
        JD_pwd = m

with open('Account information (wechat)', 'r') as f_read_wechat:
    wechat = f_read_wechat.readline().strip()
    wechat = eval(wechat)
    for n, m in wechat.items():
        wechat_name = n
        wechat_pwd = m


def login(auth_type="jingdong"):   # Judge the page type. By default, it is the JD login page

    def page(dis_play):
        global login_status    # Declare global variables

        def login_type():
            global login_status     # Declare global variables
            if not login_status:    # If not logged in
                if auth_type == "jingdong":
                    username = input("Username:")
                    passwd = input("Passwd:")
                    if JD_name == username and JD_pwd == passwd:
                        print("welcome ....")
                        dis_play()   # Log in and execute page function successfully, display content
                        login_status = True  # Change login status to True
                    else:
                        print("Wrong account or password,Please re-enter")
                elif auth_type == "wechat":
                    username = input("Username:")
                    passwd = input("Passwd:")
                    if wechat_name == username and wechat_pwd == passwd:
                        print("welcome ....")
                        dis_play()  # Log in and execute page function successfully, display content
                        login_status = True  # Change login status to True
                    else:
                        print("Wrong account or password")
            else:
                print("Landed")
        return login_type   # Return login_type The memory address of, used to point to the function object
    return page


@login()
def home():   # Main page
    print("welcome to home page")


@login()
def book():  # Bookstore
    print("welcome to home page")


@login(auth_type="wechat")
def finance():  # Finance
    print("welcome to home page")


while True:
    user_input = input('Please input:\n1: [homepage] \n2: [Bookstore] \n3: [Finance] \nQ:Sign out')
    if user_input == '1':
        home()
    elif user_input == '2':
        book()
    elif user_input == '3':
        finance()
    elif user_input == 'Q':
        break
Note: the format of account information in the file is as follows, which is saved in the form of dictionary

Account information: {'Tom':'qwe123',}

Account information (wechat): {'Toms':'qwe123 ',}

Posted by vex__ on Mon, 02 Dec 2019 10:01:53 -0800