Skip to content

Commit 788e2a6

Browse files
committed
update
1 parent f878e20 commit 788e2a6

File tree

11 files changed

+287
-0
lines changed

11 files changed

+287
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ bazel-testlogs
77
bazel-*
88
*.md2
99
test_data
10+
__pycache__
11+
htmlcov
12+
.coverage

visualize/.coveragerc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[report]
2+
exclude_lines =
3+
unittest.main()
4+
main()

visualize/__init__.py

Whitespace-only changes.

visualize/colors.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class Color:
2+
RED_500 = '#F44336'
3+
RED_50 = '#FFEBEE'
4+
RED_100 = '#FFCDD2'
5+
RED_200 = '#EF9A9A'
6+
RED_300 = '#E57373'
7+
RED_400 = '#EF5350'
8+
RED_500 = '#F44336'
9+
RED_600 = '#E53935'
10+
RED_700 = '#D32F2F'
11+
RED_800 = '#C62828'
12+
RED_900 = '#B71C1C'
13+
RED_A100 = '#FF8A80'
14+
RED_A200 = '#FF5252'
15+
RED_A400 = '#FF1744'
16+
RED_A700 = '#D50000'
17+
18+
PINK_500 = '#E91E63'
19+
PINK_50 = '#FCE4EC'
20+
PINK_100 = '#F8BBD0'
21+
PINK_200 = '#F48FB1'
22+
PINK_300 = '#F06292'
23+
PINK_400 = '#EC407A'
24+
PINK_500 = '#E91E63'
25+
PINK_600 = '#D81B60'
26+
PINK_700 = '#C2185B'
27+
PINK_800 = '#AD1457'
28+
PINK_900 = '#880E4F'
29+
PINK_A100 = '#FF80AB'
30+
PINK_A200 = '#FF4081'
31+
PINK_A400 = '#F50057'
32+
PINK_A700 = '#C51162'
33+
34+
PURPLE_500 = '#9C27B0'
35+
PURPLE_50 = '#F3E5F5'
36+
PURPLE_100 = '#E1BEE7'
37+
PURPLE_200 = '#CE93D8'
38+
PURPLE_300 = '#BA68C8'
39+
PURPLE_400 = '#AB47BC'
40+
PURPLE_500 = '#9C27B0'
41+
PURPLE_600 = '#8E24AA'
42+
PURPLE_700 = '#7B1FA2'
43+
PURPLE_800 = '#6A1B9A'
44+
PURPLE_900 = '#4A148C'
45+
PURPLE_A100 = '#EA80FC'
46+
PURPLE_A200 = '#E040FB'
47+
PURPLE_A400 = '#D500F9'
48+
PURPLE_A700 = '#AA00FF'
49+
50+
BLUE_500 = '#2196F3'
51+
BLUE_50 = '#E3F2FD'
52+
BLUE_100 = '#BBDEFB'
53+
BLUE_200 = '#90CAF9'
54+
BLUE_300 = '#64B5F6'
55+
BLUE_400 = '#42A5F5'
56+
BLUE_500 = '#2196F3'
57+
BLUE_600 = '#1E88E5'
58+
BLUE_700 = '#1976D2'
59+
BLUE_800 = '#1565C0'
60+
BLUE_900 = '#0D47A1'
61+
BLUE_A100 = '#82B1FF'
62+
BLUE_A200 = '#448AFF'
63+
BLUE_A400 = '#2979FF'
64+
BLUE_A700 = '#2962FF'

visualize/graph.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import svgwrite
2+
3+
class Graph:
4+
def __init__(self, directed=False):
5+
self.nodes = set()
6+
self.edges = {}
7+
self.directed = directed
8+
9+
def add_edge(self, u, v):
10+
self.nodes.add(u)
11+
self.nodes.add(v)
12+
if u in self.edges:
13+
self.edges[u].append(v)
14+
else:
15+
self.edges[u] = [v]
16+
if not self.directed:
17+
if v in self.edges:
18+
self.edges[v].append(u)
19+
else:
20+
self.edges[v] = [u]
21+
self.edges[v] = [u]
22+
23+
24+
25+
def main():
26+
# Create a list of edges for a tree with 7 nodes
27+
edges = {
28+
1: [2, 3],
29+
2: [3, 4, 5],
30+
3: [6, 7]
31+
}
32+
33+
if __name__ == '__main__':
34+
# Create a list of edges for a tree with 7 nodes
35+
edges = {
36+
1: [2, 3],
37+
2: [3, 4, 5],
38+
3: [6, 7]
39+
}
40+
41+
# Create an instance of the Graph class with 7 nodes and the list of edges
42+
graph = Graph(7, edges)
43+
44+
# Generate an SVG image of the tree and save it to a file
45+
graph.renderSvg('graph.svg')
46+
47+
class Visualizer:
48+
def __init__(self):
49+
pass
50+
51+
def visualize(self, graph):
52+
# calculate the position of each nodes
53+
positions = []
54+
for i, u in enumerate(graph.nodes):
55+
x = i * 30
56+
y = 50
57+
self.position.push((x, y))

visualize/graph.svg

Lines changed: 2 additions & 0 deletions
Loading

visualize/graphics.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class GraphicObject:
2+
def __init__(self, type, **kwargs):
3+
self.type = type
4+
self.kwargs = kwargs
5+
6+
class Circle(GraphicObject):
7+
def __init__(self, center, radius, **kwargs):
8+
super().__init__("circle", **kwargs)
9+
self.center = center
10+
self.radius = radius
11+
12+
class Rectangle(GraphicObject):
13+
def __init__(self, top_left, bottom_right, **kwargs):
14+
super().__init__("rectangle", **kwargs)
15+
self.top_left = top_left
16+
self.bottom_right = bottom_right
17+
18+
class Line(GraphicObject):
19+
def __init__(self, start, end, **kwargs):
20+
super().__init__("line", **kwargs)
21+
self.start = start
22+
self.end = end

visualize/image.svg

Lines changed: 2 additions & 0 deletions
Loading

visualize/tests/test_graph.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import unittest
2+
3+
from graph import Graph
4+
5+
class TestGraph(unittest.TestCase):
6+
def test_init(self):
7+
graph = Graph()
8+
self.assertEqual(graph.nodes, set())
9+
self.assertEqual(graph.edges, {})
10+
self.assertFalse(graph.directed)
11+
12+
directed_graph = Graph(directed=True)
13+
self.assertTrue(directed_graph.directed)
14+
15+
def test_add_edge(self):
16+
graph = Graph()
17+
graph.add_edge(1, 2)
18+
self.assertEqual(graph.nodes, {1, 2})
19+
self.assertEqual(graph.edges, {1: [2], 2: [1]})
20+
21+
graph.add_edge(2, 3)
22+
self.assertEqual(graph.nodes, {1, 2, 3})
23+
self.assertEqual(graph.edges, {1: [2], 2: [1, 3], 3: [2]})
24+
25+
directed_graph = Graph(directed=True)
26+
directed_graph.add_edge(1, 2)
27+
self.assertEqual(directed_graph.nodes, {1, 2})
28+
self.assertEqual(directed_graph.edges, {1: [2]})
29+
30+
def test_add_edge_undirected(self):
31+
graph = Graph(directed=False)
32+
graph.add_edge(1, 2)
33+
self.assertEqual(graph.nodes, {1, 2})
34+
self.assertEqual(graph.edges, {1: [2], 2: [1]})
35+
36+
graph.add_edge(2, 3)
37+
self.assertEqual(graph.nodes, {1, 2, 3})
38+
self.assertEqual(graph.edges, {1: [2], 2: [1, 3], 3: [2]})
39+
40+
def test_add_edge_to_existing_node(self):
41+
graph = Graph(directed=False)
42+
graph.add_edge(1, 2)
43+
graph.add_edge(1, 3)
44+
graph.add_edge(2, 3)
45+
self.assertEqual(graph.nodes, {1, 2, 3})
46+
self.assertEqual(graph.edges, {1: [2, 3], 2: [1, 3], 3: [1, 2]})
47+
48+
if __name__ == '__main__':
49+
unittest.main()

visualize/tests/test_graphics.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import unittest
2+
3+
from graphics import Circle, Rectangle, Line
4+
5+
class TestCircle(unittest.TestCase):
6+
def test_init(self):
7+
circle = Circle((0, 0), 3, color='red', filled=True)
8+
self.assertEqual(circle.type, 'circle')
9+
self.assertEqual(circle.center, (0, 0))
10+
self.assertEqual(circle.radius, 3)
11+
self.assertEqual(circle.kwargs, {'color': 'red', 'filled': True})
12+
13+
def test_center_as_float(self):
14+
circle = Circle((0.5, 0.5), 3, color='red', filled=True)
15+
self.assertEqual(circle.center, (0.5, 0.5))
16+
17+
def test_negative_radius(self):
18+
circle = Circle((0, 0), -3, color='red', filled=True)
19+
self.assertEqual(circle.radius, -3)
20+
21+
def test_color_as_none(self):
22+
circle = Circle((0, 0), 3, color=None, filled=True)
23+
self.assertEqual(circle.kwargs, {'color': None, 'filled': True})
24+
25+
def test_additional_kwargs(self):
26+
circle = Circle((0, 0), 3, color='red', filled=True, linewidth=2)
27+
self.assertEqual(circle.kwargs, {'color': 'red', 'filled': True, 'linewidth': 2})
28+
29+
class TestRectangle(unittest.TestCase):
30+
def test_init(self):
31+
rectangle = Rectangle((0, 0), (3, 4), color='blue', filled=False)
32+
self.assertEqual(rectangle.type, 'rectangle')
33+
self.assertEqual(rectangle.top_left, (0, 0))
34+
self.assertEqual(rectangle.bottom_right, (3, 4))
35+
self.assertEqual(rectangle.kwargs, {'color': 'blue', 'filled': False})
36+
37+
def test_coordinates_as_float(self):
38+
rectangle = Rectangle((0.5, 0.5), (3.5, 4.5), color='blue', filled=False)
39+
self.assertEqual(rectangle.top_left, (0.5, 0.5))
40+
self.assertEqual(rectangle.bottom_right, (3.5, 4.5))
41+
42+
def test_negative_coordinates(self):
43+
rectangle = Rectangle((-1, -1), (-2, -2), color='blue', filled=False)
44+
self.assertEqual(rectangle.top_left, (-1, -1))
45+
self.assertEqual(rectangle.bottom_right, (-2, -2))
46+
47+
def test_color_as_none(self):
48+
rectangle = Rectangle((0, 0), (3, 4), color=None, filled=False)
49+
self.assertEqual(rectangle.kwargs, {'color': None, 'filled': False})
50+
51+
def test_additional_kwargs(self):
52+
rectangle = Rectangle((0, 0), (3, 4), color='blue', filled=False, linewidth=2)
53+
self.assertEqual(rectangle.kwargs, {'color': 'blue', 'filled': False, 'linewidth': 2})
54+
55+
class TestLine(unittest.TestCase):
56+
def test_init(self):
57+
line = Line((0, 0), (3, 4), color='green', dashed=True)
58+
self.assertEqual(line.type, 'line')
59+
self.assertEqual(line.start, (0, 0))
60+
self.assertEqual(line.end, (3, 4))
61+
self.assertEqual(line.kwargs, {'color': 'green', 'dashed': True})
62+
63+
def test_coordinates_as_float(self):
64+
line = Line((0.5, 0.5), (3.5, 4.5), color='green', dashed=True)
65+
self.assertEqual(line.start, (0.5, 0.5))
66+
self.assertEqual(line.end, (3.5, 4.5))
67+
68+
def test_negative_coordinates(self):
69+
line = Line((-1, -1), (-2, -2), color='green', dashed=True)
70+
self.assertEqual(line.start, (-1, -1))
71+
self.assertEqual(line.end, (-2, -2))
72+
73+
def test_color_as_none(self):
74+
line = Line((0, 0), (3, 4), color=None, dashed=True)
75+
self.assertEqual(line.kwargs, {'color': None, 'dashed': True})
76+
77+
def test_additional_kwargs(self):
78+
line = Line((0, 0), (3, 4), color='green', dashed=True, linewidth=2)
79+
self.assertEqual(line.kwargs, {'color': 'green', 'dashed': True, 'linewidth': 2})
80+
81+
if __name__ == '__main__':
82+
unittest.main()

visualize/tree.svg

Lines changed: 2 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)