forked from cortesi/sortvis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
weave
executable file
·145 lines (135 loc) · 3.43 KB
/
weave
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
import random, math, sys
from optparse import OptionParser
from libsortvis import sortable, graph
def main():
usage = "usage: %prog [options]"
parser = OptionParser(usage)
parser.add_option(
"-a",
dest="algorithm",
default=[],
type="choice",
action="append",
choices=[i.name for i in sortable.algorithms],
help="Draw only a named algorithm."
)
parser.add_option(
"-n",
dest="numelements",
default="20",
type="int",
help="Generate a random sorting sequence of length n"
)
parser.add_option(
"-f",
dest="readfile",
help="Read data from file"
)
parser.add_option(
"-o",
dest="ofname",
help="Output file name. Only usable when a single algorithm is being drawn.",
default=""
)
parser.add_option(
"-d",
dest="dump",
default=False,
action="store_true",
help="Dump sequence"
)
parser.add_option(
"-p",
dest="printout",
default=False,
action="store_true",
help="Print traversal rather than drawing graph"
)
parser.add_option(
"-x",
dest="width",
type="int",
default=None,
help="Image width"
)
parser.add_option(
"-y",
dest="height",
type="int",
default=None,
help="Image height"
)
parser.add_option(
"-l",
dest="line",
type="int",
default=6,
help="Total line width"
)
parser.add_option(
"-b",
dest="border",
type="int",
default=1,
help="Border width"
)
parser.add_option(
"-r",
dest="rotate",
default=False,
action="store_true",
help="Rotate images 90 degrees"
)
parser.add_option(
"-t",
dest="title",
default=False,
action="store_true",
help="Add a title to image."
)
options, args = parser.parse_args()
if args:
parser.error("Script takes no arguments.")
if options.readfile:
txt = file(options.readfile).read().split()
lst = [int(i) for i in txt]
else:
lst = range(options.numelements)
random.shuffle(lst)
if options.dump:
for i in lst:
print i,
if not options.height:
options.height = (options.line + options.border + 5) * len(lst)
if not options.width:
options.width = int(options.height * 3)
ldrawer = graph.Weave(
options.width,
options.height,
)
if options.algorithm:
selected = [i.lower() for i in options.algorithm]
if options.algorithm:
todraw = [i for i in sortable.algorithms if i.name in options.algorithm]
else:
todraw = [i for i in sortable.algorithms]
if options.ofname and len(todraw) > 1:
parser.error("Cannot specify output file name when drawing more than one algorithm.")
for i in todraw:
a = i()
sortd = a(lst)
if options.printout:
print a.name
for i in sortd:
print i.path
ldrawer.draw(
sortd,
a.name if options.title else None,
options.ofname or (a.name+".png"),
options.line,
options.border,
rotate = options.rotate,
)
if __name__ == "__main__":
main()