summaryrefslogtreecommitdiff
path: root/redis/commands/helpers.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/helpers.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/helpers.py')
-rw-r--r--redis/commands/helpers.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/redis/commands/helpers.py b/redis/commands/helpers.py
index 80dfd76..afb4f9f 100644
--- a/redis/commands/helpers.py
+++ b/redis/commands/helpers.py
@@ -1,3 +1,4 @@
+import copy
import random
import string
@@ -114,3 +115,40 @@ def quote_string(v):
v = v.replace('"', '\\"')
return f'"{v}"'
+
+
+def decodeDictKeys(obj):
+ """Decode the keys of the given dictionary with utf-8."""
+ newobj = copy.copy(obj)
+ for k in obj.keys():
+ if isinstance(k, bytes):
+ newobj[k.decode("utf-8")] = newobj[k]
+ newobj.pop(k)
+ return newobj
+
+
+def stringify_param_value(value):
+ """
+ Turn a parameter value into a string suitable for the params header of
+ a Cypher command.
+ You may pass any value that would be accepted by `json.dumps()`.
+
+ Ways in which output differs from that of `str()`:
+ * Strings are quoted.
+ * None --> "null".
+ * In dictionaries, keys are _not_ quoted.
+
+ :param value: The parameter value to be turned into a string.
+ :return: string
+ """
+
+ if isinstance(value, str):
+ return quote_string(value)
+ elif value is None:
+ return "null"
+ elif isinstance(value, (list, tuple)):
+ return f'[{",".join(map(stringify_param_value, value))}]'
+ elif isinstance(value, dict):
+ return f'{{{",".join(f"{k}:{stringify_param_value(v)}" for k, v in value.items())}}}' # noqa
+ else:
+ return str(value)