summaryrefslogtreecommitdiff
path: root/redis/commands/graph/path.py
diff options
context:
space:
mode:
authorChayim <chayim@users.noreply.github.com>2021-12-02 18:34:36 +0200
committerGitHub <noreply@github.com>2021-12-02 18:34:36 +0200
commitf5d5610f0b77468bc84c3c9764a5d86ee7883410 (patch)
tree1c985280231667e1ad9cede848915b50a02940d7 /redis/commands/graph/path.py
parentc0b38584dc48f821606150d7965dca88c402192b (diff)
parentd4a9825a72e1b7715d79ce8134e678d9ef537dce (diff)
downloadredis-py-f5d5610f0b77468bc84c3c9764a5d86ee7883410.tar.gz
Merge branch 'master' into ROLE
Diffstat (limited to 'redis/commands/graph/path.py')
-rw-r--r--redis/commands/graph/path.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/redis/commands/graph/path.py b/redis/commands/graph/path.py
new file mode 100644
index 0000000..6f2214a
--- /dev/null
+++ b/redis/commands/graph/path.py
@@ -0,0 +1,74 @@
+from .edge import Edge
+from .node import Node
+
+
+class Path:
+ def __init__(self, nodes, edges):
+ if not (isinstance(nodes, list) and isinstance(edges, list)):
+ raise TypeError("nodes and edges must be list")
+
+ self._nodes = nodes
+ self._edges = edges
+ self.append_type = Node
+
+ @classmethod
+ def new_empty_path(cls):
+ return cls([], [])
+
+ def nodes(self):
+ return self._nodes
+
+ def edges(self):
+ return self._edges
+
+ def get_node(self, index):
+ return self._nodes[index]
+
+ def get_relationship(self, index):
+ return self._edges[index]
+
+ def first_node(self):
+ return self._nodes[0]
+
+ def last_node(self):
+ return self._nodes[-1]
+
+ def edge_count(self):
+ return len(self._edges)
+
+ def nodes_count(self):
+ return len(self._nodes)
+
+ def add_node(self, node):
+ if not isinstance(node, self.append_type):
+ raise AssertionError("Add Edge before adding Node")
+ self._nodes.append(node)
+ self.append_type = Edge
+ return self
+
+ def add_edge(self, edge):
+ if not isinstance(edge, self.append_type):
+ raise AssertionError("Add Node before adding Edge")
+ self._edges.append(edge)
+ self.append_type = Node
+ return self
+
+ def __eq__(self, other):
+ return self.nodes() == other.nodes() and self.edges() == other.edges()
+
+ def __str__(self):
+ res = "<"
+ edge_count = self.edge_count()
+ for i in range(0, edge_count):
+ node_id = self.get_node(i).id
+ res += "(" + str(node_id) + ")"
+ edge = self.get_relationship(i)
+ res += (
+ "-[" + str(int(edge.id)) + "]->"
+ if edge.src_node == node_id
+ else "<-[" + str(int(edge.id)) + "]-"
+ )
+ node_id = self.get_node(edge_count).id
+ res += "(" + str(node_id) + ")"
+ res += ">"
+ return res