zoukankan      html  css  js  c++  java
  • 装饰器案例2

    #!usrinenvpython

    # -*- coding:utf-8 -*9

    import time

    user,passwd='alex','abc123'      #写定账户密码

    def auth(auth_type): #定义装饰器函数

          print("auth func :",auth_type)       #检查auth_type参数是否传入装饰器中

          def outer_weapper(func):      #outer采用了外部认证方式

               def wrapper(*args,**kwargs):        #定义嵌套函数

                     print("wrapper func args:",*args,**kwargs)      #检查参数是否传入函数中

                     if auth_type=="local"          #对传入的参数进行判断

                        username=input("Username:").strip     #设定账户

                        password=input("Password:").strip      #设定密码

                        if user==username and passwd ==password:      #对账户密码进行判断

                          print("33[33:lmUser has passwd authentication33[0m")     

                          res=func(*args,**kwargs)

                         print("----after sjjj-----")

                         return res

                   else:

                        exit("33[31:lmInvid username or password33[0m")

               elif  auth_type=="ldap"

                      print("搞什么 ldap ,不会。。。。")

              return weapper

        return outer_weapper

    def index():          #定义一个首页函数

          print("welcome to index page ")

    @auth(auth_type="local")

    def home():          #定义一个用户页面

           print("welcome to home page ")

          return "from home"

    @auth(auth_type="ldap")

    def bbs():            #定义一个论坛页面

           print("welcome to bbs page ")

    index()   #调用函数wrapper()函数

    home()   #这段代码实际上调用

    bbs()

    #需求:第一:新建三个页面,功能1:打开页面需要输入账户密码;功能2:home函数打印输出后结果是None,需要给Home函数进行设定一个返回值from home;功能3:需要给三个页面加上本地认证和远程认证以及写定认证的方式,继续采用装饰器进行嵌套函数+高阶函数的应用;

    功能1实现代码:

    user,passwd='alex','abc123'      #写定账户密码

    def auth(): #定义装饰器函数

          def wrapper(*args,**kwargs):        #定义嵌套函数

                username=input("Username:").strip     #设定账户

                password=input("Password:").strip      #设定密码

                if user==username and passwd ==password:      #对账户密码进行判断

                   print("33[33:lmUser has passwd authentication33[0m")     

                   func(*args,**kwargs)

                 else:

                      exit("33[31:lmInvid username or password33[0m")

    def index():          #定义一个首页函数

          print("welcome to index page ")

    @auth

    def home():          #定义一个用户页面

           print("welcome to home page ")

         

    @auth

    def bbs():            #定义一个论坛页面

           print("welcome to bbs page ")

    index()   #调用函数

    home()

    bbs()

    功能2代码实现:

    import time

    user,passwd='alex','abc123'      #写定账户密码

    def auth(): #定义装饰器函数

          def wrapper(*args,**kwargs):        #定义嵌套函数

                username=input("Username:").strip     #设定账户

                password=input("Password:").strip      #设定密码

                if user==username and passwd ==password:      #对账户密码进行判断

                   print("33[33:lmUser has passwd authentication33[0m")     

                   res=func(*args,**kwargs)

                   print("----after sjjj-----")

                   return res

                 else:

                      exit("33[31:lmInvid username or password33[0m")

    def index():          #定义一个首页函数

          print("welcome to index page ")

    @auth

    def home():          #定义一个用户页面

           print("welcome to home page ")

          return "from home"

    @auth

    def bbs():            #定义一个论坛页面

           print("welcome to bbs page ")

    index()   #调用函数

    home()

    bbs()

    功能3的实现方法:

    在函数引用装饰器的基础上,在装饰器中进行传入参数例如:@auth(auth_type="local")

    然后对每个定义函数中传入参数auth_type,并且采用print()方法检查参数是否传入

    功能3代码:在文章的第一段代码;本段代码包含了功能1、功能2和功能3!

  • 相关阅读:
    60、剑指offer--把二叉树打印成多行
    59、剑指offer--按之字形顺序打印二叉树
    KNN(最近邻算法)
    RBM(受限玻尔兹曼机)
    Denoising Autoencod
    决策树算法
    AdaBoost算法简介
    suricata工作流程简介
    KD tree详解
    tesseract训练新字库
  • 原文地址:https://www.cnblogs.com/lindong0602/p/9340675.html
Copyright © 2011-2022 走看看