| 24 | 24 | |
| 25 | 25 | class Graph: |
| 26 | 26 | def __init__(self, data: list | None = None): |
| 27 | | pass |
| 27 | # Edge Case: Graph data is not a list |
| 28 | if not isinstance(data, list): |
| 29 | raise TypeError("Graph data malformed") |
| 30 | |
| 31 | self.nodes = [] |
| 32 | self.edges = [] |
| 33 | self.attrs = {} |
| 34 | |
| 35 | # Process each item in the data list |
| 36 | for item in data: |
| 37 | # Edge Case: Item is not a tuple |
| 38 | if not isinstance(item, tuple): |
| 39 | raise TypeError("Graph item malformed") |
| 40 | |
| 41 | # Edge Case: Item is empty tuple |
| 42 | if len(item) == 0: |
| 43 | raise TypeError("Graph item malformed") |
| 44 | |
| 45 | item_type = item[0] |
| 46 | |
| 47 | # Edge Case: Unknown item type |
| 48 | if item_type not in [NODE, EDGE, ATTR]: |
| 49 | raise ValueError("Unknown item") |
| 50 | |
| 51 | # Process based on item type |
| 52 | if item_type == NODE: |
| 53 | # Edge Case: Node tuple doesn't have exactly 3 elements |
| 54 | if len(item) != 3: |
| 55 | raise TypeError("Graph item malformed") |
| 56 | |
| 57 | node_name, node_attrs = item[1], item[2] |
| 58 | |
| 59 | # Edge Case: Node name is not a string |
| 60 | if not isinstance(node_name, str): |
| 61 | raise TypeError("Graph item malformed") |
| 62 | |
| 63 | # Edge Case: Node attributes is not a dict |
| 64 | if not isinstance(node_attrs, dict): |
| 65 | raise TypeError("Graph item malformed") |
| 66 | |
| 67 | self.nodes.append(Node(node_name, node_attrs)) |
| 68 | |
| 69 | elif item_type == EDGE: |
| 70 | # Edge Case: Edge tuple doesn't have exactly 4 elements |
| 71 | if len(item) != 4: |
| 72 | raise TypeError("Graph item malformed") |
| 73 | |
| 74 | src, dst, edge_attrs = item[1], item[2], item[3] |
| 75 | |
| 76 | # Edge Case: Edge source is not a string |
| 77 | if not isinstance(src, str): |
| 78 | raise TypeError("Graph item malformed") |
| 79 | |
| 80 | # Edge Case: Edge destination is not a string |
| 81 | if not isinstance(dst, str): |
| 82 | raise TypeError("Graph item malformed") |
| 83 | |
| 84 | # Edge Case: Edge attributes is not a dict |
| 85 | if not isinstance(edge_attrs, dict): |
| 86 | raise TypeError("Graph item malformed") |
| 87 | |
| 88 | self.edges.append(Edge(src, dst, edge_attrs)) |
| 89 | |
| 90 | elif item_type == ATTR: |
| 91 | # Edge Case: Attribute tuple doesn't have exactly 3 elements |
| 92 | if len(item) != 3: |
| 93 | raise TypeError("Graph item malformed") |
| 94 | |
| 95 | attr_name, attr_value = item[1], item[2] |
| 96 | |
| 97 | # Edge Case: Attribute name is not a string |
| 98 | if not isinstance(attr_name, str): |
| 99 | raise TypeError("Graph item malformed") |
| 100 | |
| 101 | # Edge Case: Attribute value is not a string |
| 102 | if not isinstance(attr_value, str): |
| 103 | raise TypeError("Graph item malformed") |
| 104 | |
| 105 | self.attrs[attr_name] = attr_value |
| 106 | # Handled Edge Cases: Graph data is None; Graph data is not a list; Item is not a tuple; Item is empty tuple; Unknown item type; Node tuple doesn't have exactly 3 elements; Node name is not a string; Node attributes is not a dict; Edge tuple doesn't have exactly 4 elements; Edge source is not a string; Edge destination is not a string; Edge attributes is not a dict; Attribute tuple doesn't have exactly 3 elements; Attribute name is not a string; Attribute value is not a string |