| 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 class takes a list of tuples that describe attributes, nodes, and edges. |
| 30 | Each tuple must follow specific formats based on its type (ATTR, NODE, EDGE). |
| 31 | """ |
| 32 | |
| 26 | 33 | def __init__(self, data: list | None = None): |
| 27 | | pass |
| 34 | """ |
| 35 | Initialize a Graph with optional data. |
| 36 | |
| 37 | Args: |
| 38 | data: A list of tuples describing the graph. Each tuple must be of the form: |
| 39 | - (ATTR, key, value) for graph attributes |
| 40 | - (NODE, name, attrs) for nodes |
| 41 | - (EDGE, src, dst, attrs) for edges |
| 42 | |
| 43 | Raises: |
| 44 | TypeError: If data is not a list or if any tuple is malformed |
| 45 | ValueError: If an unknown item type is encountered |
| 46 | """ |
| 47 | # Edge Case: Handle None input by initializing empty collections |
| 48 | if data is None: |
| 49 | self.attrs = {} |
| 50 | self.nodes = [] |
| 51 | self.edges = [] |
| 52 | return |
| 53 | |
| 54 | # Edge Case: Data must be a list |
| 55 | if not isinstance(data, list): |
| 56 | raise TypeError("Graph data malformed") |
| 57 | |
| 58 | self.attrs = {} |
| 59 | self.nodes = [] |
| 60 | self.edges = [] |
| 61 | |
| 62 | # Process each item in the data list |
| 63 | for item in data: |
| 64 | # Edge Case: Each item must be a tuple |
| 65 | if not isinstance(item, tuple): |
| 66 | raise TypeError("Graph item malformed") |
| 67 | |
| 68 | # Edge Case: Empty tuple is malformed |
| 69 | if len(item) == 0: |
| 70 | raise TypeError("Graph item malformed") |
| 71 | |
| 72 | item_type = item[0] |
| 73 | |
| 74 | # Edge Case: Handle attribute items (ATTR, key, value) |
| 75 | if item_type == ATTR: |
| 76 | if len(item) != 3: |
| 77 | raise TypeError("Graph item malformed") |
| 78 | self.attrs[item[1]] = item[2] |
| 79 | |
| 80 | # Edge Case: Handle node items (NODE, name, attrs) |
| 81 | elif item_type == NODE: |
| 82 | if len(item) != 3: |
| 83 | raise TypeError("Graph item malformed") |
| 84 | # Edge Case: Node attributes must be a dict |
| 85 | if not isinstance(item[2], dict): |
| 86 | raise TypeError("Node attributes must be a dict") |
| 87 | self.nodes.append(Node(item[1], item[2])) |
| 88 | |
| 89 | # Edge Case: Handle edge items (EDGE, src, dst, attrs) |
| 90 | elif item_type == EDGE: |
| 91 | if len(item) != 4: |
| 92 | raise TypeError("Graph item malformed") |
| 93 | # Edge Case: Edge attributes must be a dict |
| 94 | if not isinstance(item[3], dict): |
| 95 | raise TypeError("Edge attributes must be a dict") |
| 96 | self.edges.append(Edge(item[1], item[2], item[3])) |
| 97 | |
| 98 | # Edge Case: Handle unknown item types |
| 99 | else: |
| 100 | raise ValueError("Unknown item") |
| 101 | |
| 102 | # Handled Edge Cases: None input, non-list data, non-tuple items, empty tuples, |
| 103 | # incorrect tuple lengths for each type, non-dict node/edge attributes, |
| 104 | # unknown item types |