| 24 | 24 | |
| 25 | 25 | class Graph: |
| 26 | 26 | def __init__(self, data: list | None = None): |
| 27 | | pass |
| 27 | # Edge Case: Graph data is None |
| 28 | if data is None: |
| 29 | data = [] |
| 30 | |
| 31 | # Edge Case: Graph data is not a list |
| 32 | if not isinstance(data, list): |
| 33 | raise TypeError("Graph data malformed") |
| 34 | |
| 35 | self.nodes = [] |
| 36 | self.edges = [] |
| 37 | self.attrs = {} |
| 38 | |
| 39 | for item in data: |
| 40 | # Edge Case: Graph item is not a tuple |
| 41 | if not isinstance(item, tuple): |
| 42 | raise TypeError("Graph item malformed") |
| 43 | |
| 44 | # Edge Case: Graph item is empty |
| 45 | if len(item) == 0: |
| 46 | raise TypeError("Graph item malformed") |
| 47 | |
| 48 | item_type = item[0] |
| 49 | |
| 50 | # Edge Case: Node item with incorrect number of elements |
| 51 | if item_type == NODE: |
| 52 | if len(item) != 3: |
| 53 | raise TypeError("Graph item malformed") |
| 54 | name, attrs = item[1], item[2] |
| 55 | # Edge Case: Node name is not a string |
| 56 | if not isinstance(name, str): |
| 57 | raise TypeError("Node name must be a string") |
| 58 | # Edge Case: Node attributes is not a dict |
| 59 | if not isinstance(attrs, dict): |
| 60 | raise TypeError("Node attributes must be a dict") |
| 61 | self.nodes.append(Node(name, attrs)) |
| 62 | |
| 63 | # Edge Case: Edge item with incorrect number of elements |
| 64 | elif item_type == EDGE: |
| 65 | if len(item) != 4: |
| 66 | raise TypeError("Graph item malformed") |
| 67 | src, dst, attrs = item[1], item[2], item[3] |
| 68 | # Edge Case: Edge source is not a string |
| 69 | if not isinstance(src, str): |
| 70 | raise TypeError("Edge source must be a string") |
| 71 | # Edge Case: Edge destination is not a string |
| 72 | if not isinstance(dst, str): |
| 73 | raise TypeError("Edge destination must be a string") |
| 74 | # Edge Case: Edge attributes is not a dict |
| 75 | if not isinstance(attrs, dict): |
| 76 | raise TypeError("Edge attributes must be a dict") |
| 77 | self.edges.append(Edge(src, dst, attrs)) |
| 78 | |
| 79 | # Edge Case: Attribute item with incorrect number of elements |
| 80 | elif item_type == ATTR: |
| 81 | if len(item) != 3: |
| 82 | raise TypeError("Graph item malformed") |
| 83 | key, value = item[1], item[2] |
| 84 | # Edge Case: Attribute key is not a string |
| 85 | if not isinstance(key, str): |
| 86 | raise TypeError("Attribute key must be a string") |
| 87 | # Edge Case: Attribute value is not a string |
| 88 | if not isinstance(value, str): |
| 89 | raise TypeError("Attribute value must be a string") |
| 90 | self.attrs[key] = value |
| 91 | |
| 92 | # Edge Case: Unknown item type |
| 93 | else: |
| 94 | raise ValueError("Unknown item") |
| 95 | |
| 96 | # Handled Edge Cases: Graph data is None; Graph data is not a list; Graph item is not a tuple; Graph item is empty; Node item with incorrect number of elements; Node name is not a string; Node attributes is not a dict; Edge item with incorrect number of elements; Edge source is not a string; Edge destination is not a string; Edge attributes is not a dict; Attribute item with incorrect number of elements; Attribute key is not a string; Attribute value is not a string; Unknown item type |
| 97 | |
| 98 | def get_nodes(self): |
| 99 | """Return the list of nodes in the graph.""" |
| 100 | return self.nodes |
| 101 | |
| 102 | def get_edges(self): |
| 103 | """Return the list of edges in the graph.""" |
| 104 | return self.edges |
| 105 | |
| 106 | def get_attrs(self): |
| 107 | """Return the dictionary of attributes for the graph.""" |
| 108 | return self.attrs |
| 109 | |
| 110 | # Handled Edge Cases: None (getter methods have no edge cases to handle) |