A.2.1

Finished
2424
2525class Graph:
2626 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 (nodes, edges, attributes)
32
33 Raises:
34 TypeError: If data is not a list or if any graph item is malformed
35 ValueError: If an unknown item type is encountered
36 """
37 # Edge Case: Graph data is not a list
38 if data is not None and not isinstance(data, list):
39 raise TypeError("Graph data malformed")
40
41 self.nodes = []
42 self.edges = []
43 self.attrs = {}
44
45 # Edge Case: Empty graph (data is None)
46 if data is None:
47 return
48
49 # Process each item in the data list
50 for item in data:
51 # Edge Case: Graph item is not a tuple
52 if not isinstance(item, tuple):
53 raise TypeError("Graph item malformed")
54
55 # Edge Case: Graph item is empty
56 if len(item) == 0:
57 raise TypeError("Graph item malformed")
58
59 item_type = item[0]
60
61 # Edge Case: Unknown item type
62 if item_type not in (NODE, EDGE, ATTR):
63 raise ValueError("Unknown item")
64
65 # Process nodes
66 if item_type == NODE:
67 # Edge Case: Node tuple has incorrect number of elements
68 if len(item) != 3:
69 raise TypeError("Graph item malformed")
70
71 name, attrs = item[1], item[2]
72 # Edge Case: Node name is not a string
73 if not isinstance(name, str):
74 raise TypeError("Graph item malformed")
75
76 # Edge Case: Node attributes is not a dict
77 if not isinstance(attrs, dict):
78 raise TypeError("Graph item malformed")
79
80 self.nodes.append(Node(name, attrs))
81
82 # Process edges
83 elif item_type == EDGE:
84 # Edge Case: Edge tuple has incorrect number of elements
85 if len(item) != 4:
86 raise TypeError("Graph item malformed")
87
88 src, dst, attrs = item[1], item[2], item[3]
89 # Edge Case: Edge source or destination is not a string
90 if not isinstance(src, str) or not isinstance(dst, str):
91 raise TypeError("Graph item malformed")
92
93 # Edge Case: Edge attributes is not a dict
94 if not isinstance(attrs, dict):
95 raise TypeError("Graph item malformed")
96
97 self.edges.append(Edge(src, dst, attrs))
98
99 # Process attributes
100 elif item_type == ATTR:
101 # Edge Case: Attribute tuple has incorrect number of elements
102 if len(item) != 3:
103 raise TypeError("Graph item malformed")
104
105 key, value = item[1], item[2]
106 # Edge Case: Attribute key or value is not a string
107 if not isinstance(key, str) or not isinstance(value, str):
108 raise TypeError("Graph item malformed")
109
110 self.attrs[key] = value
111
112 # Handled Edge Cases: Graph data is not a list, empty graph (data is None), graph item is not a tuple,
113 # graph item is empty, unknown item type, node tuple has incorrect number of elements,
114 # node name is not a string, node attributes is not a dict, edge tuple has incorrect number of elements,
115 # edge source or destination is not a string, edge attributes is not a dict,
116 # attribute tuple has incorrect number of elements, attribute key or value is not a string
Test NameStatus
test_empty_graph
Pass
test_graph_with_attributes
Pass
test_graph_with_one_attribute
Pass
test_graph_with_one_edge
Pass
test_graph_with_one_node
Pass
test_graph_with_one_node_with_keywords
Pass
test_malformed_EDGE
Pass
test_malformed_attr
Pass
test_malformed_graph
Pass
test_malformed_graph_item
Pass
test_malformed_node
Pass
test_unknown_item
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.