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