| 24 | 24 | |
| 25 | 25 | class Graph: |
| 26 | 26 | def __init__(self, data: list | None = None): |
| 27 | | pass |
| 27 | # Edge Case: Graph data is None |
| 28 | if data is None: |
| 29 | data = [] |
| 30 | |
| 31 | # Edge Case: Graph data is not a list |
| 32 | if not isinstance(data, list): |
| 33 | raise TypeError("Graph data malformed") |
| 34 | |
| 35 | self.nodes = [] |
| 36 | self.edges = [] |
| 37 | self.attrs = {} |
| 38 | |
| 39 | # Process each item in the data list |
| 40 | for item in data: |
| 41 | # Edge Case: Graph item is not a tuple |
| 42 | if not isinstance(item, tuple): |
| 43 | raise TypeError("Graph item malformed") |
| 44 | |
| 45 | # Edge Case: Graph item is empty |
| 46 | if len(item) == 0: |
| 47 | raise TypeError("Graph item malformed") |
| 48 | |
| 49 | item_type = item[0] |
| 50 | |
| 51 | # Edge Case: Unknown item type |
| 52 | if item_type not in [NODE, EDGE, ATTR]: |
| 53 | raise ValueError("Unknown item") |
| 54 | |
| 55 | # Process node |
| 56 | if item_type == NODE: |
| 57 | # Edge Case: Node tuple has incorrect number of elements |
| 58 | if len(item) != 3: |
| 59 | raise TypeError("Graph item malformed") |
| 60 | |
| 61 | name, attrs = item[1], item[2] |
| 62 | # Edge Case: Node name is not a string |
| 63 | if not isinstance(name, str): |
| 64 | raise TypeError("Graph item malformed") |
| 65 | # Edge Case: Node attributes is not a dict |
| 66 | if not isinstance(attrs, dict): |
| 67 | raise TypeError("Graph item malformed") |
| 68 | |
| 69 | self.nodes.append(Node(name, attrs)) |
| 70 | |
| 71 | # Process edge |
| 72 | elif item_type == EDGE: |
| 73 | # Edge Case: Edge tuple has incorrect number of elements |
| 74 | if len(item) != 4: |
| 75 | raise TypeError("Graph item malformed") |
| 76 | |
| 77 | src, dst, attrs = item[1], item[2], item[3] |
| 78 | # Edge Case: Edge source or destination is not a string |
| 79 | if not isinstance(src, str) or not isinstance(dst, str): |
| 80 | raise TypeError("Graph item malformed") |
| 81 | # Edge Case: Edge attributes is not a dict |
| 82 | if not isinstance(attrs, dict): |
| 83 | raise TypeError("Graph item malformed") |
| 84 | |
| 85 | self.edges.append(Edge(src, dst, attrs)) |
| 86 | |
| 87 | # Process attribute |
| 88 | elif item_type == ATTR: |
| 89 | # Edge Case: Attribute tuple has incorrect number of elements |
| 90 | if len(item) != 3: |
| 91 | raise TypeError("Graph item malformed") |
| 92 | |
| 93 | key, value = item[1], item[2] |
| 94 | # Edge Case: Attribute key or value is not a string |
| 95 | if not isinstance(key, str) or not isinstance(value, str): |
| 96 | raise TypeError("Graph item malformed") |
| 97 | |
| 98 | self.attrs[key] = value |
| 99 | |
| 100 | # Handled Edge Cases: Graph data is None; Graph data is not a list; Graph item is not a tuple; Graph item is empty; Unknown item type; Node tuple has incorrect number of elements; Node name is not a string; Node attributes is not a dict; Edge tuple has incorrect number of elements; Edge source or destination is not a string; Edge attributes is not a dict; Attribute tuple has incorrect number of elements; Attribute key or value is not a string |