Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
venv
__pycache__
*.pyc
*.pyc
.vscode/
.pytest_cache/
88 changes: 86 additions & 2 deletions nodes/manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,89 @@
# Create here the NodeManager Class
"""
This module defines the NodeManager class which manages a collection of Node objects.
"""

from typing import List
from .node import Node

class NodeManager:
pass
"""
Manages a collection of Node objects, allowing for removal of nodes and their children.

Attributes:
nodes (List[Node]): A list of Node objects managed by this instance.
"""
nodes: List[Node]

def __init__(self, nodes: List[Node]) -> None:
"""
Initializes the NodeManager with a list of Node objects.

Args:
nodes (List[Node]): A list of Node objects to be managed.

Raises:
ValueError: If nodes is not a list.
"""
if not isinstance(nodes, list):
raise ValueError("nodes must be a list")
self.nodes = nodes.copy()

def __len__(self) -> int:
"""
Returns the number of nodes managed by this instance.

Returns:
int: The number of nodes.
"""
return len(self.nodes)

def __getitem__(self, index: int) -> Node:
"""
Returns the Node at the specified index.

Args:
index (int): The index of the Node to retrieve.

Returns:
Node: The Node at the specified index.
"""
return self.nodes[index]

def remove(self, node: Node) -> None:
"""
Removes the specified Node from the manager.

Args:
node (Node): The Node to be removed.

Raises:
ValueError: If the Node is not found in the manager.
"""
if node not in self.nodes:
raise ValueError("Node not found in the manager")
self.nodes.remove(node)

def remove_cascade(self, node: Node) -> None:
"""
Removes the specified Node and all its children from the manager.

Args:
node (Node): The Node to be removed along with its children.

Raises:
ValueError: If the Node is not found in the manager.
"""
if node not in self.nodes:
raise ValueError("Node not found in the manager")

to_remove = [node]
while to_remove:
current = to_remove.pop()
children = [n for n in self.nodes if n.parent == current.id]
to_remove.extend(children)
self.nodes.remove(current)

for n in self.nodes[:]:
if n.parent == node.id:
self.nodes.remove(n)

31 changes: 28 additions & 3 deletions nodes/node.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# Create here the Node Class

"""
This module defines the Node class which represents a node with an id and a parent id.
"""

class Node:
pass
"""
Represents a node with an id and a parent id.

Attributes:
id (int): The unique identifier of the node.
parent (int): The identifier of the parent node.
"""
id: int
parent: int

def __init__(self, id: int, parent: int):
if not isinstance(id, int):
raise ValueError("id must be an integer")
if not isinstance(parent, int):
raise ValueError("parent must be an integer")
if parent >= id:
raise ValueError("Parent cannot be greater than or equal to id.")
if parent == id:
raise ValueError("Parent cannot be the same as id.")
self.id = id
self.parent = parent

def __repr__(self):
return f"Node({self.id}, {self.parent})"