On Fri, 28 Oct 2022, Leah Leshchinsky wrote:
Add fstrings where possible to improve readabilty
Signed-off-by: Leah Leshchinsky <lleshchi(a)redhat.com>
diff --git a/tuna-cmd.py b/tuna-cmd.py
index c5bc65059da7..fa07909967c8 100755
--- a/tuna-cmd.py
+++ b/tuna-cmd.py
@@ -89,7 +89,7 @@ irqs = None
class HelpMessageParser(argparse.ArgumentParser):
def error(self, message):
- sys.stderr.write('error: %s\n' % message)
+ sys.stderr.write(f'error: {message}\n')
self.print_help()
sys.exit(2)
@@ -263,13 +263,13 @@ def thread_help(tid):
ps = procfs.pidstats()
if tid not in ps:
- print("tuna: " + _("thread %d doesn't exists!") % tid)
+ print(f"tuna: thread {tid} doesn't exists!")
- Why not correct the grammar here? (should be 'exist' and not 'exists')
- tuna-cmd.py inherits help, help has
tuna/help.py:_ = gettext.gettext
so the underscore is an alias for gettext.getext. It's possible that
the other language capabilities of tuna are broken, but if we want to
preserve it for now, we need to keep the underscore. eg
print(f"tuna: " + _(f"thread {tid} doesn't exist!"))
- try to test your code, the above could be tested by this
$./tuna-cmd.py what_is 1234
tuna: thread 1234 doesn't exist!
for this to at least potentially work you would have to correct
the grammar in these three files
$ git grep exists po/*
po/ja.po:msgid "thread %d doesn't exists!"
po/pt_BR.po:msgid "thread %d doesn't exists!"
po/zh_CN.po:msgid "thread %d doesn't exists!"
It isn't too much detail to explain how to test in the description
return
pinfo = ps[tid]
cmdline = procfs.process_cmdline(pinfo)
help, title = tuna.kthread_help_plain_text(tid, cmdline)
- print("%s\n\n%s" % (title, _(help)))
+ print(f"{title}\n\n{help}")
- Retain the underscore here too, so it should be
print(f"{title}\n\n{_(help)}"
I can see that there isn't always help available
$ ./tuna-cmd.py what_is 123
Kernel Thread 123 (acpi_thermal_pm):
So now we get two newlines for no reasons, we should probably test for
that. If there is help, it looks like this
$ ./tuna-cmd.py what_is 2
Kernel Thread 2 (kthreadd):
Used to create kernel threads via kthread_create(). It is the parent of
all the other kernel threads.
def save(cpu_list, thread_list, filename):
@@ -295,7 +295,7 @@ def ps_show_header(has_ctxt_switch_info, cgroups=False):
def ps_show_sockets(pid, ps, inodes, inode_re, indent=0):
header_printed = False
- dirname = "/proc/%s/fd" % pid
+ dirname = f"/proc/{pid}/fd"
try:
filenames = os.listdir(dirname)
except: # Process died
@@ -688,7 +688,7 @@ def main():
try:
tuna.threads_set_priority(args.thread_list, args.priority,
args.affect_children)
except OSError as err:
- print("tuna: %s" % err)
+ print(f"tuna: {err}")
sys.exit(2)
elif args.command in ['show_configs']:
@@ -705,7 +705,7 @@ def main():
spread = args.command in ['spread', 'x']
if not (args.thread_list or args.irq_list):
- parser.error("tuna: %s " % (args.command) + _("requires a
thread/irq list!\n"))
+ parser.error(f"tuna: {args.command} requires a thread/irq
list!\n")
if args.thread_list:
tuna.move_threads_to_cpu(args.cpu_list, args.thread_list, spread=spread)
--