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

Loading…
Cancel
Save