File tree Expand file tree Collapse file tree 2 files changed +13
-16
lines changed Expand file tree Collapse file tree 2 files changed +13
-16
lines changed Original file line number Diff line number Diff line change @@ -1278,9 +1278,8 @@ n = 14 # Total number of web pages (nodes)
12781278# * Q[i, j] = 1 if there is a link from i to j
12791279# * Q[i, j] = 0 otherwise
12801280Q = np.zeros((n, n), dtype=int)
1281- f = open(infile, 'r')
1282- edges = f.readlines()
1283- f.close()
1281+ with open(infile) as f:
1282+ edges = f.readlines()
12841283for edge in edges:
12851284 from_node, to_node = re.findall('\w', edge)
12861285 i, j = alphabet.index(from_node), alphabet.index(to_node)
Original file line number Diff line number Diff line change @@ -399,19 +399,17 @@ def map_graph_to_distance_matrix(in_file):
399399 Q = np.full((num_nodes, num_nodes), np.inf)
400400
401401 # Now we read in the data and modify Q
402- infile = open(in_file)
403- for line in infile:
404- elements = line.split(',')
405- node = elements.pop(0)
406- node = int(node[4:]) # convert node description to integer
407- if node != destination_node:
408- for element in elements:
409- destination, cost = element.split()
410- destination = int(destination[4:])
411- Q[node, destination] = float(cost)
412- Q[destination_node, destination_node] = 0
413-
414- infile.close()
402+ with open(in_file) as infile:
403+ for line in infile:
404+ elements = line.split(',')
405+ node = elements.pop(0)
406+ node = int(node[4:]) # convert node description to integer
407+ if node != destination_node:
408+ for element in elements:
409+ destination, cost = element.split()
410+ destination = int(destination[4:])
411+ Q[node, destination] = float(cost)
412+ Q[destination_node, destination_node] = 0
415413 return Q
416414```
417415
You can’t perform that action at this time.
0 commit comments