weekends

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 | None): A list of tuples describing the graph. Each tuple has a type (NODE, EDGE, or ATTR)
32 and additional elements depending on the type.
33
34 Raises:
35 TypeError: If data is not a list or if a graph item is malformed.
36 ValueError: If an unknown item type is encountered.
37 """
38 # Edge Case: Graph data is None
39 if data is None:
40 data = []
41
42 # Edge Case: Graph data is not a list
43 if not isinstance(data, list):
44 raise TypeError("Graph data malformed")
45
46 self.nodes = []
47 self.edges = []
48 self.attrs = {}
49
50 # Process each item in the data list
51 for item in data:
52 # Edge Case: Graph item is not a tuple
53 if not isinstance(item, tuple):
54 raise TypeError("Graph item malformed")
55
56 # Edge Case: Graph item is empty
57 if len(item) == 0:
58 raise TypeError("Graph item malformed")
59
60 item_type = item[0]
61
62 # Edge Case: Unknown item type
63 if item_type not in [NODE, EDGE, ATTR]:
64 raise ValueError("Unknown item")
65
66 # Process NODE items
67 if item_type == NODE:
68 # Edge Case: Node item doesn't have exactly 3 elements
69 if len(item) != 3:
70 raise TypeError("Graph item malformed")
71
72 name, attrs = item[1], item[2]
73 # Edge Case: Node name is not a string
74 if not isinstance(name, str):
75 raise TypeError("Graph item malformed")
76 # Edge Case: Node attributes is not a dict
77 if not isinstance(attrs, dict):
78 raise TypeError("Graph item malformed")
79
80 self.nodes.append(Node(name, attrs))
81
82 # Process EDGE items
83 elif item_type == EDGE:
84 # Edge Case: Edge item doesn't have exactly 4 elements
85 if len(item) != 4:
86 raise TypeError("Graph item malformed")
87
88 src, dst, attrs = item[1], item[2], item[3]
89 # Edge Case: Edge source or destination is not a string
90 if not isinstance(src, str) or not isinstance(dst, str):
91 raise TypeError("Graph item malformed")
92 # Edge Case: Edge attributes is not a dict
93 if not isinstance(attrs, dict):
94 raise TypeError("Graph item malformed")
95
96 self.edges.append(Edge(src, dst, attrs))
97
98 # Process ATTR items
99 elif item_type == ATTR:
100 # Edge Case: Attribute item doesn't have exactly 3 elements
101 if len(item) != 3:
102 raise TypeError("Graph item malformed")
103
104 key, value = item[1], item[2]
105 # Edge Case: Attribute key or value is not a string
106 if not isinstance(key, str) or not isinstance(value, str):
107 raise TypeError("Graph item malformed")
108
109 self.attrs[key] = value
110 # Handled Edge Cases: Graph data is None; Graph data is not a list; Graph item is not a tuple;
111 # Graph item is empty; Unknown item type; Node item doesn't have exactly 3 elements;
112 # Node name is not a string; Node attributes is not a dict; Edge item doesn't have exactly 4 elements;
113 # Edge source or destination is not a string; Edge attributes is not a dict;
114 # Attribute item doesn't have exactly 3 elements; Attribute key or value 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.