| 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 | Args: |
| 31 | data: A list of tuples representing graph elements. Each tuple should have: |
| 32 | - 2 elements for ATTR: (ATTR, key, value) |
| 33 | - 3 elements for NODE: (NODE, name, attrs) |
| 34 | - 4 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: Handle None input by initializing empty collections |
| 41 | if data is None: |
| 42 | self.attrs = {} |
| 43 | self.nodes = [] |
| 44 | self.edges = [] |
| 45 | return |
| 46 | |
| 47 | # Edge Case: Ensure data is a list |
| 48 | if not isinstance(data, list): |
| 49 | raise TypeError("Graph data malformed") |
| 50 | |
| 51 | # Initialize empty collections |
| 52 | self.attrs = {} |
| 53 | self.nodes = [] |
| 54 | self.edges = [] |
| 55 | |
| 56 | # Process each item in the data |
| 57 | for item in data: |
| 58 | # Edge Case: Ensure each item is a tuple |
| 59 | if not isinstance(item, tuple): |
| 60 | raise TypeError("Graph item malformed") |
| 61 | |
| 62 | # Edge Case: Ensure item has at least one element to check its type |
| 63 | if len(item) < 1: |
| 64 | raise TypeError("Graph item malformed") |
| 65 | |
| 66 | item_type = item[0] |
| 67 | |
| 68 | # Process attributes |
| 69 | if item_type == ATTR: |
| 70 | # Edge Case: Ensure ATTR tuple has exactly 3 elements |
| 71 | if len(item) != 3: |
| 72 | raise TypeError("Graph item malformed") |
| 73 | key, value = item[1], item[2] |
| 74 | self.attrs[key] = value |
| 75 | |
| 76 | # Process nodes |
| 77 | elif item_type == NODE: |
| 78 | # Edge Case: Ensure NODE tuple has exactly 3 elements |
| 79 | if len(item) != 3: |
| 80 | raise TypeError("Graph item malformed") |
| 81 | name, attrs = item[1], item[2] |
| 82 | # Edge Case: Ensure node attributes is a dict |
| 83 | if not isinstance(attrs, dict): |
| 84 | raise TypeError("Node attributes must be a dict") |
| 85 | self.nodes.append(Node(name, attrs)) |
| 86 | |
| 87 | # Process edges |
| 88 | elif item_type == EDGE: |
| 89 | # Edge Case: Ensure EDGE tuple has exactly 4 elements |
| 90 | if len(item) != 4: |
| 91 | raise TypeError("Graph item malformed") |
| 92 | src, dst, attrs = item[1], item[2], item[3] |
| 93 | # Edge Case: Ensure edge attributes is a dict |
| 94 | if not isinstance(attrs, dict): |
| 95 | raise TypeError("Edge attributes must be a dict") |
| 96 | self.edges.append(Edge(src, dst, attrs)) |
| 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, items with insufficient elements, |
| 103 | # incorrect tuple lengths for each type, non-dict attributes, unknown item types |