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