systematic use of context managers for dealing with files instead of open()/close...
[nepi.git] / src / nepi / data / processing / ping / parser.py
index b8132e5..b51f37f 100644 (file)
@@ -39,29 +39,27 @@ def parse_file(filename):
 
     """
 
-    f = open(filename, "r")
+    with open(filename, "r") as f:
 
-    # Traceroute info
-    target_ip = None
-    target_hostname = None
+        # Traceroute info
+        target_ip = None
+        target_hostname = None
    
-    data = []
-
-    for line in f:
-        # match traceroute to ...
-        m = re.match(_rre, line)
-        if not m:
-            continue
-
-        target_ip = m.groupdict()["ip"]
-        # FIX THIS: Make sure the regular expression does not inlcude 
-        # the ')' in the ip group 
-        target_ip = target_ip.replace(")","")
-        target_hostname = m.groupdict()["hostname"]
-        time = m.groupdict()["time"]
-        data.append((target_ip, target_hostname, time))
-
-    f.close()
+        data = []
+
+        for line in f:
+            # match traceroute to ...
+            m = re.match(_rre, line)
+            if not m:
+                continue
+
+            target_ip = m.groupdict()["ip"]
+            # FIX THIS: Make sure the regular expression does not inlcude 
+            # the ')' in the ip group 
+            target_ip = target_ip.replace(")","")
+            target_hostname = m.groupdict()["hostname"]
+            time = m.groupdict()["time"]
+            data.append((target_ip, target_hostname, time))
 
     return data