Debian Codegraph @

valkey/commands/graph/path.py

raw
1 from .edge import Edge
2 from .node import Node
3
4
5 class Path:
6 def __init__(self, nodes, edges):
7 if not (isinstance(nodes, list) and isinstance(edges, list)):
8 raise TypeError("nodes and edges must be list")
9
10 self._nodes = nodes
11 self._edges = edges
12 self.append_type = Node
13
14 @classmethod
15 def new_empty_path(cls):
16 return cls([], [])
17
18 def nodes(self):
19 return self._nodes
20
21 def edges(self):
22 return self._edges
23
24 def get_node(self, index):
25 return self._nodes[index]
26
27 def get_relationship(self, index):
28 return self._edges[index]
29
30 def first_node(self):
31 return self._nodes[0]
32
33 def last_node(self):
34 return self._nodes[-1]
35
36 def edge_count(self):
37 return len(self._edges)
38
39 def nodes_count(self):
40 return len(self._nodes)
41
42 def add_node(self, node):
43 if not isinstance(node, self.append_type):
44 raise AssertionError("Add Edge before adding Node")
45 self._nodes.append(node)
46 self.append_type = Edge
47 return self
48
49 def add_edge(self, edge):
50 if not isinstance(edge, self.append_type):
51 raise AssertionError("Add Node before adding Edge")
52 self._edges.append(edge)
53 self.append_type = Node
54 return self
55
56 def __eq__(self, other):
57 # Type checking
58 if not isinstance(other, Path):
59 return False
60
61 return self.nodes() == other.nodes() and self.edges() == other.edges()
62
63 def __str__(self):
64 res = "<"
65 edge_count = self.edge_count()
66 for i in range(0, edge_count):
67 node_id = self.get_node(i).id
68 res += "(" + str(node_id) + ")"
69 edge = self.get_relationship(i)
70 res += (
71 "-[" + str(int(edge.id)) + "]->"
72 if edge.src_node == node_id
73 else "<-[" + str(int(edge.id)) + "]-"
74 )
75 node_id = self.get_node(edge_count).id
76 res += "(" + str(node_id) + ")"
77 res += ">"
78 return res