From 508fb5da3b5b7ad740f82cabc2a01d8a64794b28 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Fri, 20 Dec 2024 17:45:40 +0100 Subject: [PATCH] use str.format() instead of % / percent format --- pyproject.toml | 5 ++--- scripts/cpu_distribution.py | 6 +++--- scripts/fans.py | 4 +++- scripts/ifconfig.py | 3 ++- scripts/internal/check_broken_links.py | 2 +- scripts/internal/print_api_speed.py | 2 +- scripts/meminfo.py | 2 +- scripts/nettop.py | 3 +-- scripts/procinfo.py | 4 ++-- scripts/sensors.py | 7 ++++--- scripts/temperatures.py | 2 +- scripts/top.py | 8 +++++--- scripts/who.py | 2 +- 13 files changed, 27 insertions(+), 23 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index fa5cd07a1..fa63e600f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -99,11 +99,10 @@ ignore = [ # T201 == print() # T203 == pprint() # TRY003 == raise-vanilla-args -# UP031 == Use format specifiers instead of percent format ".github/workflows/*" = ["EM101", "EM102", "EM103", "T201", "T203"] "psutil/tests/*" = ["EM101", "EM102", "EM103", "TRY003"] -"scripts/*" = ["EM101", "EM102", "EM103", "T201", "T203", "UP031"] -"scripts/internal/*" = ["EM101", "EM102", "EM103", "T201", "T203", "TRY003", "UP031"] +"scripts/*" = ["EM101", "EM102", "EM103", "T201", "T203"] +"scripts/internal/*" = ["EM101", "EM102", "EM103", "T201", "T203", "TRY003"] "setup.py" = [ "B904", # Use ` raise from` to specify exception cause (PYTHON2.7 COMPAT) "C4", # flake8-comprehensions (PYTHON2.7 COMPAT) diff --git a/scripts/cpu_distribution.py b/scripts/cpu_distribution.py index 85495c0d3..ecb0b9c0d 100755 --- a/scripts/cpu_distribution.py +++ b/scripts/cpu_distribution.py @@ -72,13 +72,13 @@ def main(): clean_screen() cpus_percent = psutil.cpu_percent(percpu=True) for i in range(num_cpus): - print("CPU %-6i" % i, end="") + print("CPU {:<6}".format(i), end="") if cpus_hidden: print(" (+ hidden)", end="") print() for _ in range(num_cpus): - print("%-10s" % cpus_percent.pop(0), end="") + print("{:<10}".format(cpus_percent.pop(0)), end="") print() # processes @@ -93,7 +93,7 @@ def main(): pname = procs[num].pop() except IndexError: pname = "" - print("%-10s" % pname[:10], end="") + print("{:<10}".format(pname[:10]), end="") print() curr_line += 1 if curr_line >= shutil.get_terminal_size()[1]: diff --git a/scripts/fans.py b/scripts/fans.py index bfab434c4..ca6a453bd 100755 --- a/scripts/fans.py +++ b/scripts/fans.py @@ -27,7 +27,9 @@ def main(): for name, entries in fans.items(): print(name) for entry in entries: - print(" %-20s %s RPM" % (entry.label or name, entry.current)) + print( + " {:<20} {} RPM".format(entry.label or name, entry.current) + ) print() diff --git a/scripts/ifconfig.py b/scripts/ifconfig.py index 93a14b952..3ab3c013b 100755 --- a/scripts/ifconfig.py +++ b/scripts/ifconfig.py @@ -99,7 +99,8 @@ def main(): ) ) for addr in addrs: - print(" %-4s" % af_map.get(addr.family, addr.family), end="") + fam = " {:<4}".format(af_map.get(addr.family, addr.family)) + print(fam, end="") print(f" address : {addr.address}") if addr.broadcast: print(f" broadcast : {addr.broadcast}") diff --git a/scripts/internal/check_broken_links.py b/scripts/internal/check_broken_links.py index 408886817..2a51381d3 100755 --- a/scripts/internal/check_broken_links.py +++ b/scripts/internal/check_broken_links.py @@ -252,7 +252,7 @@ def main(): else: for fail in fails: fname, url = fail - print("%-30s: %s " % (fname, url)) + print("{:<30}: {} ".format(fname, url)) print('-' * 20) print(f"total: {len(fails)} fails!") sys.exit(1) diff --git a/scripts/internal/print_api_speed.py b/scripts/internal/print_api_speed.py index 673f51c89..5c155f4d3 100755 --- a/scripts/internal/print_api_speed.py +++ b/scripts/internal/print_api_speed.py @@ -103,7 +103,7 @@ def print_timings(): def timecall(title, fun, *args, **kw): - print("%-50s" % title, end="") + print("{:<50}".format(title), end="") sys.stdout.flush() t = timer() for n in range(TIMES): diff --git a/scripts/meminfo.py b/scripts/meminfo.py index a13b7e00b..6bee96998 100755 --- a/scripts/meminfo.py +++ b/scripts/meminfo.py @@ -38,7 +38,7 @@ def pprint_ntuple(nt): value = getattr(nt, name) if name != 'percent': value = bytes2human(value) - print('%-10s : %7s' % (name.capitalize(), value)) + print('{:<10} : {:>7}'.format(name.capitalize(), value)) def main(): diff --git a/scripts/nettop.py b/scripts/nettop.py index eafcd0f5d..cda16bf07 100755 --- a/scripts/nettop.py +++ b/scripts/nettop.py @@ -81,8 +81,7 @@ def refresh_window(tot_before, tot_after, pnic_before, pnic_after): # totals printl( - "total bytes: sent: %-10s received: %s" - % ( + "total bytes: sent: {:<10} received: {}".format( bytes2human(tot_after.bytes_sent), bytes2human(tot_after.bytes_recv), ) diff --git a/scripts/procinfo.py b/scripts/procinfo.py index afdda11b7..5feb49bd3 100755 --- a/scripts/procinfo.py +++ b/scripts/procinfo.py @@ -120,9 +120,9 @@ def print_(a, b): if sys.stdout.isatty() and psutil.POSIX: - fmt = '\x1b[1;32m%-13s\x1b[0m %s' % (a, b) + fmt = "\x1b[1;32m{:<13}\x1b[0m {}".format(a, b) else: - fmt = '%-11s %s' % (a, b) + fmt = "{:<11} {}".format(a, b) print(fmt) diff --git a/scripts/sensors.py b/scripts/sensors.py index fe1286ba5..05aec894a 100755 --- a/scripts/sensors.py +++ b/scripts/sensors.py @@ -59,7 +59,7 @@ def main(): if name in temps: print(" Temperatures:") for entry in temps[name]: - s = " %-20s %s°C (high=%s°C, critical=%s°C)" % ( + s = " {:<20} {}°C (high={}°C, critical={}°C)".format( entry.label or name, entry.current, entry.high, @@ -71,8 +71,9 @@ def main(): print(" Fans:") for entry in fans[name]: print( - " %-20s %s RPM" - % (entry.label or name, entry.current) + " {:<20} {} RPM".format( + entry.label or name, entry.current + ) ) # Battery. diff --git a/scripts/temperatures.py b/scripts/temperatures.py index 2253fef7f..6bc078766 100755 --- a/scripts/temperatures.py +++ b/scripts/temperatures.py @@ -36,7 +36,7 @@ def main(): for name, entries in temps.items(): print(name) for entry in entries: - line = " %-20s %s °C (high = %s °C, critical = %s °C)" % ( + line = " {:<20} {} °C (high = {} °C, critical = %{} °C)".format( entry.label or name, entry.current, entry.high, diff --git a/scripts/top.py b/scripts/top.py index 993f47780..46fca7553 100755 --- a/scripts/top.py +++ b/scripts/top.py @@ -129,13 +129,15 @@ def get_dashes(perc): percs = psutil.cpu_percent(interval=0, percpu=True) for cpu_num, perc in enumerate(percs): dashes, empty_dashes = get_dashes(perc) - line = " CPU%-2s [%s%s] %5s%%" % (cpu_num, dashes, empty_dashes, perc) + line = " CPU{:<2} [{}{}] {:>5}%".format( + cpu_num, dashes, empty_dashes, perc + ) printl(line, color=get_color(perc)) # memory usage mem = psutil.virtual_memory() dashes, empty_dashes = get_dashes(mem.percent) - line = " Mem [%s%s] %5s%% %6s / %s" % ( + line = " Mem [{}{}] {:>5}% {:>6} / {}".format( dashes, empty_dashes, mem.percent, @@ -147,7 +149,7 @@ def get_dashes(perc): # swap usage swap = psutil.swap_memory() dashes, empty_dashes = get_dashes(swap.percent) - line = " Swap [%s%s] %5s%% %6s / %s" % ( + line = " Swap [{}{}] {:>5}% {:>6} / {}".format( dashes, empty_dashes, swap.percent, diff --git a/scripts/who.py b/scripts/who.py index 094c4fd1c..d45954723 100755 --- a/scripts/who.py +++ b/scripts/who.py @@ -21,7 +21,7 @@ def main(): users = psutil.users() for user in users: proc_name = psutil.Process(user.pid).name() if user.pid else "" - line = "%-12s %-10s %-10s %-14s %s" % ( + line = "{:<12} {:<10} {:<10} {:<14} {}".format( user.name, user.terminal or '-', datetime.fromtimestamp(user.started).strftime("%Y-%m-%d %H:%M"),