unicode

Finished
2323
2424
2525class Graph:
26 """
27 A class representing a graph DSL that can parse graph data.
28
29 The graph data is expected to be a list of tuples where each tuple represents
30 either an attribute, a node, or an edge.
31
32 Parameters:
33 data (list | None): A list of graph items. Each item is a tuple.
34
35 Raises:
36 TypeError: If the graph data is malformed or a graph item is malformed.
37 ValueError: If an unknown item type is encountered.
38 """
2639 def __init__(self, data: list | None = None):
27 pass
40 # Edge Case: Graph data is not a list
41 if data is not None and not isinstance(data, list):
42 raise TypeError("Graph data malformed")
43
44 self.attrs = {}
45 self.nodes = []
46 self.edges = []
47
48 # Edge Case: Empty graph (data is None)
49 if data is None:
50 return
51
52 # Process each item in the graph data
53 for item in data:
54 # Edge Case: Graph item is not a tuple
55 if not isinstance(item, tuple):
56 raise TypeError("Graph item malformed")
57
58 # Edge Case: Empty tuple
59 if len(item) == 0:
60 raise TypeError("Graph item malformed")
61
62 item_type = item[0]
63
64 # Edge Case: Unknown item type
65 if item_type not in [NODE, EDGE, ATTR]:
66 raise ValueError("Unknown item")
67
68 # Process attribute
69 if item_type == ATTR:
70 # Edge Case: Attribute tuple doesn't have exactly 3 elements
71 if len(item) != 3:
72 raise TypeError("Graph item malformed")
73
74 key, value = item[1], item[2]
75 self.attrs[key] = value
76
77 # Process node
78 elif item_type == NODE:
79 # Edge Case: Node tuple doesn't have exactly 3 elements
80 if len(item) != 3:
81 raise TypeError("Graph item malformed")
82
83 name, attrs = item[1], item[2]
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 # Process edge
91 elif item_type == EDGE:
92 # Edge Case: Edge tuple doesn't have exactly 4 elements
93 if len(item) != 4:
94 raise TypeError("Graph item malformed")
95
96 src, dst, attrs = item[1], item[2], item[3]
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 # Handled Edge Cases: Graph data is not a list, Empty graph (data is None),
104 # Graph item is not a tuple, Empty tuple, Unknown item type,
105 # Attribute tuple doesn't have exactly 3 elements,
106 # Node tuple doesn't have exactly 3 elements, Node attributes is not a dict,
107 # Edge tuple doesn't have exactly 4 elements, Edge attributes is not a dict
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.