From ce3924c901a4d3ae579d5a84f8ea91225429c7c1 Mon Sep 17 00:00:00 2001 From: Malthe Borch Date: Fri, 3 Jan 2014 10:21:37 +0100 Subject: [PATCH] Just use a regular private method along with 'property'. --- rq/job.py | 46 ++++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/rq/job.py b/rq/job.py index 79e5f2d..98b0b4d 100644 --- a/rq/job.py +++ b/rq/job.py @@ -1,6 +1,5 @@ import inspect from uuid import uuid4 -from functools import wraps try: from cPickle import loads, dumps, UnpicklingError except ImportError: # noqa @@ -69,23 +68,6 @@ class Job(object): data = None - def lazy(f, _attrs=[]): - attr = "_" + f.__name__ - _attrs.append(attr) - - @wraps(f) - def decorator(job): - if job.data is not None: - payload = unpickle(job.data) - for name, value in zip(_attrs, payload): - setattr(job, name, value) - - del job.data - - return getattr(job, attr) - - return property(decorator) - # Job construction @classmethod def create(cls, func, args=None, kwargs=None, connection=None, @@ -169,26 +151,30 @@ class Job(object): return import_attribute(self.func_name) - # Note: The order in which the following lazy attributes are - # declared is important. Don't change! + def _get_lazy(self, name): + if self.data is not None: + self._func_name, self._instance, self._args, self._kwargs = \ + unpickle(self.data) + + del self.data - @lazy + return getattr(self, "_" + name) + + @property def func_name(self): - return self._func_name + return self._get_lazy('func_name') - @lazy + @property def instance(self): - return self._instance + return self._get_lazy('instance') - @lazy + @property def args(self): - return self._args + return self._get_lazy('args') - @lazy + @property def kwargs(self): - return self._kwargs - - del lazy + return self._get_lazy('kwargs') @classmethod def exists(cls, job_id, connection=None):