-
Notifications
You must be signed in to change notification settings - Fork 0
/
composite_example.py
96 lines (75 loc) · 2.2 KB
/
composite_example.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
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
from __future__ import annotations
from abc import ABCMeta, abstractmethod
class Node(metaclass=ABCMeta):
def __init__(self, name: str) -> None:
self.name = name
self.parent: Branch | None = None
@abstractmethod
def __str__(self) -> str:
...
def attach(self, node: Node) -> None:
raise NotImplementedError
@abstractmethod
def pprint(self, prefix: str = "") -> None:
...
class Branch(Node):
def __init__(self, name: str) -> None:
super().__init__(name)
self.subnodes: list[Node] = []
def __str__(self) -> str:
return f"Branch({self.name!r})"
def attach(self, node: Node) -> None:
if node.parent is not None: # 親は1つ
raise ValueError("%s is already attached", node)
self.subnodes.append(node)
node.parent = self
def pprint(self, prefix: str = "") -> None:
"""
>>> branch = Branch("b")
>>> leaf = Leaf("l")
>>> branch.attach(leaf)
>>> branch.pprint()
Branch('b')
Leaf('l')
>>> parent_branch = Branch("p")
>>> child_branch = Branch("c")
>>> leaf1 = Leaf("l1")
>>> leaf2 = Leaf("l2")
>>> parent_branch.attach(child_branch)
>>> child_branch.attach(leaf1)
>>> parent_branch.attach(leaf2)
>>> parent_branch.pprint()
Branch('p')
Branch('c')
Leaf('l1')
Leaf('l2')
"""
print(f"{prefix}{self}")
for subnode in self.subnodes:
subnode.pprint(prefix + " ")
class Leaf(Node):
def __str__(self) -> str:
return f"Leaf({self.name!r})"
def pprint(self, prefix: str = "") -> None:
print(f"{prefix}{self}")
if __name__ == "__main__":
root_branch = Branch("root")
branch1 = Branch("branch1")
branch2 = Branch("branch2")
node1 = Leaf("node1")
node2 = Leaf("node2")
node3 = Leaf("node3")
branch2.attach(node3)
branch1.attach(branch2)
branch1.attach(node2)
root_branch.attach(branch1)
root_branch.attach(node1)
root_branch.pprint()
"""
Branch('root')
Branch('branch1')
Branch('branch2')
Leaf('node3')
Leaf('node2')
Leaf('node1')
"""