一:編寫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 """
2、編寫裝飾器,爲每一個函數加上統計運行時間的功能spa
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 """
3、編寫裝飾器,爲函數加上認證的功能,即要求認證成功才能執行函數code
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 """
4、編寫裝飾器,爲多個函數加上認證功能(用戶的帳戶密碼來源於文件),要求登陸成功一次,後續的函數都無需再輸入用戶名和密碼。blog
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 """