MickeyLater

double wrapper Reddit question

Feb 15th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. def evaluate_error_flag(error_flag, output):
  2. """Decorator for processing error messages. Calls embedded within functions.
  3.  
  4. :error_flag: True (error) or False (no error).
  5.  
  6. :function: Either the error message (if error) or the data being validated (if no error).
  7.  
  8. :returns: Validated data (if no error)
  9. """
  10. def evaluate_error(error_flag, output):
  11. func(error_flag, output)
  12. if error_flag:
  13. print(output)
  14. return output
  15. return evaluate_error
  16.  
  17. @evaluate_error_flag
  18. def try_function(*args):
  19. """Wrapper uses a try statement and the wrapper to check for validity of statements.
  20. """
  21. def try_func(*args):
  22. try:
  23. return False, func(*args)
  24. except:
  25. return True, "Error in function {}.".format(str(function))
  26. return try_func
  27.  
  28. @try_function
  29. def do_something(some_data, some_other_data):
  30. # some code
  31. pass
Add Comment
Please, Sign In to add comment