summaryrefslogtreecommitdiff
path: root/redis/commands/graph/node.py
diff options
context:
space:
mode:
authorAvital Fine <79420960+AvitalFineRedis@users.noreply.github.com>2021-11-30 17:47:25 +0100
committerGitHub <noreply@github.com>2021-11-30 18:47:25 +0200
commit175a05f4de17918b74bde7f554182968b1f6aabb (patch)
tree6fe2b660c03c4e0342d1b0c6e124490baf14f7a0 /redis/commands/graph/node.py
parentb94e230b17d08e6c89d134e933c706256b79bc4a (diff)
downloadredis-py-175a05f4de17918b74bde7f554182968b1f6aabb.tar.gz
Adding RedisGraph support (#1673)
Co-authored-by: Chayim I. Kirshen <c@kirshen.com>
Diffstat (limited to 'redis/commands/graph/node.py')
-rw-r--r--redis/commands/graph/node.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/redis/commands/graph/node.py b/redis/commands/graph/node.py
new file mode 100644
index 0000000..47e4eeb
--- /dev/null
+++ b/redis/commands/graph/node.py
@@ -0,0 +1,84 @@
+from ..helpers import quote_string
+
+
+class Node:
+ """
+ A node within the graph.
+ """
+
+ def __init__(self, node_id=None, alias=None, label=None, properties=None):
+ """
+ Create a new node.
+ """
+ self.id = node_id
+ self.alias = alias
+ if isinstance(label, list):
+ label = [inner_label for inner_label in label if inner_label != ""]
+
+ if (
+ label is None
+ or label == ""
+ or (isinstance(label, list) and len(label) == 0)
+ ):
+ self.label = None
+ self.labels = None
+ elif isinstance(label, str):
+ self.label = label
+ self.labels = [label]
+ elif isinstance(label, list) and all(
+ [isinstance(inner_label, str) for inner_label in label]
+ ):
+ self.label = label[0]
+ self.labels = label
+ else:
+ raise AssertionError(
+ "label should be either None, " "string or a list of strings"
+ )
+
+ self.properties = properties or {}
+
+ def toString(self):
+ res = ""
+ if self.properties:
+ props = ",".join(
+ key + ":" + str(quote_string(val))
+ for key, val in sorted(self.properties.items())
+ )
+ res += "{" + props + "}"
+
+ return res
+
+ def __str__(self):
+ res = "("
+ if self.alias:
+ res += self.alias
+ if self.labels:
+ res += ":" + ":".join(self.labels)
+ if self.properties:
+ props = ",".join(
+ key + ":" + str(quote_string(val))
+ for key, val in sorted(self.properties.items())
+ )
+ res += "{" + props + "}"
+ res += ")"
+
+ return res
+
+ def __eq__(self, rhs):
+ # Quick positive check, if both IDs are set.
+ if self.id is not None and rhs.id is not None and self.id != rhs.id:
+ return False
+
+ # Label should match.
+ if self.label != rhs.label:
+ return False
+
+ # Quick check for number of properties.
+ if len(self.properties) != len(rhs.properties):
+ return False
+
+ # Compare properties.
+ if self.properties != rhs.properties:
+ return False
+
+ return True