| 24 | 24 | |
| 25 | 25 | class Graph: |
| 26 | 26 | def __init__(self, data: list | None = None): |
| 27 | | pass |
| 27 | """ |
| 28 | Initialize a Graph with DSL data. |
| 29 | |
| 30 | Parameters: |
| 31 | data (list | None): A list of tuples describing the graph. Each tuple represents |
| 32 | either an attribute, a Node, or an Edge. |
| 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: Graph data is not a list |
| 39 | if data is not None and not isinstance(data, list): |
| 40 | raise TypeError("Graph data malformed") |
| 41 | |
| 42 | # Initialize graph components |
| 43 | self.nodes = [] |
| 44 | self.edges = [] |
| 45 | self.attrs = {} |
| 46 | |
| 47 | # Edge Case: Empty data (None or empty list) |
| 48 | if data is None or len(data) == 0: |
| 49 | return |
| 50 | |
| 51 | # Process each item in the data |
| 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: Item is an empty tuple |
| 58 | if len(item) == 0: |
| 59 | raise TypeError("Graph item malformed") |
| 60 | |
| 61 | item_type = item[0] |
| 62 | |
| 63 | # Edge Case: Item type is ATTR |
| 64 | if item_type == ATTR: |
| 65 | # Edge Case: ATTR tuple doesn't have exactly 3 elements |
| 66 | if len(item) != 3: |
| 67 | raise TypeError("Graph item malformed") |
| 68 | |
| 69 | key, value = item[1], item[2] |
| 70 | self.attrs[key] = value |
| 71 | |
| 72 | # Edge Case: Item type is NODE |
| 73 | elif item_type == NODE: |
| 74 | # Edge Case: NODE tuple doesn't have exactly 3 elements |
| 75 | if len(item) != 3: |
| 76 | raise TypeError("Graph item malformed") |
| 77 | |
| 78 | name, attrs = item[1], item[2] |
| 79 | # Edge Case: Node attributes is not a dict |
| 80 | if not isinstance(attrs, dict): |
| 81 | raise TypeError("Node attributes must be a dictionary") |
| 82 | |
| 83 | self.nodes.append(Node(name, attrs)) |
| 84 | |
| 85 | # Edge Case: Item type is EDGE |
| 86 | elif item_type == EDGE: |
| 87 | # Edge Case: EDGE tuple doesn't have exactly 4 elements |
| 88 | if len(item) != 4: |
| 89 | raise TypeError("Graph item malformed") |
| 90 | |
| 91 | src, dst, attrs = item[1], item[2], item[3] |
| 92 | # Edge Case: Edge attributes is not a dict |
| 93 | if not isinstance(attrs, dict): |
| 94 | raise TypeError("Edge attributes must be a dictionary") |
| 95 | |
| 96 | self.edges.append(Edge(src, dst, attrs)) |
| 97 | |
| 98 | # Edge Case: Unknown item type |
| 99 | else: |
| 100 | raise ValueError("Unknown item") |
| 101 | |
| 102 | # Handled Edge Cases: Graph data not a list, empty data, item not a tuple, |
| 103 | # empty tuple, ATTR with wrong element count, |
| 104 | # NODE with wrong element count, Node attributes not dict, |
| 105 | # EDGE with wrong element count, Edge attributes not dict, |
| 106 | # unknown item type |