fishlium

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