| 23 | 23 | |
| 24 | 24 | |
| 25 | 25 | class Graph: |
| 26 | """ |
| 27 | A class representing a graph data structure. |
| 28 | |
| 29 | The graph can be initialized with data that describes nodes, edges, and attributes. |
| 30 | |
| 31 | Parameters: |
| 32 | data (list | None): A list of tuples describing the graph elements. Each tuple should have: |
| 33 | - 2 elements for ATTR: (ATTR, key, value) |
| 34 | - 2 elements for NODE: (NODE, name, attrs) |
| 35 | - 3 elements for EDGE: (EDGE, src, dst, attrs) |
| 36 | |
| 37 | Raises: |
| 38 | TypeError: If data is not a list or if any tuple is malformed. |
| 39 | ValueError: If an unknown item type is encountered. |
| 40 | """ |
| 26 | 41 | def __init__(self, data: list | None = None): |
| 27 | | pass |
| 42 | # Edge Case: Handle None input by initializing empty collections |
| 43 | if data is None: |
| 44 | self.attrs = {} |
| 45 | self.nodes = [] |
| 46 | self.edges = [] |
| 47 | return |
| 48 | |
| 49 | # Edge Case: Ensure data is a list |
| 50 | if not isinstance(data, list): |
| 51 | raise TypeError("Graph data malformed") |
| 52 | |
| 53 | # Initialize graph components |
| 54 | self.attrs = {} |
| 55 | self.nodes = [] |
| 56 | self.edges = [] |
| 57 | |
| 58 | # Process each item in the data list |
| 59 | for item in data: |
| 60 | # Edge Case: Ensure each item is a tuple |
| 61 | if not isinstance(item, tuple): |
| 62 | raise TypeError("Graph item malformed") |
| 63 | |
| 64 | # Edge Case: Ensure item has at least one element to check its type |
| 65 | if len(item) < 1: |
| 66 | raise TypeError("Graph item malformed") |
| 67 | |
| 68 | item_type = item[0] |
| 69 | |
| 70 | # Process attributes |
| 71 | if item_type == ATTR: |
| 72 | # Edge Case: Ensure attribute tuple has exactly 3 elements |
| 73 | if len(item) != 3: |
| 74 | raise TypeError("Graph item malformed") |
| 75 | |
| 76 | _, key, value = item |
| 77 | self.attrs[key] = value |
| 78 | |
| 79 | # Process nodes |
| 80 | elif item_type == NODE: |
| 81 | # Edge Case: Ensure node tuple has exactly 3 elements |
| 82 | if len(item) != 3: |
| 83 | raise TypeError("Graph item malformed") |
| 84 | |
| 85 | _, name, attrs = item |
| 86 | # Edge Case: Ensure node attributes is a dictionary |
| 87 | if not isinstance(attrs, dict): |
| 88 | raise TypeError("Node attributes must be a dictionary") |
| 89 | |
| 90 | self.nodes.append(Node(name, attrs)) |
| 91 | |
| 92 | # Process edges |
| 93 | elif item_type == EDGE: |
| 94 | # Edge Case: Ensure edge tuple has exactly 4 elements |
| 95 | if len(item) != 4: |
| 96 | raise TypeError("Graph item malformed") |
| 97 | |
| 98 | _, src, dst, attrs = item |
| 99 | # Edge Case: Ensure edge attributes is a dictionary |
| 100 | if not isinstance(attrs, dict): |
| 101 | raise TypeError("Edge attributes must be a dictionary") |
| 102 | |
| 103 | self.edges.append(Edge(src, dst, attrs)) |
| 104 | |
| 105 | # Edge Case: Handle unknown item types |
| 106 | else: |
| 107 | raise ValueError("Unknown item") |
| 108 | |
| 109 | # Handled Edge Cases: None input, non-list data, non-tuple items, items with insufficient elements, |
| 110 | # incorrect tuple lengths for each type, non-dict attributes, unknown item types |