seahawk

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