-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmanager.py
More file actions
55 lines (41 loc) · 1.93 KB
/
manager.py
File metadata and controls
55 lines (41 loc) · 1.93 KB
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
from typing import List
import copy
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from nodes.node import Node
class NodeManager:
nodes: List[Node] # Adding the type hint for the 'nodes' attribute
# Initializes the NodeManager with a list of Node objects.
# Ensures that the input is a list of Node instances.
def __init__(self, nodes: List[Node]):
if not isinstance(nodes, list):
raise ValueError("The nodes must be a list of Node objects.")
if not all(isinstance(node, Node) for node in nodes):
raise ValueError("The nodes must be a list of Node objects.")
#This object must not mutate the received List
self.nodes: List[Node] = copy.deepcopy(nodes)
# Returns the number of nodes in the NodeManager.
def __len__(self) -> int:
return len(self.nodes)
# Retrieves a Node object by index from the nodes list.
def __getitem__(self, index: int) -> Node:
return self.nodes[index]
# Removes a Node from the manager.
# Raises a ValueError if the node is not in the list of nodes.
def remove(self, node: Node) -> None:
if node not in self.nodes:
raise ValueError("The node does not exist in the nodes member.")
self.nodes.remove(node)
# Removes a Node and all its children (cascade removal).
# If the node has children, they are recursively removed as well.
def remove_cascade(self, node: Node) -> None:
#Check if the node exists in the list
if node not in self.nodes:
raise ValueError("The node does not exist in the nodes member.")
# First remove the node itself
self.remove(node)
# Cascade removal of nodes where the parent is the removed node
to_remove = [n for n in self.nodes if n.parent == node.id]
for child in to_remove:
self.remove_cascade(child)