Skip to content

Instantly share code, notes, and snippets.

@poros
Created October 26, 2015 01:03
Show Gist options
  • Save poros/a478b5f6a0381138906c to your computer and use it in GitHub Desktop.
Save poros/a478b5f6a0381138906c to your computer and use it in GitHub Desktop.
Using objects and decorators instead of classes with just one method and some constants
from job_class import Job
def delete_expired_stuff(time):
print "Delete expired stuff before %s" % time
class CleanupJob(Job):
# configuration: run at 5am
run_at = '05:00'
# implementation: nuke expired sessions
def run(self):
delete_expired_stuff(self.run_at)
if __name__ == '__main__':
CleanupJob().run()
from job_object import Job
def delete_expired_stuff(time):
print "Delete expired stuff before %s" % time
job = Job(run_at='05:00')
@job.run
def do_cleanup(job):
delete_expired_stuff(job.run_at)
if __name__ == '__main__':
job.run()
class Job(object):
run_at = None
def __init__(self):
print "Scheduled at %s" % self.run_at
def run(self):
raise NotImplementedError
class Job(object):
def __init__(self, run_at):
self.run_at = run_at
print "Scheduled at %s" % run_at
def _func(self, job):
raise NotImplementedError
def run(self, func=None):
if func is not None:
self._func = func
return func
return self._func(self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment