Skip to content

Commit 1ed8e84

Browse files
committed
update
1 parent b663735 commit 1ed8e84

File tree

7 files changed

+120
-11
lines changed

7 files changed

+120
-11
lines changed

visualize/graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def main():
2626
# Create a list of edges for a tree with 7 nodes
2727
edges = {1: [2, 3], 2: [3, 4, 5], 3: [6, 7]}
2828

29-
graph = Graph(7, edges)
29+
graph = Graph()
3030

3131

3232
if __name__ == "__main__":

visualize/graph.svg

Lines changed: 0 additions & 2 deletions
This file was deleted.

visualize/image.svg

Lines changed: 0 additions & 2 deletions
This file was deleted.

visualize/test.svg

Lines changed: 2 additions & 0 deletions
Loading

visualize/tests/test_visualizer.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import unittest
2-
from typing import List
3-
42
from graphics import Circle, Rectangle, Line
53
from graph import Graph
64
from graphics import GraphicObject
7-
from visualizer import Visualizer
5+
from visualizer import Visualizer, SvgVisualizer
6+
import svgwrite
7+
import os
8+
89

910
class TestVisualizer(unittest.TestCase):
1011
def test_visualize(self):
@@ -27,5 +28,57 @@ def test_visualize(self):
2728
self.assertIsInstance(graphic_objects[3], Line)
2829

2930

30-
if __name__ == '__main__':
31+
class TestSvgVisualizer(unittest.TestCase):
32+
def setUp(self):
33+
self.skip_tear_down = False
34+
self.filepath = ""
35+
36+
def tearDown(self):
37+
if self.skip_tear_down:
38+
return
39+
# Delete the file created by the test
40+
try:
41+
os.remove(self.filepath)
42+
except FileNotFoundError:
43+
pass
44+
45+
def test_draw(self):
46+
# Create a Graph object with a few nodes and edges
47+
graph = Graph()
48+
graph.add_edge(1, 2)
49+
graph.add_edge(2, 3)
50+
graph.add_edge(3, 4)
51+
52+
# Create an instance of SvgVisualizer
53+
visualizer = SvgVisualizer()
54+
55+
# Call the draw method
56+
objects = visualizer.visualize(graph)
57+
drawing = visualizer.draw(objects)
58+
59+
# Check that the returned value is an svgwrite.Drawing object
60+
self.assertIsInstance(drawing, svgwrite.Drawing)
61+
62+
def test_write(self):
63+
# Create a Graph object with a few nodes and edges
64+
self.filepath = "test.svg"
65+
graph = Graph()
66+
graph.add_edge(1, 2)
67+
graph.add_edge(2, 3)
68+
graph.add_edge(3, 4)
69+
70+
# Create an instance of SvgVisualizer
71+
visualizer = SvgVisualizer()
72+
graphic_objects = visualizer.visualize(graph)
73+
74+
# Call the write method
75+
visualizer.write(self.filepath, graphic_objects)
76+
77+
# Check that the file was created
78+
self.assertTrue(os.path.exists(self.filepath))
79+
80+
self.skip_tear_down = True
81+
82+
83+
if __name__ == "__main__":
3184
unittest.main()

visualize/tree.svg

Lines changed: 0 additions & 2 deletions
This file was deleted.

visualize/visualizer.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import List
22
from graph import Graph
33
from graphics import GraphicObject, Circle, Line
4+
import svgwrite
45

56

67
class Visualizer:
@@ -27,3 +28,62 @@ def visualize(self, graph: Graph) -> List[GraphicObject]:
2728
for v in vs:
2829
graphic_objects.append(Line(positions[u], positions[v]))
2930
return graphic_objects
31+
32+
33+
class SvgVisualizer(Visualizer):
34+
def __init__(self):
35+
super().__init__()
36+
37+
def draw(self, graphic_objects: List[GraphicObject]) -> svgwrite.Drawing:
38+
"""Converts a list of GraphicObject instances to an SVG Drawing object.
39+
40+
Args:
41+
graphic_objects: A list of GraphicObject instances.
42+
43+
Returns:
44+
An svgwrite.Drawing object.
45+
"""
46+
dwg = svgwrite.Drawing()
47+
for obj in graphic_objects:
48+
if obj.type == "circle":
49+
dwg.add(self._circle_to_svg(obj))
50+
elif obj.type == "line":
51+
dwg.add(self._line_to_svg(obj))
52+
# Add code to handle other types of GraphicObjects
53+
return dwg
54+
55+
def write(self, filepath: str, graphic_objects: List[GraphicObject]):
56+
"""Writes a list of GraphicObject instances to an SVG file.
57+
58+
Args:
59+
filepath: The path to the output file.
60+
graphic_objects: A list of GraphicObject instances.
61+
"""
62+
dwg = self.draw(graphic_objects)
63+
dwg.saveas(filepath)
64+
65+
def _circle_to_svg(self, circle: Circle) -> svgwrite.shapes.Circle:
66+
"""Converts a Circle instance to an SVG Circle object.
67+
68+
Args:
69+
circle: A Circle instance.
70+
71+
Returns:
72+
An svgwrite.shapes.Circle object.
73+
"""
74+
x, y = circle.center
75+
r = circle.radius
76+
return svgwrite.shapes.Circle(center=(x, y), r=r)
77+
78+
def _line_to_svg(self, line: Line) -> svgwrite.shapes.Line:
79+
"""Converts a Line instance to an SVG Line object.
80+
81+
Args:
82+
line: A Line instance.
83+
84+
Returns:
85+
An svgwrite.shapes.Line object.
86+
"""
87+
x1, y1 = line.start
88+
x2, y2 = line.end
89+
return svgwrite.shapes.Line(start=(x1, y1), end=(x2, y2))

0 commit comments

Comments
 (0)