zoukankan      html  css  js  c++  java
  • 2019年3月21日 装饰器进阶2-带参数的装饰器

    eval 函数

    eval()官方文档里面给出来的功能解释是:将字符串string对象转化为有效的表达式参与求值运算返回计算结果

    语法上:调用的是:eval(expression,globals=None, locals=None)返回的是计算结果

    其中:

        expression是一个参与计算的python表达式

        globals是可选的参数,如果设置属性不为None的话,就必须是dictionary对象了

        locals也是一个可选的对象,如果设置属性不为None的话,可以是任何map对象了

    user_list=[#定义密码库
        {'name':'sxj','password':'123'},
        {'name':'abc','password':'111'},
        {'name':'def','password':'222'},#注意这里的222是字符串类型
        {'name':'ghw','password':333}#注意这里的333是int 类型
    ]
    current_dic={'username':None,'login':False}#用户字典,反应登入状态,用字典做全局变量
    
    def auth(auth_type='filedb'):
        def idf(func):#验证登入信息是否正确
            def wrapper(*args,**kwargs):
                if auth_type=='filedb':
                    if current_dic['username'] and current_dic['login']:#验证登入状态,如果正确登入,则直接执行
                        print("验证通过")
                        res = func(*args, **kwargs)
                        return res
                    else:
                        user_name=input('please input username: '.strip())#.strip指的是移除前后空格或者制表符
                        pass_word=input('please input password: '.strip())#注意这里输入的是字符串 与上面的333会因为类型不同导致密码错误
                        for u_dic in user_list:
                            if u_dic['name'] == user_name and str(u_dic['password'])== pass_word: #为了防止发生类型不同,所以用str强制转化为字符串类型
                                current_dic['username']=user_name#更新用户字典的登入状态
                                current_dic['login']=True#更新用户字典的登入状态
                                print("验证通过")
                                res = func(*args, **kwargs)
                                return res
                        else:
                            print('I am sorry')
                elif auth_type=='sxjdb':
                    print('hello,%s'%auth_type)
                elif auth_type=='wwwdb':
                    print('hello,%s'%auth_type)
                else:
                    print('wrong auth_type=%s'%auth_type)
            return wrapper
        return idf
    
    @auth(auth_type='filedb')
    def index():
        print('welcome to index')
    
    @auth(auth_type='sxjdb')
    def home(name2):
        print('%s,welcome to home'%name2)
    
    @auth(auth_type='ww4wdb')
    def shopping_car():
        print('this is shopping car,you have %s,%s,%s'%('奶茶','妹妹','sxj'))
    
    print('First>',current_dic)
    home('sxj')
    print('Last>',current_dic)
    shopping_car()

    >>>

    First> {'username': None, 'login': False}
    hello,sxjdb
    Last> {'username': None, 'login': False}
    wrong auth_type=ww4wdb

  • 相关阅读:
    Echrarts的基本API
    Echarts中Option属性设置
    大数据ETL处理时遇到的坑
    模块设计模式
    PAT (Advanced Level) Practice 代码
    递归中的 DFS 与 DP 比较
    CF 1557 D. Ezzat and Grid
    CF 1557 C. Moamen and XOR
    CF 1555 E. Boring Segments
    CF 1555 D. Say No to Palindromes
  • 原文地址:https://www.cnblogs.com/python1988/p/10573983.html
Copyright © 2011-2022 走看看