装饰器案例2

#!\usr\bin\env\python

# -*- 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("\033[33:lmUser has passwd authentication\033[0m")     

                      res=func(*args,**kwargs)

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

                     return res

               else:

                    exit("\033[31:lmInvid username or password\033[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("\033[33:lmUser has passwd authentication\033[0m")     

               func(*args,**kwargs)

             else:

                  exit("\033[31:lmInvid username or password\033[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("\033[33:lmUser has passwd authentication\033[0m")     

               res=func(*args,**kwargs)

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

               return res

             else:

                  exit("\033[31:lmInvid username or password\033[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!

posted on 2018-07-20 11:47  凛冬08  阅读(92)  评论(0编辑  收藏  举报

导航