装饰器练习题

一:编写3个函数,每个函数执行的时间是不一样的

a = time.localtime()

def log_1():
    print('%s-%s-%s'%(a.tm_year, a.tm_mon, a.tm_mday))

def log_2():
    time.sleep(2)
    print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))

def log_3():
    time.sleep(4)
    print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))
log_1()
log_2()
log_3()
"""
2018-3-21
2018-3-21
2018-3-21
"""

二、编写装饰器,为每个函数加上统计运行时间的功能

import time
def timmer(func):

    def inner():
        start_time = time.time()
        func()
        wait_time = time.time() - start_time
        print("%s 运行时间:" % func.__name__, wait_time)
    return inner


a = time.localtime()

@timmer
def log_1():
    print('%s-%s-%s'%(a.tm_year, a.tm_mon, a.tm_mday))
@timmer
def log_2():
    time.sleep(2)
    print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))
@timmer
def log_3():
    time.sleep(4)
    print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))
log_1()
log_2()
log_3()
"""
2018-3-21
log_1 运行时间: 3.0994415283203125e-05
2018-3-21
log_2 运行时间: 2.0049030780792236
2018-3-21
log_3 运行时间: 4.004503965377808
"""

三、编写装饰器,为函数加上认证的功能,即要求认证成功才能执行函数

user_status = False
def login(func):
    def inner():
        _username = "alex"
        _password = "abc!23"
        global user_status
        if user_status is False:
            username = input("输入用户名:")
            password = input("密码:")
            if username == _username and password == _password:
                print("welcome login...")
                user_status = True
            else:
                print("wrong username or password!")
        if user_status:
            func()
    return inner

a = time.localtime()

def log_1():
    print('%s-%s-%s'%(a.tm_year, a.tm_mon, a.tm_mday))


def log_2():
    time.sleep(2)
    print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))
@login
def log_3():
    time.sleep(4)
    print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))


log_1()
log_2()
log_3()
"""
2018-3-21
2018-3-21
输入用户名:alex
密码:abc!23
welcome login...
2018-3-21
"""

四、编写装饰器,为多个函数加上认证功能(用户的账户密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码。

import os,time

user_status = False
def login(func):
    file = os.path.exists('user_info.txt')
    if file is True:
        file = open(file='user_info.txt', mode='r+', encoding='utf-8')
        f = file.read()
        user_info = eval(f)
        file.close()
    else:
        file = open('user_info.txt', mode='w', encoding='utf-8')
        choice = input("是否注册用户?[Y/N]")
        if choice == 'Y' or choice == 'y':
            name = input("请输入新用户用户名:")
            password = input("请输入新用户密码:")
            user_info = {'name': name, 'password': password}
            row = str(user_info)
            file.write(row)
            file.close()

    def inner():
        _username = user_info['name']
        _password = user_info['password']
        global user_status
        if user_status is False:
            username = input("输入用户名:")
            password = input("密码:")
            if username == _username and password == _password:
                print("welcome login...")
                user_status = True
            else:
                print("wrong username or password!")
        if user_status:
            func()
    return inner


a = time.localtime()


def log_1():
    print('%s-%s-%s'%(a.tm_year, a.tm_mon, a.tm_mday))

@login
def log_2():
    time.sleep(2)
    print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))

@login
def log_3():
    time.sleep(4)
    print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))


log_1()
log_2()
log_3()

"""
是否注册用户?[Y/N]Y
请输入新用户用户名:hqs
请输入新用户密码:123
2018-3-21
输入用户名:hqs
密码:123
welcome login...
2018-3-21
2018-3-21
"""

 

posted @ 2018-03-21 16:28  休耕  阅读(876)  评论(0编辑  收藏  举报