Condense various conditionals in print statements into a single line
to improve readability. Additionally, remove unused header_printed
variable.
Small fix, print_header changed to header_printed.
Signed-off-by: Leah Leshchinsky <lleshchi(a)redhat.com>
---
tuna-cmd.py | 28 +++++++++-------------------
1 file changed, 9 insertions(+), 19 deletions(-)
diff --git a/tuna-cmd.py b/tuna-cmd.py
index 7e33a128d676..e5d24ff84077 100755
--- a/tuna-cmd.py
+++ b/tuna-cmd.py
@@ -156,14 +156,10 @@ def ps_show_header(has_ctxt_switch_info, cgroups=False):
has_ctxt_switch_info and " %9s %12s" % (
"voluntary", "nonvoluntary")
or "", "cmd"), end=' ')
- if cgroups:
- print(" %7s" % ("cgroup"))
- else:
- print("")
+ print(" %7s" % ("cgroup") if cgroups else "")
def ps_show_sockets(pid, ps, inodes, inode_re, indent=0):
- header_printed = False
dirname = "/proc/%s/fd" % pid
try:
filenames = os.listdir(dirname)
@@ -182,12 +178,10 @@ def ps_show_sockets(pid, ps, inodes, inode_re, indent=0):
inode = int(inode_match.group(1))
if inode not in inodes:
continue
- if not header_printed:
- print("%s%-10s %-6s %-6s %15s:%-5s %15s:%-5s" %
- (sindent, "State", "Recv-Q", "Send-Q",
- "Local Address", "Port",
- "Peer Address", "Port"))
- header_printed = True
+ print("%s%-10s %-6s %-6s %15s:%-5s %15s:%-5s" %
+ (sindent, "State", "Recv-Q", "Send-Q",
+ "Local Address", "Port",
+ "Peer Address", "Port"))
s = inodes[inode]
print("%s%-10s %-6d %-6d %15s:%-5d %15s:%-5d" %
(sindent, s.state(),
@@ -203,7 +197,6 @@ def format_affinity(affinity):
ncpus = os.sysconf('SC_NPROCESSORS_CONF')
return ",".join(str(hex(a)) for a in procfs.hexbitmask(affinity, ncpus))
-
def ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info, sock_inodes,
sock_inode_re, cgroups):
global irqs
@@ -246,15 +239,12 @@ def ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info, sock_inodes,
ctxt_switch_info = " %9d %12s" % (voluntary_ctxt_switches,
nonvoluntary_ctxt_switches)
- if affect_children:
- print(" %-5d " % pid, end=' ')
- else:
- print(" %-5d" % pid, end=' ')
+ # Indent affected children
+ print(" %-5d " % pid if affect_children else " %-5d" % pid, end=' ')
print("%6s %5d %8s%s %15s %s" % (sched, rtprio, affinity,
ctxt_switch_info, cmd, users), end=' ')
- if cgroups:
- print(" %9s" % cgout, end=' ')
- print("")
+ print(" %9s" % cgout if cgroups else "")
+
if sock_inodes:
ps_show_sockets(pid, ps, sock_inodes, sock_inode_re,
affect_children and 3 or 4)
--
2.27.0