| 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 can be initialized with data that describes nodes, edges, and attributes. |
| 30 | Each item in the data list should be a tuple with specific formats: |
| 31 | - (ATTR, key, value) for graph attributes |
| 32 | - (NODE, name, attrs_dict) for nodes |
| 33 | - (EDGE, src, dst, attrs_dict) for edges |
| 34 | |
| 35 | Raises: |
| 36 | TypeError: If data is not a list or if any item is malformed |
| 37 | ValueError: If an unknown item type is encountered |
| 38 | """ |
| 26 | 39 | def __init__(self, data: list | None = None): |
| 27 | | pass |
| 40 | """ |
| 41 | Initialize a Graph with optional data. |
| 42 | |
| 43 | Args: |
| 44 | data: A list of tuples describing the graph elements |
| 45 | |
| 46 | Raises: |
| 47 | TypeError: If data is not a list or if any item is malformed |
| 48 | ValueError: If an unknown item type is encountered |
| 49 | """ |
| 50 | # Edge Case: Handle None data |
| 51 | if data is None: |
| 52 | data = [] |
| 53 | |
| 54 | # Edge Case: Graph data is not a list |
| 55 | if not isinstance(data, list): |
| 56 | raise TypeError("Graph data malformed") |
| 57 | |
| 58 | self.nodes = [] |
| 59 | self.edges = [] |
| 60 | self.attrs = {} |
| 61 | |
| 62 | # Edge Case: Process each item in the data list |
| 63 | for item in data: |
| 64 | # Edge Case: Graph item is not a tuple |
| 65 | if not isinstance(item, tuple): |
| 66 | raise TypeError("Graph item malformed") |
| 67 | |
| 68 | # Edge Case: Graph item is empty |
| 69 | if len(item) == 0: |
| 70 | raise TypeError("Graph item malformed") |
| 71 | |
| 72 | item_type = item[0] |
| 73 | |
| 74 | # Edge Case: Handle ATTR type items |
| 75 | if item_type == ATTR: |
| 76 | # Edge Case: ATTR item doesn't have exactly 3 elements |
| 77 | if len(item) != 3: |
| 78 | raise TypeError("Graph item malformed") |
| 79 | |
| 80 | _, key, value = item |
| 81 | self.attrs[key] = value |
| 82 | |
| 83 | # Edge Case: Handle NODE type items |
| 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 | _, name, attrs = item |
| 90 | # Edge Case: Node attributes is not a dict |
| 91 | if not isinstance(attrs, dict): |
| 92 | raise TypeError("Graph item malformed") |
| 93 | |
| 94 | self.nodes.append(Node(name, attrs)) |
| 95 | |
| 96 | # Edge Case: Handle EDGE type items |
| 97 | elif item_type == EDGE: |
| 98 | # Edge Case: EDGE item doesn't have exactly 4 elements |
| 99 | if len(item) != 4: |
| 100 | raise TypeError("Graph item malformed") |
| 101 | |
| 102 | _, src, dst, attrs = item |
| 103 | # Edge Case: Edge attributes is not a dict |
| 104 | if not isinstance(attrs, dict): |
| 105 | raise TypeError("Graph item malformed") |
| 106 | |
| 107 | self.edges.append(Edge(src, dst, attrs)) |
| 108 | |
| 109 | # Edge Case: Handle unknown item types |
| 110 | else: |
| 111 | raise ValueError("Unknown item") |
| 112 | |
| 113 | # Handled Edge Cases: None data, non-list data, non-tuple items, empty items, |
| 114 | # incorrect tuple lengths for each type, non-dict attributes, |
| 115 | # unknown item types |