sourdough

Finished
2323
2424
2525class Graph:
26 """
27 A class representing a graph data structure.
28
29 The graph can be initialized with data that describes nodes, edges, and attributes.
30 """
31
2632 def __init__(self, data: list | None = None):
27 pass
33 """
34 Initialize a Graph object.
35
36 Args:
37 data: A list of tuples describing the graph. Each tuple represents either
38 an attribute, a node, or an edge.
39
40 Raises:
41 TypeError: If data is not a list or if any graph item is malformed.
42 ValueError: If an unknown item type is encountered.
43 """
44 # Edge Case: Handle None input
45 if data is None:
46 data = []
47
48 # Edge Case: Graph data is not a list
49 if not isinstance(data, list):
50 raise TypeError("Graph data malformed")
51
52 self.nodes = []
53 self.edges = []
54 self.attrs = {}
55
56 # Process each item in the data
57 for item in data:
58 # Edge Case: Graph item is not a tuple
59 if not isinstance(item, tuple):
60 raise TypeError("Graph item malformed")
61
62 # Edge Case: Graph item is empty
63 if len(item) == 0:
64 raise TypeError("Graph item malformed")
65
66 item_type = item[0]
67
68 # Edge Case: Process attribute item
69 if item_type == ATTR:
70 # Edge Case: Attribute item has wrong number of elements
71 if len(item) != 3:
72 raise TypeError("Graph item malformed")
73
74 _, key, value = item
75 self.attrs[key] = value
76
77 # Edge Case: Process node item
78 elif item_type == NODE:
79 # Edge Case: Node item has wrong number of elements
80 if len(item) != 3:
81 raise TypeError("Graph item malformed")
82
83 _, name, attrs = item
84 # Edge Case: Node attributes is not a dict
85 if not isinstance(attrs, dict):
86 raise TypeError("Graph item malformed")
87
88 self.nodes.append(Node(name, attrs))
89
90 # Edge Case: Process edge item
91 elif item_type == EDGE:
92 # Edge Case: Edge item has wrong number of elements
93 if len(item) != 4:
94 raise TypeError("Graph item malformed")
95
96 _, src, dst, attrs = item
97 # Edge Case: Edge attributes is not a dict
98 if not isinstance(attrs, dict):
99 raise TypeError("Graph item malformed")
100
101 self.edges.append(Edge(src, dst, attrs))
102
103 # Edge Case: Unknown item type
104 else:
105 raise ValueError("Unknown item")
106
107 # Handled Edge Cases: None input, data not a list, item not a tuple, empty item,
108 # attribute item with wrong elements, node item with wrong elements,
109 # node attributes not a dict, edge item with wrong elements,
110 # edge attributes not a dict, 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.