Last active
May 26, 2024 18:25
-
-
Save luis261/dd7fe193627cad3a5f21fd6f894c3ef3 to your computer and use it in GitHub Desktop.
This file contains 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
def decorate_msg(msg, width, str_fmtmeth): | |
msg = "[" + msg + "]" if str_fmtmeth is None else f"[{getattr(str, str_fmtmeth)(msg)}]" | |
return msg.center(width, "*") | |
def emit_sectioned_stdout( | |
*msg_payloads, | |
header="attention", footer="", fmt_meth="upper", width=70, | |
**print_kwargs | |
): | |
print(decorate_msg(header, width, fmt_meth)) | |
if len(msg_payloads) == 0: return | |
print(*msg_payloads, **print_kwargs) | |
print("*" * width if len(footer) == 0 else decorate_msg(footer, width, fmt_meth)) | |
def call_with_verbose_retry(main_fn, *args, seconds_before_retry=60, **kwargs): | |
try: | |
main_fn(*args, **kwargs) | |
except Exception as e: | |
import traceback, sys | |
emit_sectioned_stdout( | |
traceback.format_exc(), | |
header="caught generic exception" | |
) | |
sys.stdout.flush(); sys.stderr.flush() | |
import time; time.sleep(seconds_before_retry) | |
try: | |
main_fn(*args, **kwargs) | |
except Exception as e: | |
raise e from None # remove traceback from previous try | |
if __name__ == "__main__": # adhoc demo: function which causes nested error | |
def _main(): | |
some_mapping = {} | |
try: | |
1/0 | |
except ZeroDivisionError: | |
some_mapping["somekey"] | |
print("Hmm. This shouldn't be reached. Ever!") | |
call_with_verbose_retry(_main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment