-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeometric_shapes.py
48 lines (37 loc) · 1.11 KB
/
geometric_shapes.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
class GraphicObject:
def __init__(self, color=None):
self.color = color
self.children = []
self._name = "Group"
@property
def name(self):
return self._name
def _print(self, items, depth):
items.append("*" * depth)
if self.color:
items.append(self.color)
items.append(f"{self.name}\n")
for child in self.children:
child._print(items, depth + 1)
def __str__(self):
items = []
self._print(items, 0)
return "".join(items)
class Circle(GraphicObject):
@property
def name(self):
return "Circle"
class Square(GraphicObject):
@property
def name(self):
return "Square"
if __name__ == "__main__":
drawing = GraphicObject()
drawing._name = "My Drawing"
drawing.children.append(Square("Red"))
drawing.children.append(Circle("Yellow"))
group = GraphicObject() # no name
group.children.append(Circle("Blue"))
group.children.append(Square("Blue"))
drawing.children.append(group)
print(drawing)