JiaYou

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 NODE: (NODE, name, attrs)
33 - 3 elements for EDGE: (EDGE, src, dst, attrs)
34 - 2 elements for ATTR: (ATTR, name, value)
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.nodes = []
43 self.edges = []
44 self.attrs = {}
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 self.nodes = []
52 self.edges = []
53 self.attrs = {}
54
55 # Process each item in the data list
56 for item in data:
57 # Edge Case: Ensure each item is a tuple
58 if not isinstance(item, tuple):
59 raise TypeError("Graph item malformed")
60
61 # Edge Case: Ensure item has at least one element to check its type
62 if len(item) < 1:
63 raise TypeError("Graph item malformed")
64
65 item_type = item[0]
66
67 # Handle NODE items
68 if item_type == NODE:
69 # Edge Case: NODE must have exactly 3 elements (type, name, attrs)
70 if len(item) != 3:
71 raise TypeError("Graph item malformed")
72
73 name, attrs = item[1], item[2]
74 # Edge Case: NODE attributes must be a dict
75 if not isinstance(attrs, dict):
76 raise TypeError("Node attributes must be a dictionary")
77
78 self.nodes.append(Node(name, attrs))
79
80 # Handle EDGE items
81 elif item_type == EDGE:
82 # Edge Case: EDGE must have exactly 4 elements (type, src, dst, attrs)
83 if len(item) != 4:
84 raise TypeError("Graph item malformed")
85
86 src, dst, attrs = item[1], item[2], item[3]
87 # Edge Case: EDGE attributes must be a dict
88 if not isinstance(attrs, dict):
89 raise TypeError("Edge attributes must be a dictionary")
90
91 self.edges.append(Edge(src, dst, attrs))
92
93 # Handle ATTR items
94 elif item_type == ATTR:
95 # Edge Case: ATTR must have exactly 3 elements (type, name, value)
96 if len(item) != 3:
97 raise TypeError("Graph item malformed")
98
99 name, value = item[1], item[2]
100 self.attrs[name] = value
101
102 # Edge Case: Handle unknown item types
103 else:
104 raise ValueError("Unknown item")
105
106 # Handled Edge Cases: None input, non-list data, non-tuple items, items with insufficient elements,
107 # incorrect tuple lengths for NODE/EDGE/ATTR, non-dict attributes, unknown item types
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.