首页 > 编程语言 > 详细

python之装饰器应用

时间:2016-12-29 08:32:45      阅读:132      评论:0      收藏:0      [点我收藏+]
# -*- 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之装饰器应用

原文:http://daring.blog.51cto.com/891785/1887112

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!