• python(15)- 装饰器及装饰器的使用


    装饰器

    1.无参数

    2.函数有参数

    3.函数动态参数

     4.装饰器参数 

     

     

     

    装饰器的应用

    下面题目同http://www.cnblogs.com/xuyaping/p/6679305.html,只不过加了装饰器统计时间和认证功能。
     
    1、定义无参装饰器为被装饰器添加统计运行时间的功能
    #定义闭包无参函数,为程序增加统计时间功能
    import time
    def timer(func):         #定义timer函数,func变量值为login
        def wrapper():       
            start_time=time.time()   #设置函数起始时间
            func()                   #函数名加()调用函数,即调用login(),然后调用结束后继续在wrapper函数中运行
            stop_time=time.time()
            print("run time is %s"%(stop_time-start_time))
        return wrapper   #返回wrapper函数名,然后再次进入wrapper函数
    
    @timer        #相当于timer(login)-->赋值给timer函数名
    def login():
        #读取注册用户的信息,用户名,密码,输错次数,写入字典中
        user={}
        with open("DB1",encoding="utf8") as f:
            for line in f:
                username_list=line.strip().split("|")      #username_list--->['egon', '123', '2']
                user[username_list[0]]={"name":username_list[0],
                         "pwd":username_list[1],
                         "times":username_list[2]}
        # print(user)  #-->{'egon': {'name': 'egon', 'pwd': '123', 'times': '2'}, 'xuyaping': {'name': 'xuyaping', 'pwd': '123', 'times': '0'}, 'xyy': {'name': 'xyy', 'pwd': '123', 'times': '1'}}
     
        #读取黑名单用户,将黑名单用户加入列表中
        with open("black_lockname",encoding="utf8") as f1:
            black_list=[]
            for line in f1:
                black_list.append(line.strip())
        # print(black_list)
     
     
        while True:
            username = input("please input your username:").strip()
            passwd = input("please input your passwd:").strip()
            #用户在黑名单中
            if username in black_list:
                print("该用户为黑名单用户,请滚")
                break
     
            # 用户为注册用户
            elif username in user:
                user[username]["times"]=int(user[username]["times"])
                if user[username]["times"]<3 and passwd==user[username]["pwd"]:
                    print("登录成功")
                    user[username]["times"]=0
                    #将修改后的信息重新写入DB1中
                    with open("DB1","w",encoding="utf8") as f3:
                        for i in user:
                            f3.write(i + "|" + user[i]["pwd"] + "|" + str(user[i]["times"]) + "
    ")
                    break
     
                else:
                    user[username]["times"]+=1
                    print("登录错误")
                    # 将修改后的信息重新写入DB1中
                    with open("DB1", "w", encoding="utf8") as f3:
                        for i in user:
                            f3.write(i + "|" + user[i]["pwd"] + "|" + str(user[i]["times"]) + "
    ")
                    if user[username]["times"]==3:
                        black_list.append(username)
                        print("账户被锁定")
                        # 将修改后的信息重新写入black_lockname中
                        with open("black_lockname","w",encoding="utf8") as f4:
                            for j in black_list:
                                f4.write(j+ "
    ")
                        break
     
            #用户不是注册用户
            else:
                print("该用户没有注册")
                break
    
    login()
    

       

     
    2、定义有参装饰器为被装饰器添加认证功能:用户的信息可以来源于file也可以是ldap,三次验证失败锁定用户
     
    #定义闭包有参函数,为程序增加验证功能
    def auth2(auth_type):       
        def auth(func):   #func参数此时被赋值为login
            def wragger(*args,**kwargs):   #wragger函数携带变量auth_type的值
                if auth_type=="file":
                    func()     #运行函数login
                elif auth_type=="ldap":
                    print("你他妈还想不想玩了?")
    
            return wragger
        return auth
    
    @auth2(auth_type="file")   #相当于运行函数auth2(file),返回auth,auth(login)赋值给login函数名
    def login():
        #读取注册用户的信息,用户名,密码,输错次数,写入字典中
        user={}
        with open("DB1",encoding="utf8") as f:
            for line in f:
                username_list=line.strip().split("|")      #username_list--->['egon', '123', '2']
                user[username_list[0]]={"name":username_list[0],
                         "pwd":username_list[1],
                         "times":username_list[2]}
        # print(user)  #-->{'egon': {'name': 'egon', 'pwd': '123', 'times': '2'}, 'xuyaping': {'name': 'xuyaping', 'pwd': '123', 'times': '0'}, 'xyy': {'name': 'xyy', 'pwd': '123', 'times': '1'}}
     
        #读取黑名单用户,将黑名单用户加入列表中
        with open("black_lockname",encoding="utf8") as f1:
            black_list=[]
            for line in f1:
                black_list.append(line.strip())
        # print(black_list)
     
     
        while True:
            username = input("please input your username:").strip()
            passwd = input("please input your passwd:").strip()
            #用户在黑名单中
            if username in black_list:
                print("该用户为黑名单用户,请滚")
                break
     
            # 用户为注册用户
            elif username in user:
                user[username]["times"]=int(user[username]["times"])
                if user[username]["times"]<3 and passwd==user[username]["pwd"]:
                    print("登录成功")
                    user[username]["times"]=0
                    #将修改后的信息重新写入DB1中
                    with open("DB1","w",encoding="utf8") as f3:
                        for i in user:
                            f3.write(i + "|" + user[i]["pwd"] + "|" + str(user[i]["times"]) + "
    ")
                    break
     
                else:
                    user[username]["times"]+=1
                    print("登录错误")
                    # 将修改后的信息重新写入DB1中
                    with open("DB1", "w", encoding="utf8") as f3:
                        for i in user:
                            f3.write(i + "|" + user[i]["pwd"] + "|" + str(user[i]["times"]) + "
    ")
                    if user[username]["times"]==3:
                        black_list.append(username)
                        print("账户被锁定")
                        # 将修改后的信息重新写入black_lockname中
                        with open("black_lockname","w",encoding="utf8") as f4:
                            for j in black_list:
                                f4.write(j+ "
    ")
                        break
     
            #用户不是注册用户
            else:
                print("该用户没有注册")
                break
    
    login()
    

      

      

  • 相关阅读:
    hadoop2.2.0 centos6.4 编译安装详解
    Hadoop 2.2.0的高可用性集群中遇到的一些问题(64位)
    Visual Studio 常用快捷键
    Android(1)—Mono For Android 环境搭建及破解
    IbatisNet SqlMap.config配置节导致的程序无法通过
    CAD数据分块,偏移校准,加载到百度地图、高德地图、谷歌等地图上
    数据库SQL优化大总结
    Scratch 3下载,最新版Scratch下载,macOS、Windows版
    高性能网站设计之缓存更新的套路
    【验证无效】MySQL的count(*)的优化,获取千万级数据表的总行数
  • 原文地址:https://www.cnblogs.com/xuyaping/p/6690776.html
Copyright © 2020-2023  润新知