couple

Finished
2424
2525class Graph:
2626 def __init__(self, data: list | None = None):
27 pass
27 """
28 Initialize a Graph with a list of data tuples.
29
30 Parameters:
31 data (list): A list of tuples representing graph elements. Each tuple should have:
32 - First element: type (NODE, EDGE, or ATTR)
33 - Second element: data specific to the type
34 - Optional third element: attributes dictionary
35
36 Raises:
37 TypeError: If data is not a list or if a graph item is malformed
38 ValueError: If an unknown item type is encountered
39 """
40 # Edge Case: Data is None
41 if data is None:
42 data = []
43
44 # Edge Case: Data is not 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 # Process each item in the data list
53 for item in data:
54 # Edge Case: Item is not a tuple
55 if not isinstance(item, tuple):
56 raise TypeError("Graph item malformed")
57
58 # Edge Case: Item tuple is empty
59 if len(item) == 0:
60 raise TypeError("Graph item malformed")
61
62 item_type = item[0]
63
64 # Edge Case: Node item with incorrect number of elements
65 if item_type == NODE:
66 if len(item) != 3:
67 raise TypeError("Graph item malformed")
68 name, attrs = item[1], item[2]
69 # Edge Case: Node attributes is not a dict
70 if not isinstance(attrs, dict):
71 raise TypeError("Node attributes must be a dict")
72 self.nodes.append(Node(name, attrs))
73
74 # Edge Case: Edge item with incorrect number of elements
75 elif item_type == EDGE:
76 if len(item) != 4:
77 raise TypeError("Graph item malformed")
78 src, dst, attrs = item[1], item[2], item[3]
79 # Edge Case: Edge attributes is not a dict
80 if not isinstance(attrs, dict):
81 raise TypeError("Edge attributes must be a dict")
82 self.edges.append(Edge(src, dst, attrs))
83
84 # Edge Case: Attribute item with incorrect number of elements
85 elif item_type == ATTR:
86 if len(item) != 3:
87 raise TypeError("Graph item malformed")
88 key, value = item[1], item[2]
89 self.attrs[key] = value
90
91 # Edge Case: Unknown item type
92 else:
93 raise ValueError("Unknown item")
94
95 # Handled Edge Cases: Data is None, Data is not a list, Item is not a tuple,
96 # Item tuple is empty, Node item with incorrect number of elements,
97 # Node attributes is not a dict, Edge item with incorrect number of elements,
98 # Edge attributes is not a dict, Attribute item with incorrect number of elements,
99 # Unknown item type
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.