Advertisement
cvalente

sitter

Aug 22nd, 2019
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. class Sitter():
  2.     """Class for getting stuff done, or not."""
  3.    
  4.     num_attempts            = 1
  5.  
  6.     task                    = None
  7.     args                    = None
  8.     post_action             = None
  9.     post_action_success     = None
  10.     post_action_fail        = None
  11.  
  12.     def __init__(self, task_, args_=None):
  13.         """Class initializer."""
  14.         self.task = task_
  15.         self.args = args_
  16.    
  17.     def set_num_attempts(self, num_attempts_):
  18.         """How many times will task completion be attempted."""
  19.         self.num_attempts = num_attempts_
  20.  
  21.     def set_post_action_level(self, post_action, level=1):
  22.         """Set post action callback with given level
  23.        level=1 - Call regardless of task succeess
  24.        level=2 - Call after successful attempt
  25.        level=3 - Call if defined number attempts are unsucessful."""
  26.         pass
  27.  
  28.     def set_post_action(self, post_action_):
  29.         """Set post action callback regardless of task succeess."""
  30.         self.post_action = post_action_
  31.  
  32.     def set_post_action_success(self, post_action_):
  33.         """Set post action callback on task succeess."""
  34.         self.post_action_success = post_action_
  35.  
  36.     def set_post_action_fail(self, post_action_):
  37.         """Set post action callback on task failed."""
  38.         self.post_action_fail = post_action_
  39.  
  40.     def start(self):
  41.         """Start task."""
  42.         # declare aux
  43.         bHasArgs = self.args is not None
  44.         attempts = 0
  45.         bContinue = True
  46.         bSuccess = False
  47.        
  48.         while (bContinue):
  49.             # declare aux
  50.  
  51.             # call task
  52.             if self.args is not None:
  53.                 bSuccess = self.task(self.args)
  54.             else:
  55.                 bSuccess = self.task()
  56.  
  57.             if bSuccess:
  58.                 # if success, complete and quit
  59.                 bSuccess  = True
  60.                 bContinue = False
  61.             else:
  62.                 # else, increment counter...
  63.                 attempts = attempts + 1
  64.                 # and check if it should continue
  65.                 if attempts > self.num_attempts:
  66.                     bContinue = False
  67.  
  68.         if self.post_action is not None:
  69.             self.post_action()
  70.  
  71.         if bSuccess and self.post_action_success is not None:
  72.             self.post_action_success()
  73.  
  74.         if not bSuccess and self.post_action_fail is not None:
  75.             self.post_action_fail()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement