Move seemingly general purpose decorator into class scope, use and delete.

Also, reduce repeat attribute declarations, at the cost of a
requirement that decorated functions appear in the right order.
main
Malthe Borch 11 years ago
parent ba0b39a43b
commit c898fe618e

@ -63,28 +63,29 @@ def get_current_job():
return Job.fetch(job_id)
def lazy(f):
"""Decorator for a lazy data property."""
class Job(object):
"""A Job is just a convenient datastructure to pass around job (meta) data.
"""
data = None
def lazy(f, _attrs=[]):
attr = "_" + f.__name__
_attrs.append(attr)
@wraps(f)
def decorator(job):
if job.data is not None:
job._func_name, job._instance, job._args, job._kwargs = unpickle(job.data)
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)
class Job(object):
"""A Job is just a convenient datastructure to pass around job (meta) data.
"""
data = None
# Job construction
@classmethod
def create(cls, func, args=None, kwargs=None, connection=None,
@ -117,10 +118,6 @@ class Job(object):
job._dependency_id = depends_on.id if isinstance(depends_on, Job) else depends_on
return job
@lazy
def func_name(self):
return self._func_name
def _get_status(self):
self._status = as_text(self.connection.hget(self.key, 'status'))
return self._status
@ -172,6 +169,13 @@ 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!
@lazy
def func_name(self):
return self._func_name
@lazy
def instance(self):
return self._instance
@ -184,6 +188,8 @@ class Job(object):
def kwargs(self):
return self._kwargs
del lazy
@classmethod
def exists(cls, job_id, connection=None):
"""Returns whether a job hash exists for the given job ID."""

Loading…
Cancel
Save