From 5ec2030c17137569e8b527aaabc486c1e7ad4cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Oliveira?= Date: Mon, 24 Jun 2024 15:14:00 +0200 Subject: [PATCH] Convert several .format strings to f-strings (#48) This addresses several pylint issues --- anycast_healthchecker/main.py | 10 ++++----- anycast_healthchecker/servicecheck.py | 2 +- anycast_healthchecker/utils.py | 32 +++++++++++++-------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/anycast_healthchecker/main.py b/anycast_healthchecker/main.py index 8ba0046..349a7ea 100644 --- a/anycast_healthchecker/main.py +++ b/anycast_healthchecker/main.py @@ -49,9 +49,9 @@ def main(): args = docopt(__doc__, version=__version__) if args['--print']: for section in DEFAULT_OPTIONS: - print("[{}]".format(section)) + print(f"[{section}]") for key, value in DEFAULT_OPTIONS[section].items(): - print("{k} = {v}".format(k=key, v=value)) + print(f"{key} = {value}") print() sys.exit(0) @@ -68,15 +68,15 @@ def main(): if args['--print-conf']: for section in config: - print("[{}]".format(section)) + print(f"[{section}]") for key, value in config[section].items(): - print("{k} = {v}".format(k=key, v=value)) + print(f"{key} = {value}") print() sys.exit(0) try: lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) - lock_socket.bind('\0' + "{}".format(PROGRAM_NAME)) + lock_socket.bind('\0' + f"{PROGRAM_NAME}") except socket.error as exc: sys.exit("failed to acquire a lock by creating an abstract namespace" " socket: {}".format(exc)) diff --git a/anycast_healthchecker/servicecheck.py b/anycast_healthchecker/servicecheck.py index 1e59735..2b75952 100644 --- a/anycast_healthchecker/servicecheck.py +++ b/anycast_healthchecker/servicecheck.py @@ -129,7 +129,7 @@ def _run_check(self): return 126 else: duration = (time.time() - start_time) * 1000 - msg = "check duration {t:.3f}ms".format(t=duration) + msg = f"check duration {duration:.3f}ms" self.log.info(msg) self.metric_check_duration.labels(**self.labels).set(duration) diff --git a/anycast_healthchecker/utils.py b/anycast_healthchecker/utils.py index aa98ff7..972852a 100644 --- a/anycast_healthchecker/utils.py +++ b/anycast_healthchecker/utils.py @@ -272,7 +272,7 @@ def modify_ip_prefixes( msg = ("CRITICAL: failed to create Bird configuration {e}, " "this is FATAL error, thus exiting main program" .format(e=error)) - sys.exit("{m}".format(m=msg)) + sys.exit(f"{msg}") else: log.info("Bird configuration for IPv%s is updated", ip_version) reconfigure_bird(reconfigure_cmd) @@ -354,10 +354,10 @@ def configuration_check(config): # Catch the case where the directory, under which we store the pid file, is # missing. if not os.path.isdir(os.path.dirname(pidfile)): - raise ValueError("{d} doesn't exit".format(d=os.path.dirname(pidfile))) + raise ValueError(f"{os.path.dirname(pidfile)} doesn't exit") if not isinstance(num_level, int): - raise ValueError('Invalid log level: {}'.format(log_level)) + raise ValueError(f'Invalid log level: {log_level}') for _file in 'log_file', 'stderr_file': if config.has_option('daemon', _file): @@ -602,7 +602,7 @@ def create_bird_config_files(bird_configuration): "history of changes for {b}:{e}" .format(d=history_dir, b=config_file, e=exc)) else: - print("{d} is created".format(d=history_dir)) + print(f"{history_dir} is created") def running(processid): @@ -662,7 +662,7 @@ def get_ip_prefixes_from_bird(filename): return prefixes -class BaseOperation(object): +class BaseOperation: """Run operation on a list. Arguments: @@ -822,8 +822,8 @@ def write_temp_bird_conf(dummy_ip_prefix, .format(t=datetime.datetime.now(), n=PROGRAM_NAME, p=os.getpid())) - tmpf.write("{c}\n".format(c=comment)) - tmpf.write("define {n} =\n".format(n=variable_name)) + tmpf.write(f"{comment}\n") + tmpf.write(f"define {variable_name} =\n") tmpf.write("{s}[\n".format(s=4 * ' ')) # all entries of the array need a trailing comma except the last # one. A single element array doesn't need a trailing comma. @@ -890,7 +890,7 @@ def update_pidfile(pidfile): try: pid = int(pid) except ValueError: - print("cleaning stale pidfile with invalid data:'{}'".format(pid)) + print(f"cleaning stale pidfile with invalid data:'{pid}'") write_pid(pidfile) else: if running(pid): @@ -899,19 +899,19 @@ def update_pidfile(pidfile): # be stopped. Since newer version has a different locking # mechanism, we can end up with both versions running. # In order to avoid this situation we refuse to startup. - sys.exit("process {} is already running".format(pid)) + sys.exit(f"process {pid} is already running") else: # pidfile exists with a PID for a process that is not running. # Let's update PID. - print("updating stale processID({}) in pidfile".format(pid)) + print(f"updating stale processID({pid}) in pidfile") write_pid(pidfile) except FileNotFoundError: # Either it's 1st time we run or previous run was terminated # successfully. - print("creating pidfile {f}".format(f=pidfile)) + print(f"creating pidfile {pidfile}") write_pid(pidfile) except OSError as exc: - sys.exit("failed to update pidfile:{e}".format(e=exc)) + sys.exit(f"failed to update pidfile:{exc}") def write_pid(pidfile): @@ -927,10 +927,10 @@ def write_pid(pidfile): pid = str(os.getpid()) try: with open(pidfile, mode='w') as _file: - print("writing processID {p} to pidfile".format(p=pid)) + print(f"writing processID {pid} to pidfile") _file.write(pid) except OSError as exc: - sys.exit("failed to write pidfile:{e}".format(e=exc)) + sys.exit(f"failed to write pidfile:{exc}") def shutdown(pidfile, signalnb=None, frame=None): @@ -1001,7 +1001,7 @@ def log_format(): 'message', ] - return ' '.join(['%({0:s})'.format(i) for i in supported_keys]) + return ' '.join([f'%({i:s})' for i in supported_keys]) custom_format = log_format() json_formatter = CustomJsonFormatter(custom_format, @@ -1069,7 +1069,7 @@ def log_format(): return logger -class CustomLogger(object): +class CustomLogger: """Helper Logger to redirect STDOUT or STDERR to a logging hander. It wraps a Logger class into a file like object, which provides a handy