| 24 | 24 | |
| 25 | 25 | class Graph: |
| 26 | 26 | def __init__(self, data: list | None = None): |
| 27 | | pass |
| 27 | """ |
| 28 | Initialize a Graph with optional data. |
| 29 | |
| 30 | Parameters: |
| 31 | data (list | None): A list of tuples describing the graph. Each tuple should have: |
| 32 | - 2 elements for ATTR: (ATTR, key, value) |
| 33 | - 2 elements for NODE: (NODE, name, attrs) |
| 34 | - 3 elements for EDGE: (EDGE, src, dst, attrs) |
| 35 | |
| 36 | Raises: |
| 37 | TypeError: If data is not a list or if any tuple is malformed. |
| 38 | ValueError: If an unknown item type is encountered. |
| 39 | """ |
| 40 | # Edge Case: Data is not a list |
| 41 | if data is not None and not isinstance(data, list): |
| 42 | raise TypeError("Graph data malformed") |
| 43 | |
| 44 | self.nodes = [] |
| 45 | self.edges = [] |
| 46 | self.attrs = {} |
| 47 | |
| 48 | # Edge Case: Empty data |
| 49 | if data is None: |
| 50 | return |
| 51 | |
| 52 | # Edge Case: Data is an empty list |
| 53 | if len(data) == 0: |
| 54 | return |
| 55 | |
| 56 | for item in data: |
| 57 | # Edge Case: Item is not a tuple |
| 58 | if not isinstance(item, tuple): |
| 59 | raise TypeError("Graph item malformed") |
| 60 | |
| 61 | # Edge Case: Empty tuple |
| 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 | # Edge Case: ATTR item with wrong number of elements |
| 72 | if item_type == ATTR and len(item) != 3: |
| 73 | raise TypeError("Graph item malformed") |
| 74 | |
| 75 | # Edge Case: NODE item with wrong number of elements |
| 76 | if item_type == NODE and len(item) != 3: |
| 77 | raise TypeError("Graph item malformed") |
| 78 | |
| 79 | # Edge Case: EDGE item with wrong number of elements |
| 80 | if item_type == EDGE and len(item) != 4: |
| 81 | raise TypeError("Graph item malformed") |
| 82 | |
| 83 | if item_type == ATTR: |
| 84 | _, key, value = item |
| 85 | self.attrs[key] = value |
| 86 | elif item_type == NODE: |
| 87 | _, name, attrs = item |
| 88 | # Edge Case: Node attrs is not a dict |
| 89 | if not isinstance(attrs, dict): |
| 90 | raise TypeError("Node attributes must be a dictionary") |
| 91 | self.nodes.append(Node(name, attrs)) |
| 92 | elif item_type == EDGE: |
| 93 | _, src, dst, attrs = item |
| 94 | # Edge Case: Edge attrs is not a dict |
| 95 | if not isinstance(attrs, dict): |
| 96 | raise TypeError("Edge attributes must be a dictionary") |
| 97 | self.edges.append(Edge(src, dst, attrs)) |
| 98 | |
| 99 | # Handled Edge Cases: Data is not a list, empty data, data is an empty list, item is not a tuple, |
| 100 | # empty tuple, unknown item type, ATTR item with wrong number of elements, NODE item with wrong |
| 101 | # number of elements, EDGE item with wrong number of elements, Node attrs is not a dict, |
| 102 | # Edge attrs is not a dict |