Node_Base represents a node in the memory graph. This base class has different subclasses for different types of nodes.
| 10 | from abc import ABC, abstractmethod |
| 11 | |
| 12 | class Node_Base(ABC): |
| 13 | """ |
| 14 | Node_Base represents a node in the memory graph. This base class has different subclasses for different types of nodes. |
| 15 | """ |
| 16 | |
| 17 | def __init__(self, data, children=None): |
| 18 | """ |
| 19 | Create a Node_Base object. |
| 20 | """ |
| 21 | self.data = data |
| 22 | self.children = Sequence1D([]) if children is None else children |
| 23 | self.parent_indices = {} |
| 24 | |
| 25 | def __repr__(self): |
| 26 | """ |
| 27 | Return a string representation of the node showing the original data represented by the node. |
| 28 | """ |
| 29 | return f'Node with {self.get_type_name()}' |
| 30 | |
| 31 | def add_parent_index(self, parent, parent_index): |
| 32 | """ |
| 33 | Add a parent to the node. |
| 34 | """ |
| 35 | if not parent in self.parent_indices: |
| 36 | self.parent_indices[parent] = [] |
| 37 | self.parent_indices[parent].append(parent_index) |
| 38 | |
| 39 | def is_root(self): |
| 40 | """ |
| 41 | Return if the node is the root node. |
| 42 | """ |
| 43 | return len(self.parent_indices) == 0 |
| 44 | |
| 45 | def get_parent_indices(self): |
| 46 | return self.parent_indices |
| 47 | |
| 48 | def get_id(self): |
| 49 | """ |
| 50 | Return the id of the node. |
| 51 | """ |
| 52 | return id(self.data) |
| 53 | |
| 54 | def __eq__(self, other): |
| 55 | """ |
| 56 | Return if the node is equal to another node. |
| 57 | """ |
| 58 | return self.get_id() == other.get_id() |
| 59 | |
| 60 | def __hash__(self): |
| 61 | """ |
| 62 | Return the hash of the node. |
| 63 | """ |
| 64 | return self.get_id() |
| 65 | |
| 66 | def get_data(self): |
| 67 | """ |
| 68 | Return the original data represented by the node. |
| 69 | """ |
nothing calls this directly
no outgoing calls
no test coverage detected