From 175a05f4de17918b74bde7f554182968b1f6aabb Mon Sep 17 00:00:00 2001 From: Avital Fine <79420960+AvitalFineRedis@users.noreply.github.com> Date: Tue, 30 Nov 2021 17:47:25 +0100 Subject: Adding RedisGraph support (#1673) Co-authored-by: Chayim I. Kirshen --- redis/commands/helpers.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'redis/commands/helpers.py') 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) -- cgit v1.2.1