-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_dot_old.py
70 lines (63 loc) · 2.42 KB
/
write_dot_old.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import pickle
from coursedef import Course
import sys
# write the dot file from the catalog
# note: only writes courses with prerequisites
rfile = 'catalog.pickle'
if len(sys.argv) < 2:
print('expected command line argument: <write file>')
exit()
wdot = sys.argv[1]
# shrink defines whether or not to graph courses with no prereqs
shrink = True
intree = set()
# write the node for this course
def wnode(f, t):
# level-dependent shaping (courses with different #s
# of prereqs get differently shaped outlines
if False:
if t[5] == '1':
f.write(f'\n"{t}" [shape=oval]')
elif t[5] == '2':
f.write(f'\n"{t}" [shape=doublecircle]')
elif t[5] == '3':
f.write(f'\n"{t}" [shape=triangle]')
elif t[5] == '4':
f.write(f'\n"{t}" [shape=box]')
else:
raise ValueError('GRADUATE COURSE')
else:
# plain text (no shaped outlines)
f.write(f'\n"{t}" [shape=plain fontsize="8"]')
# write the edge for from (prereq) to to (course)
def wedge(f, fm, to):
f.write(f'\n"{fm}" -> "{to}" [color="black:invis:black" weight="2"]')
with open(rfile, 'rb') as f:
cc = pickle.load(f)
with open(wdot, 'w') as d:
# write graph type, RPI central node
d.write('digraph D {\nlayout=twopi; graph [ranksep="30 2" overlap_scale=-5];\nratio=auto;\nfontsize="5"\n"RPI" [shape=circle root=true fontsize="240" fontcolor="red"]')
# find the courses with prerequisites
for tt, cs in cc.items():
if tt[5] in ('1', '2', '3', '4'):
if len(cs.prereqs) > 0:
for p in cs.prereqs:
# add course to tree set
intree.add(p)
# write nodes
for tt, cs in cc.items():
if tt[5] in ('1', '2', '3', '4'):
if not shrink or len(cs.prereqs) > 0 or tt in intree:
wnode(d, tt[:4]+tt[5:])
# write edges
d.write('\n')
for tt, cs in cc.items():
if tt[5] in ('1', '2', '3', '4'):
if len(cs.prereqs) > 0:
for p in cs.prereqs:
wedge(d, p[:4]+p[5:], tt[:4]+tt[5:])
else:
if not shrink or tt in intree:
# connect no prereqs courses to central node
wedge(d, 'RPI', tt[:4]+tt[5:])
d.write('\n}')