python装饰器应用场景待完成

    python装饰器就是用于拓展原来函数功能的一种特殊函数,这个函数的特殊之处在于它的返回值也是一个函数,他的作用就是在不改原函数的代码前提下增加函数的功能。

1 异常处理:
     try-exception是python捕获异常的常用方式,但在代码频繁的使用,采用装饰器对try-catch进行了封装。异常处理使用装饰器.

def get_decorator(errors=(Exception, ), default_value=''):

    def decorator(func):

        def new_func(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except errors, e:
                print "Got error! ", repr(e)
                return default_value

        return new_func

    return decorator

try_except = get_decorator(default_value='default')
a = {}

@try_except
def example1(a):
    return a['b']

@try_except
def example2(a):
    return doesnt_exist()

print example1(a)
print example2(a)
#Got error!  KeyError('b',)
#default
#Got error!  NameError("global name 'doesnt_exist' is not #defined",)
#default

2 缓存
缓存装饰器和参数检查装饰器很像,但是更关注那些内部状态不会影响输出的函数。对于这样的函数,其每一组参数都可以关联到一个独特的结果。这种风格的编程是函数式编程的特色(函数编程请查看:http://en.wikipedia.org/wiki/Functional_programming),当输入值的集合是有限的时可以使用。

因此,缓存装饰器能够将输出结果和产生这个结果所需要的输入参数绑定起来,这样在随后的相同调用中就可以直接返回结果。这种行为叫做记忆(memoizing,http://en.wikipedia.org/wiki/Memoizing),并且使用装饰器来实现是很简单的:

import time
import hashlib
import pickle

cache = {}

def is_obsolete(entry, duration):
    return time.time() - entry['time']> duration

def compute_key(function, args, kw):
    key = pickle.dumps((function.__name__, args, kw))
    return hashlib.sha1(key).hexdigest()

def memoize(duration=10):
    def _memoize(function):
        def __memoize(*args, **kw):
            key = compute_key(function, args, kw)
            # do we have it already ?
            if (key in cache and not is_obsolete(cache[key], duration)):
                print('we got a winner')
                return cache[key]['value']
            # computing
            result = function(*args, **kw)
            # storing the result
            cache[key] = {
                'value': result,
                'time': time.time()
            }
            return result
        return __memoize
    return _memoize


下面是使用该装饰器的一个例子:

@memoize()
def very_very_very_complex_stuff(a, b):
    # if your computer gets too hot on this calculation
    # consider stopping it
    return a + b

very_very_very_complex_stuff(2, 2)
4
very_very_very_complex_stuff(2, 2)
we got a winner

@memoize(1) # invalidates the cache after 1 second
def very_very_very_complex_stuff(a, b):
    return a + b
very_very_very_complex_stuff(2, 2)
4
very_very_very_complex_stuff(2, 2)
we got a winner
4
cache
{'c2727f43c6e39b3694649ee0883234cf': {'value': 4, 'time': 1199734132.7102251)}
time.sleep(2)
very_very_very_complex_stuff(2, 2)
4
缓存计算代价‘昂贵’的函数,能够显著地提升应用程序的性能,但是使用时要小心。缓存的值也可以被绑定到函数自身,来管理其作用域,生命周期,而不是存放到一个集中的字典中。但是,更高效的缓存装饰器应该使用以高级缓存算法为基础的特殊的缓存库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

**星光*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值