首页 > 代码库 > python之装饰器应用
python之装饰器应用
# -*- coding:utf-8 -*-nhj # Author:Lin import time userna,passwd = ‘lin‘,‘abc123‘ def authentication_method(auth_val): print(‘this is authentication method‘) def out_wrapper(func): def wrapper(*args,**kwargs): username = input(‘Username:‘).strip() password = input(‘Password:‘).strip() if auth_val == ‘local‘: if username == userna and password == passwd: print(‘\033[32;1m User has passed authentication \033[0m‘) print(‘\033[31;1m Welcome %s go back \033[0m‘ % username) start_time = time.time() func(*args,**kwargs) stop_timee = time.time() print(‘func run time is %s‘%(stop_timee-start_time)) else: exit(‘‘) elif auth_val == ‘ldap‘: if username == userna and password == passwd: print(‘\033[32;1m User has passed authentication \033[0m‘) print(‘\033[31;1m Welcome %s go back \033[0m‘ % username) start_time = time.time() func(*args, **kwargs) stop_timee = time.time() print(‘func run time is %s‘ % (stop_timee - start_time)) else: exit(‘‘) return wrapper return out_wrapper def index(): print(‘this is index page‘) @authentication_method(auth_val = ‘local‘) def home(): time.sleep(1) print(‘this is home page‘) @authentication_method(auth_val = ‘ldap‘) def bbs(username,author_timer): time.sleep(1) print(‘this is bbs page‘) print(‘bbs author:%s,author_timer:%s‘ %(username,author_timer)) index() home() bbs(‘lins‘,‘2016-11-28‘)
python之装饰器应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。