带参数的Python 2.6装饰器

时间:2012-02-11 19:20:58

标签: python django decorator decorator-chaining

我正在为django编写文件系统缓存装饰器。问题是当我用这个装饰器和@register.simple_tag装饰我的函数时,我得到my_decorated_func需要0个参数错误(当页面加载了这个模板标签时)

from functools import wraps

from django.conf import settings
from django.core.cache import get_cache
from django.utils.http import urlquote
from django.utils.hashcompat import md5_constructor

FILESYSTEM_CACHE_NAME = 'filesystem'


def filesystem_cache(key_prefix, cache_time=None):
    """
    Caches function based on key_prefix and function args/kwargs.
    Stores function result in filesystem cache for a certain cache_time.
    """
    if cache_time:
        FILESYSTEM_CACHE_TIME = cache_time
    else:
        FILESYSTEM_CACHE_TIME = settings.CACHES['filesystem'].get('TIMEOUT')
    def wrapfunc(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            full_args = list()
            full_args.extend(args)
            for k, v in kwargs.items():
                full_args.append('%s:%s' % (str(k), str(v)))
            md5_args = md5_constructor(u':'.join([urlquote(var) for var in full_args]))
            cache_key = 'template.cache.%s.%s' % (key_prefix, md5_args.hexdigest())
            filesystem_cache = get_cache(FILESYSTEM_CACHE_NAME)
            cached_value = filesystem_cache.get(cache_key)
            if cached_value:
                # if cached value exists - return it
                return cached_value
            result = func(*args, **kwargs)
            filesystem_cache.set(cache_key, result, FILESYSTEM_CACHE_TIME)
            return result
        return wrapper
    return wrapfunc

@register.simple_tag
@filesystem_cache('countrypages')
def my_decorated_func(country, date, locale=None):

使用来自控制台的参数调用my_decorated_func是可以的,但是渲染模板:

{% my_decorated_func country todays_date 'en' %}

给我一​​个错误。

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:0)

这是一个解决方案:pip install decorator,而不是

def filesystem_cache(key_prefix, cache_time=None):
    """
    Caches function based on key_prefix and function args/kwargs.
    Stores function result in filesystem cache for a certain cache_time.
    """
    if cache_time:
        FILESYSTEM_CACHE_TIME = cache_time
    else:
        FILESYSTEM_CACHE_TIME = settings.CACHES['filesystem'].get('TIMEOUT')
    @decorator
    def wrapfunc(func, *args, **kwargs):
        full_args = list()
        full_args.extend(args)
        for k, v in kwargs.items():
            full_args.append('%s:%s' % (str(k), str(v)))
        md5_args = md5_constructor(u':'.join([urlquote(var) for var in full_args]))
        cache_key = 'template.cache.%s.%s' % (key_prefix, md5_args.hexdigest())
        filesystem_cache = get_cache(FILESYSTEM_CACHE_NAME)
        cached_value = filesystem_cache.get(cache_key)
        if cached_value:
            # if cached value exists - return it
            return cached_value
        result = func(*args, **kwargs)
        filesystem_cache.set(cache_key, result, FILESYSTEM_CACHE_TIME)
        return result
    return wrapfunc