From d0d7cf214584a1980b6ac433fe30feb111073e6e Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 16 Sep 2021 21:19:46 -0700 Subject: [PATCH] Replace in+remove with index+pop. The code turns out to be longer with these duplicate try-except, but they are different enough that I can't really condense it at the moment. --- voussoirkit/operatornotify.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/voussoirkit/operatornotify.py b/voussoirkit/operatornotify.py index 8117c38..4472509 100644 --- a/voussoirkit/operatornotify.py +++ b/voussoirkit/operatornotify.py @@ -190,6 +190,8 @@ def get_level_by_argv(argv): and level is either an integer log level, or None if the user did not opt in. Even if you are not attaching operatornotify to your logger, you can still use this value to make decisions about when/what to notify. + + Raises ValueError if --operatornotify-level X is not a recognized level. ''' # This serves the purpose of normalizing the argument, but also creating a # duplicate list so we are not altering sys.argv. @@ -197,18 +199,27 @@ def get_level_by_argv(argv): argv = ['--operatornotify-level' if arg == '--operatornotify_level' else arg for arg in argv] level = None - if '--operatornotify-level' in argv: - level = argv.pop(argv.index('--operatornotify-level') + 1) + + try: + index = argv.index('--operatornotify-level') + except ValueError: + pass + else: + argv.pop(index) + level = argv.pop(index) try: level = int(level) except ValueError: level = vlogging.get_level_by_name(level) - argv.remove('--operatornotify-level') - if '--operatornotify' in argv: + try: + index = argv.index('--operatornotify') + except ValueError: + pass + else: if level is None: level = vlogging.WARNING - argv.remove('--operatornotify') + argv.pop(index) return (argv, level)