Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Sitter():
- """Class for getting stuff done, or not."""
- num_attempts = 1
- task = None
- args = None
- post_action = None
- post_action_success = None
- post_action_fail = None
- def __init__(self, task_, args_=None):
- """Class initializer."""
- self.task = task_
- self.args = args_
- def set_num_attempts(self, num_attempts_):
- """How many times will task completion be attempted."""
- self.num_attempts = num_attempts_
- def set_post_action_level(self, post_action, level=1):
- """Set post action callback with given level
- level=1 - Call regardless of task succeess
- level=2 - Call after successful attempt
- level=3 - Call if defined number attempts are unsucessful."""
- pass
- def set_post_action(self, post_action_):
- """Set post action callback regardless of task succeess."""
- self.post_action = post_action_
- def set_post_action_success(self, post_action_):
- """Set post action callback on task succeess."""
- self.post_action_success = post_action_
- def set_post_action_fail(self, post_action_):
- """Set post action callback on task failed."""
- self.post_action_fail = post_action_
- def start(self):
- """Start task."""
- # declare aux
- bHasArgs = self.args is not None
- attempts = 0
- bContinue = True
- bSuccess = False
- while (bContinue):
- # declare aux
- # call task
- if self.args is not None:
- bSuccess = self.task(self.args)
- else:
- bSuccess = self.task()
- if bSuccess:
- # if success, complete and quit
- bSuccess = True
- bContinue = False
- else:
- # else, increment counter...
- attempts = attempts + 1
- # and check if it should continue
- if attempts > self.num_attempts:
- bContinue = False
- if self.post_action is not None:
- self.post_action()
- if bSuccess and self.post_action_success is not None:
- self.post_action_success()
- if not bSuccess and self.post_action_fail is not None:
- self.post_action_fail()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement