-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path02. Composite.py
43 lines (29 loc) · 873 Bytes
/
02. Composite.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
class Component:
def __init__(self, name):
self.name = name
self.parent = None
def move(self, new_path):
new_folder = get_path(new_path)
del self.parent.children[self.name]
new_folder.children[self.name] = self
self.parent = new_folder
def delete(self):
del self.parent.children[self.name]
class Folder(Component):
def __init__(self, name):
super().__init__(name)
self.children = {}
def add_child(self, child):
self.parent = self
self.children[child.name] = child
class File(Component):
def __init__(self, name, contents):
super().__init__(name)
self.contents = contents
root = Folder('')
def get_path(path):
names = path.split('/')[1:]
node = root
for name in names:
node = node.children[name]
return node