diff mbox series

[5/7] rteval: systopology.py: Use "with" for resource allocation

Message ID 20220603161224.10947-5-jkacur@redhat.com
State New
Headers show
Series [1/7] rteval: rteval-cmd: Use "with" with open | expand

Commit Message

John Kacur June 3, 2022, 4:12 p.m. UTC
- Use "with" with open

Signed-off-by: John Kacur <jkacur@redhat.com>
---
 rteval/systopology.py | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/rteval/systopology.py b/rteval/systopology.py
index b2da7bba7de0..e852f86e450f 100644
--- a/rteval/systopology.py
+++ b/rteval/systopology.py
@@ -180,13 +180,14 @@  class NumaNode:
     def getmeminfo(self):
         """ read info about memory attached to this node """
         self.meminfo = {}
-        for l in open(os.path.join(self.path, "meminfo"), "r"):
-            elements = l.split()
-            key = elements[2][0:-1]
-            val = int(elements[3])
-            if len(elements) == 5 and elements[4] == "kB":
-                val *= 1024
-            self.meminfo[key] = val
+        with open(os.path.join(self.path, "meminfo"), "r") as fp:
+            for l in fp:
+                elements = l.split()
+                key = elements[2][0:-1]
+                val = int(elements[3])
+                if len(elements) == 5 and elements[4] == "kB":
+                    val *= 1024
+                self.meminfo[key] = val
 
     def getcpustr(self):
         """ return list of cpus for this node as a string """
@@ -212,13 +213,14 @@  class SimNumaNode(NumaNode):
 
     def getmeminfo(self):
         self.meminfo = {}
-        for l in open(SimNumaNode.mempath, "r"):
-            elements = l.split()
-            key = elements[0][0:-1]
-            val = int(elements[1])
-            if len(elements) == 3 and elements[2] == "kB":
-                val *= 1024
-            self.meminfo[key] = val
+        with open(SimNumaNode.mempath, "r") as fp:
+            for l in fp:
+                elements = l.split()
+                key = elements[0][0:-1]
+                val = int(elements[1])
+                if len(elements) == 3 and elements[2] == "kB":
+                    val *= 1024
+                self.meminfo[key] = val
 
 #
 # Class to abstract the system topology of numa nodes and cpus