future

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