chris

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 - 3 elements for ATTR: (ATTR, key, value)
35
36 Raises:
37 TypeError: If data is not a list or if any 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 # Edge Case: Empty data list
53 if not data:
54 return
55
56 for item in data:
57 # Edge Case: Item is not a tuple
58 if not isinstance(item, tuple):
59 raise TypeError("Graph item malformed")
60
61 # Edge Case: Empty tuple
62 if len(item) == 0:
63 raise TypeError("Graph item malformed")
64
65 item_type = item[0]
66
67 # Edge Case: Unknown item type
68 if item_type not in [NODE, EDGE, ATTR]:
69 raise ValueError("Unknown item")
70
71 # Edge Case: NODE item with incorrect 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 incorrect number of elements
76 if item_type == EDGE and len(item) != 4:
77 raise TypeError("Graph item malformed")
78
79 # Edge Case: ATTR item with incorrect number of elements
80 if item_type == ATTR and len(item) != 3:
81 raise TypeError("Graph item malformed")
82
83 if item_type == NODE:
84 _, name, attrs = item
85 # Edge Case: Node name is not a string
86 if not isinstance(name, str):
87 raise TypeError("Graph item malformed")
88 # Edge Case: Node attrs is not a dict
89 if not isinstance(attrs, dict):
90 raise TypeError("Graph item malformed")
91 self.nodes.append(Node(name, attrs))
92 elif item_type == EDGE:
93 _, src, dst, attrs = item
94 # Edge Case: Edge src is not a string
95 if not isinstance(src, str):
96 raise TypeError("Graph item malformed")
97 # Edge Case: Edge dst is not a string
98 if not isinstance(dst, str):
99 raise TypeError("Graph item malformed")
100 # Edge Case: Edge attrs is not a dict
101 if not isinstance(attrs, dict):
102 raise TypeError("Graph item malformed")
103 self.edges.append(Edge(src, dst, attrs))
104 elif item_type == ATTR:
105 _, key, value = item
106 # Edge Case: Attr key is not a string
107 if not isinstance(key, str):
108 raise TypeError("Graph item malformed")
109 self.attrs[key] = value
110 # Handled Edge Cases: Data is None, Data is not a list, Empty data list, Item is not a tuple, Empty tuple, Unknown item type, NODE item with incorrect number of elements, EDGE item with incorrect number of elements, ATTR item with incorrect number of elements, Node name is not a string, Node attrs is not a dict, Edge src is not a string, Edge dst is not a string, Edge attrs is not a dict, Attr key 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.