let it go

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