get rid of decorator dependency

This commit is contained in:
AlanCoding
2018-10-30 11:54:36 -04:00
parent 92f0893764
commit d8d710a83d
4 changed files with 29 additions and 31 deletions

View File

@@ -154,12 +154,11 @@ def test_memoize_delete(memoized_function, mock_cache):
def test_memoize_parameter_error(): def test_memoize_parameter_error():
@common.memoize(cache_key='foo', track_function=True)
def fn():
return
with pytest.raises(common.IllegalArgumentError): with pytest.raises(common.IllegalArgumentError):
fn() @common.memoize(cache_key='foo', track_function=True)
def fn():
return
def test_extract_ansible_vars(): def test_extract_ansible_vars():

View File

@@ -18,14 +18,11 @@ import contextlib
import tempfile import tempfile
import six import six
import psutil import psutil
from functools import reduce from functools import reduce, wraps
from StringIO import StringIO from StringIO import StringIO
from decimal import Decimal from decimal import Decimal
# Decorator
from decorator import decorator
# Django # Django
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.db import DatabaseError from django.db import DatabaseError
@@ -136,31 +133,35 @@ def memoize(ttl=60, cache_key=None, track_function=False):
''' '''
Decorator to wrap a function and cache its result. Decorator to wrap a function and cache its result.
''' '''
if cache_key and track_function:
raise IllegalArgumentError("Can not specify cache_key when track_function is True")
cache = get_memoize_cache() cache = get_memoize_cache()
def _memoizer(f, *args, **kwargs): def memoize_decorator(f):
if cache_key and track_function: @wraps(f)
raise IllegalArgumentError("Can not specify cache_key when track_function is True") def _memoizer(*args, **kwargs):
if track_function:
if track_function: cache_dict_key = slugify('%r %r' % (args, kwargs))
cache_dict_key = slugify('%r %r' % (args, kwargs)) key = slugify("%s" % f.__name__)
key = slugify("%s" % f.__name__) cache_dict = cache.get(key) or dict()
cache_dict = cache.get(key) or dict() if cache_dict_key not in cache_dict:
if cache_dict_key not in cache_dict: value = f(*args, **kwargs)
value = f(*args, **kwargs) cache_dict[cache_dict_key] = value
cache_dict[cache_dict_key] = value cache.set(key, cache_dict, ttl)
cache.set(key, cache_dict, ttl) else:
value = cache_dict[cache_dict_key]
else: else:
value = cache_dict[cache_dict_key] key = cache_key or slugify('%s %r %r' % (f.__name__, args, kwargs))
else: value = cache.get(key)
key = cache_key or slugify('%s %r %r' % (f.__name__, args, kwargs)) if value is None:
value = cache.get(key) value = f(*args, **kwargs)
if value is None: cache.set(key, value, ttl)
value = f(*args, **kwargs)
cache.set(key, value, ttl)
return value return value
return decorator(_memoizer)
return _memoizer
return memoize_decorator
def memoize_delete(function_name): def memoize_delete(function_name):

View File

@@ -5,7 +5,6 @@ boto==2.47.0
channels==1.1.8 channels==1.1.8
celery==4.2.1 celery==4.2.1
daphne==1.3.0 # Last before backwards-incompatible channels 2 upgrade daphne==1.3.0 # Last before backwards-incompatible channels 2 upgrade
decorator==4.2.1
Django==1.11.16 Django==1.11.16
django-auth-ldap==1.2.8 django-auth-ldap==1.2.8
django-crum==0.7.2 django-crum==0.7.2

View File

@@ -23,7 +23,6 @@ channels==1.1.8
constantly==15.1.0 # via twisted constantly==15.1.0 # via twisted
cryptography==2.3.1 # via requests cryptography==2.3.1 # via requests
daphne==1.3.0 daphne==1.3.0
decorator==4.2.1
defusedxml==0.4.1 # via python-saml defusedxml==0.4.1 # via python-saml
django-auth-ldap==1.2.8 django-auth-ldap==1.2.8
django-crum==0.7.2 django-crum==0.7.2