future

Finished
2323
2424
2525class Graph:
26 """
27 A class representing a graph DSL that can parse graph data structures.
28
29 The Graph class takes a list of tuples that describe attributes, nodes, and edges.
30 Each tuple must follow specific formats:
31 - ATTR: (ATTR, key, value)
32 - NODE: (NODE, name, attrs_dict)
33 - EDGE: (EDGE, src, dst, attrs_dict)
34 """
35
2636 def __init__(self, data: list | None = None):
27 pass
37 """
38 Initialize a Graph with the given data.
39
40 Args:
41 data: A list of tuples describing the graph components.
42 Can be None, which creates an empty graph.
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: Graph 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: Handle attribute items
76 if item_type == ATTR:
77 # Edge Case: Attribute tuple must have exactly 3 elements
78 if len(item) != 3:
79 raise TypeError("Graph item malformed")
80 key, value = item[1], item[2]
81 self.attrs[key] = value
82
83 # Edge Case: Handle node items
84 elif item_type == NODE:
85 # Edge Case: Node tuple must have exactly 3 elements
86 if len(item) != 3:
87 raise TypeError("Graph item malformed")
88 name, attrs = item[1], item[2]
89 # Edge Case: Node attributes must be a dict
90 if not isinstance(attrs, dict):
91 raise TypeError("Graph item malformed")
92 self.nodes.append(Node(name, attrs))
93
94 # Edge Case: Handle edge items
95 elif item_type == EDGE:
96 # Edge Case: Edge tuple must have exactly 4 elements
97 if len(item) != 4:
98 raise TypeError("Graph item malformed")
99 src, dst, attrs = item[1], item[2], item[3]
100 # Edge Case: Edge attributes must be a dict
101 if not isinstance(attrs, dict):
102 raise TypeError("Graph item malformed")
103 self.edges.append(Edge(src, dst, attrs))
104
105 # Edge Case: Handle unknown item types (not one of ATTR, NODE, EDGE)
106 # This addresses edge case 2: unknown item type when item_type is not a recognized constant
107 else:
108 raise ValueError("Unknown item")
109
110 # Handled Edge Cases: None data, non-list data, non-tuple items, empty tuples,
111 # incorrect tuple lengths for each type, non-dict attributes,
112 # unknown item types
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.