goodagent

Finished
2424
2525class Graph:
2626 def __init__(self, data: list | None = None):
27 pass
27 """
28 Initialize a Graph with optional data.
29
30 Parameters:
31 data (list | None): A list of tuples describing the graph. 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: Data is not a list
41 if data is not None and not isinstance(data, list):
42 raise TypeError("Graph data malformed")
43
44 self.nodes = []
45 self.edges = []
46 self.attrs = {}
47
48 # Edge Case: Empty data
49 if data is None:
50 return
51
52 for item in data:
53 # Edge Case: Item is not a tuple
54 if not isinstance(item, tuple):
55 raise TypeError("Graph item malformed")
56
57 # Edge Case: Empty tuple
58 if len(item) == 0:
59 raise TypeError("Graph item malformed")
60
61 item_type = item[0]
62
63 # Edge Case: Unknown item type
64 if item_type not in (NODE, EDGE, ATTR):
65 raise ValueError("Unknown item")
66
67 # Edge Case: ATTR item with wrong number of elements
68 if item_type == ATTR and len(item) != 3:
69 raise TypeError("Graph item malformed")
70
71 # Edge Case: NODE item with wrong number of elements
72 if item_type == NODE and len(item) != 3:
73 raise TypeError("Graph item malformed")
74
75 # Edge Case: EDGE item with wrong number of elements
76 if item_type == EDGE and len(item) != 4:
77 raise TypeError("Graph item malformed")
78
79 if item_type == ATTR:
80 _, key, value = item
81 self.attrs[key] = value
82 elif item_type == NODE:
83 _, name, attrs = item
84 # Edge Case: Node attributes is not a dict
85 if not isinstance(attrs, dict):
86 raise TypeError("Node attributes must be a dictionary")
87 self.nodes.append(Node(name, attrs))
88 elif item_type == EDGE:
89 _, src, dst, attrs = item
90 # Edge Case: Edge attributes is not a dict
91 if not isinstance(attrs, dict):
92 raise TypeError("Edge attributes must be a dictionary")
93 self.edges.append(Edge(src, dst, attrs))
94 # Handled Edge Cases: Data is not a list, empty data, item is not a tuple, empty tuple, unknown item type,
95 # ATTR item with wrong number of elements, NODE item with wrong number of elements,
96 # EDGE item with wrong number of elements, Node attributes is not a dict, Edge attributes is not a dict
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.