forked from vannynguyen/campus_map
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_graph.py
More file actions
35 lines (31 loc) · 1.25 KB
/
load_graph.py
File metadata and controls
35 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from vertex import Vertex
def load_graph(filename):
#dictionary of vertices
vertices = {}
#reading from file; fill dictionary with vertices
in_file = open(filename, "r")
for line in in_file:
#strip away whitespace and then delimit semicolons
clean_line = line.strip()
info = clean_line.split(";")
#separate coordinates
coordinates = info[2].strip().split(",")
x = coordinates[0].strip()
y = coordinates[1].strip()
#create Vertex based off info
generic_vertex = Vertex(info[0],x,y)
#add Vertex to list, name = key and reference = value
vertices[info[0]] = generic_vertex
in_file.close()
#second time looping; fill in adjacent vertices
in_file = open(filename, "r")
for line in in_file:
#strip away whitespace and then delimit semicolons
clean_line = line.strip()
info = clean_line.split(";")
#for each line, find vertex reference using dictionary and then add references to adjacent vertices
for adjacent_vertex in info[1].split(","):
av = adjacent_vertex.strip()
vertices[info[0]].list.append(vertices[av])
vertices[info[0]].list_of_names.append(av)
return vertices