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