diff options
author | John Kacur <jkacur@redhat.com> | 2022-08-22 14:32:01 -0400 |
---|---|---|
committer | John Kacur <jkacur@redhat.com> | 2022-08-22 14:33:34 -0400 |
commit | 5795eaa4fe5b249699c6a788305952deca99fae1 (patch) | |
tree | b39893710974511d3e6eb9f0361e52aadbb18b1a | |
parent | 6a3f1eea554cc81a1bfb407c9d9426ece96795c8 (diff) | |
download | python-linux-procfs-5795eaa4fe5b249699c6a788305952deca99fae1.tar.gz |
python-linux-procfs: Use f-strings
Use f-strings instead of regular strings to improve readability
Signed-off-by: John Kacur <jkacur@redhat.com>
-rwxr-xr-x | procfs/procfs.py | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/procfs/procfs.py b/procfs/procfs.py index 6e68ed1..404c9bc 100755 --- a/procfs/procfs.py +++ b/procfs/procfs.py @@ -164,7 +164,7 @@ class pidstat: def load(self, basedir="/proc"): try: - f = open("%s/%d/stat" % (basedir, self.pid)) + f = open(f"{basedir}/{self.pid}/stat") except FileNotFoundError: # The pid has disappeared, propagate the error raise @@ -322,7 +322,7 @@ class pidstatus: def load(self, basedir="/proc"): self.fields = {} - with open("%s/%d/status" % (basedir, self.pid)) as f: + with open(f"{basedir}/{self.pid}/status") as f: for line in f.readlines(): fields = line.split(":") if len(fields) != 2: @@ -379,7 +379,7 @@ class process: def load_cmdline(self): try: - with open("/proc/%d/cmdline" % self.pid) as f: + with open(f"/proc/{self.pid}/cmdline") as f: self.cmdline = f.readline().strip().split('\0')[:-1] except FileNotFoundError: """ This can happen when a pid disappears """ @@ -389,13 +389,13 @@ class process: self.cmdline = None def load_threads(self): - self.threads = pidstats("/proc/%d/task/" % self.pid) + self.threads = pidstats(f"/proc/{self.pid}/task/") # remove thread leader del self.threads[self.pid] def load_cgroups(self): self.cgroups = "" - with open("/proc/%d/cgroup" % self.pid) as f: + with open(f"/proc/{self.pid}/cgroup") as f: for line in reversed(f.readlines()): if len(self.cgroups) != 0: self.cgroups = self.cgroups + "," + line[:-1] @@ -429,7 +429,7 @@ class process: >>> """ self.environ = {} - with open("/proc/%d/environ" % self.pid) as f: + with open(f"/proc/{self.pid}/environ") as f: for x in f.readline().split('\0'): if len(x) > 0: y = x.split('=') @@ -558,13 +558,13 @@ class pidstats: priorities = "" processed_pids = [] while True: - name = "%s/%d" % (basename, cpu) + name = f"{basename}/{cpu}" pids = self.find_by_name(name) if not pids or len([n for n in pids if n not in processed_pids]) == 0: break for pid in pids: try: - priorities += "%s," % self.processes[pid]["stat"]["rt_priority"] + priorities += f'{self.processes[pid]["stat"]["rt_priority"]}' except IOError: # We're doing lazy loading of /proc files # So if we get this exception is because the @@ -586,7 +586,7 @@ class pidstats: break for pid in pids: try: - priorities += "%s," % self.processes[pid]["stat"]["rt_priority"] + priorities += f'{self.processes[pid]["stat"]["rt_priority"]}' except IOError: # We're doing lazy loading of /proc files # So if we get this exception is because the @@ -690,7 +690,7 @@ class interrupts: def parse_affinity(self, irq): try: - with open("/proc/irq/%s/smp_affinity" % irq) as f: + with open(f"/proc/irq/{irq}/smp_affinity") as f: line = f.readline() return bitmasklist(line, self.nr_cpus) except IOError: @@ -973,7 +973,7 @@ class smaps: def reload(self): line = None - with open("/proc/%d/smaps" % self.pid) as f: + with open(f"/proc/{self.pid}/smaps") as f: while True: line = self.parse_entry(f, line) if not line: @@ -1012,13 +1012,11 @@ class cpustat: self.guest = int(fields[8]) def __repr__(self): - s = "< user: %s, nice: %s, system: %s, idle: %s, iowait: %s, irq: %s, softirq: %s" % \ - (self.user, self.nice, self.system, self.idle, - self.iowait, self.irq, self.softirq) + s = f"< user: {self.user}, nice: {self.nice}, system: {self.system}, idle: {self.idle}, iowait: {self.iowait}, irq: {self.irq}, softirq: {self.softirq}" if hasattr(self, 'steal'): - s += ", steal: %d" % self.steal + s += f", steal: {self.steal}" if hasattr(self, 'guest'): - s += ", guest: %d" % self.guest + s += f", guest: {self.guest}" return s + ">" @@ -1094,21 +1092,21 @@ if __name__ == '__main__': ints = interrupts() for i in list(ints.interrupts.keys()): - print("%s: %s" % (i, ints.interrupts[i])) + print(f"{i}: {ints.interrupts[i]}") options = cmdline() for o in list(options.options.keys()): - print("%s: %s" % (o, options.options[o])) + print(f"{o}: {options.options[o]}") cpu = cpuinfo() - print("\ncpuinfo data: %d processors" % cpu.nr_cpus) + print(f"\ncpuinfo data: {cpu.nr_cpus} processors") for tag in list(cpu.keys()): - print("%s=%s" % (tag, cpu[tag])) + print(f"{tag}={cpu[tag]}") print("smaps:\n" + ("-" * 40)) s = smaps(int(sys.argv[1])) for i in range(s.nr_entries): - print("%#x %s" % (s.entries[i].vm_start, s.entries[i].name)) + print(f"{s.entries[i].vm_start:#x} {s.entries[i].name}") print("-" * 40) for a in s.find_by_name_fragment(sys.argv[2]): print(a["Size"]) |