bohdan

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 data by initializing empty collections
49 if data is None:
50 self.attrs = {}
51 self.nodes = []
52 self.edges = []
53 return
54
55 # Edge Case: Data must be a list
56 if not isinstance(data, list):
57 raise TypeError("Graph data malformed")
58
59 self.attrs = {}
60 self.nodes = []
61 self.edges = []
62
63 # Process each item in the data list
64 for item in data:
65 # Edge Case: Each item must be a tuple
66 if not isinstance(item, tuple):
67 raise TypeError("Graph item malformed")
68
69 # Edge Case: Empty tuple is malformed
70 if len(item) == 0:
71 raise TypeError("Graph item malformed")
72
73 item_type = item[0]
74
75 # Edge Case: Unknown item type
76 if item_type not in (NODE, EDGE, ATTR):
77 raise ValueError("Unknown item")
78
79 # Edge Case: Process ATTR items with correct tuple length
80 if item_type == ATTR:
81 if len(item) != 3:
82 raise TypeError("Graph item malformed")
83 _, key, value = item
84 self.attrs[key] = value
85
86 # Edge Case: Process NODE items with correct tuple length
87 elif item_type == NODE:
88 if len(item) != 3:
89 raise TypeError("Graph item malformed")
90 _, name, attrs = item
91 # Edge Case: Node attributes must be a dict
92 if not isinstance(attrs, dict):
93 raise TypeError("Node attributes must be a dict")
94 self.nodes.append(Node(name, attrs))
95
96 # Edge Case: Process EDGE items with correct tuple length
97 elif item_type == EDGE:
98 if len(item) != 4:
99 raise TypeError("Graph item malformed")
100 _, src, dst, attrs = item
101 # Edge Case: Edge attributes must be a dict
102 if not isinstance(attrs, dict):
103 raise TypeError("Edge attributes must be a dict")
104 self.edges.append(Edge(src, dst, attrs))
105
106 # Handled Edge Cases: None data, non-list data, non-tuple items, empty tuples,
107 # unknown item types, incorrect tuple lengths for each type,
108 # non-dict node attributes, non-dict edge attributes
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.