zoukankan      html  css  js  c++  java
  • functools.wraps 带参数的装饰器 多个装饰器装饰同一个函数


    装饰器
    开发原则 : 开放封闭原则
    装饰器的作用 :在不改变原函数的调用方式的情况下,在函数的前后添加功能
    装饰器的本质 : 闭包函数
    def wrapper(func):
        def inner(*args,**kwargs):
            print('在被装饰的函数执行之前做的事')
            ret = func(*args,**kwargs)
            print('在被装饰的函数执行之后做的事')
            return ret
        return inner
    
    @wrapper   #holiday = wrapper(holiday)
    def holiday(day):
        print('全体放假%s天'%day)
        return '好开心'
    
    ret = holiday(3)
    print(ret)
    def outer(*args):
        print(args)
        print(*args)
        def inner(*args):
            print('inner : ',args)
        inner(*args)
    
    
    outer(1,2,3,4)   #==outer(*[1,2,3,4])  #==outer(*(1,2,3,4))

     

    # 1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),
    # 要求登录成功一次,后续的函数都无需再输入用户名和密码
    FLAG = False
    def login(func):
        def inner(*args,**kwargs):
            global FLAG
            '''登录程序'''
            if FLAG:
                ret = func(*args, **kwargs)  # func是被装饰的函数
                return ret
            else:
                username = input('username : ')
                password = input('password : ')
                if username == 'boss_gold' and password == '22222':
                    FLAG = True
                    ret = func(*args,**kwargs)      #func是被装饰的函数
                    return ret
                else:
                    print('登录失败')
        return inner
    
    @login
    def shoplist_add():
        print('增加一件物品')
    
    @login
    def shoplist_del():
        print('删除一件物品')
    
    shoplist_add()
    shoplist_del()
    # 2.编写装饰器,为多个函数加上记录调用功能,要求每次调用函数都将被调用的函数名称写入文件
    def log(func):
        def inner(*args,**kwargs):
            with open('log','a',encoding='utf-8') as f:
                f.write(func.__name__+'
    ')
            ret = func(*args,**kwargs)
            return ret
        return inner
    
    @log
    def shoplist_add():
        print('增加一件物品')
    
    @log
    def shoplist_del():
        print('删除一件物品')
    
    shoplist_add()
    shoplist_del()
    shoplist_del()
    shoplist_del()
    shoplist_del()
    shoplist_del()

    # 1.编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果
    # 2.为题目1编写装饰器,实现缓存网页内容的功能:
    # 具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则,就去下载,然后存到文件中
    import os
    from urllib.request import urlopen
    def cache(func):
        def inner(*args,**kwargs):
            if os.path.getsize('web_cache'):
                with open('web_cache','rb') as f:
                    return f.read()
            ret = func(*args,**kwargs)  #get()
            with open('web_cache','wb') as f:
                f.write(b'*********'+ret)
            return ret
        return inner
    
    @cache
    def get(url):
        code = urlopen(url).read()
        return code
    
    
    # {'网址':"文件名"}
    ret = get('http://www.baidu.com')
    print(ret)
    ret = get('http://www.baidu.com')
    print(ret)
    ret = get('http://www.baidu.com')
    print(ret)
    functools wraps的用法
    from functools import wraps
    def wrapper(func):  #func = holiday
        @wraps(func)
        def inner(*args,**kwargs):
            print('在被装饰的函数执行之前做的事')
            ret = func(*args,**kwargs)
            print('在被装饰的函数执行之后做的事')
            return ret
        return inner
    
    @wrapper   #holiday = wrapper(holiday)
    def holiday(day):
        '''这是一个放假通知'''
        print('全体放假%s天'%day)
        return '好开心'
    
    print(holiday.__name__)
    print(holiday.__doc__)
    ret = holiday(3)   #inner
    print(ret)

    __name__和__doc__用法,前者是用str形式显示函数名,后者是显示备注部分。

    def wahaha():
        '''
        一个打印娃哈哈的函数
        :return:
        '''
        print('娃哈哈')
    
    print(wahaha.__name__) #查看字符串格式的函数名
    print(wahaha.__doc__)  #document



  • 相关阅读:
    为https请求配置ssl(不用keystore,直接用证书,rsa私钥,java代码)
    http请求对于List类型参数的处理
    java中string转ByteBuffer
    lua for循环如何从第0位开始
    lua中的cjson简单使用
    mongodb返回方便读的数据
    markdown简单插入图片
    #问题#java报Annotation processing is not supported for module cycles
    #问题#java报can't create non-static inner class instance
    git commit+push的完整步骤
  • 原文地址:https://www.cnblogs.com/qunxiadexiaoxiangjiao/p/8146095.html
Copyright © 2011-2022 走看看