Created
October 26, 2015 01:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Job(object): | |
run_at = None | |
def __init__(self): | |
print "Scheduled at %s" % self.run_at | |
def run(self): | |
raise NotImplementedError |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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