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,27 +63,28 @@ 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.
"""
attr = "_" + f.__name__
@wraps(f) data = None
def decorator(job):
if job.data is not None:
job._func_name, job._instance, job._args, job._kwargs = unpickle(job.data)
del job.data
return getattr(job, attr) def lazy(f, _attrs=[]):
attr = "_" + f.__name__
_attrs.append(attr)
return property(decorator) @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
class Job(object): return getattr(job, attr)
"""A Job is just a convenient datastructure to pass around job (meta) data.
"""
data = None return property(decorator)
# Job construction # Job construction
@classmethod @classmethod
@ -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