sn62

Finished
2323
2424
2525class Graph:
26 """
27 A class representing a graph data structure.
28
29 The graph can be initialized with a list of tuples that describe attributes, nodes, and edges.
30 Each tuple must follow a specific format based on its type (ATTR, NODE, EDGE).
31
32 Parameters:
33 data (list | None): A list of tuples describing the graph elements. Defaults to None.
34
35 Raises:
36 TypeError: If the data is not a list or if a graph item is malformed.
37 ValueError: If an unknown item type is encountered.
38 """
2639 def __init__(self, data: list | None = None):
27 pass
40 # Edge Case: Handle None input by initializing empty collections
41 if data is None:
42 self.attrs = {}
43 self.nodes = []
44 self.edges = []
45 return
46
47 # Edge Case: Graph data is not a list
48 if not isinstance(data, list):
49 raise TypeError("Graph data malformed")
50
51 self.attrs = {}
52 self.nodes = []
53 self.edges = []
54
55 # Process each item in the data list
56 for item in data:
57 # Edge Case: Item is not a tuple or doesn't have at least one element
58 if not isinstance(item, tuple) or len(item) < 1:
59 raise TypeError("Graph item malformed")
60
61 item_type = item[0]
62
63 # Edge Case: Unknown item type
64 if item_type not in (NODE, EDGE, ATTR):
65 raise ValueError("Unknown item")
66
67 # Process attribute item
68 if item_type == ATTR:
69 # Edge Case: Attribute tuple doesn't have exactly 3 elements
70 if len(item) != 3:
71 raise TypeError("Graph item malformed")
72 key, value = item[1], item[2]
73 self.attrs[key] = value
74
75 # Process node item
76 elif item_type == NODE:
77 # Edge Case: Node tuple doesn't have exactly 3 elements
78 if len(item) != 3:
79 raise TypeError("Graph item malformed")
80 name, attrs = item[1], item[2]
81 # Edge Case: Node attributes is not a dict
82 if not isinstance(attrs, dict):
83 raise TypeError("Graph item malformed")
84 self.nodes.append(Node(name, attrs))
85
86 # Process edge item
87 elif item_type == EDGE:
88 # Edge Case: Edge tuple doesn't have exactly 4 elements
89 if len(item) != 4:
90 raise TypeError("Graph item malformed")
91 src, dst, attrs = item[1], item[2], item[3]
92 # Edge Case: Edge attributes is not a dict
93 if not isinstance(attrs, dict):
94 raise TypeError("Graph item malformed")
95 self.edges.append(Edge(src, dst, attrs))
96
97 # Handled Edge Cases: None input, non-list data, non-tuple items, items with insufficient length, unknown item types,
98 # attribute tuples with wrong length, node tuples with wrong length, node attributes not dict,
99 # edge tuples with wrong length, edge attributes not dict
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.