Skip to content

Commit

Permalink
use str.format() instead of % / percent format
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Dec 20, 2024
1 parent c9088a5 commit 508fb5d
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 23 deletions.
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions scripts/cpu_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]:
Expand Down
4 changes: 3 additions & 1 deletion scripts/fans.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
3 changes: 2 additions & 1 deletion scripts/ifconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion scripts/internal/check_broken_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion scripts/internal/print_api_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion scripts/meminfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
3 changes: 1 addition & 2 deletions scripts/nettop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
4 changes: 2 additions & 2 deletions scripts/procinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
7 changes: 4 additions & 3 deletions scripts/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion scripts/temperatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions scripts/top.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion scripts/who.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 508fb5d

Please sign in to comment.