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