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