This commit is contained in:
@@ -43,19 +43,32 @@ def parse_to_list(response):
|
||||
"""Optimistically parse the response to a list."""
|
||||
res = []
|
||||
|
||||
special_values = {"infinity", "nan", "-infinity"}
|
||||
|
||||
if response is None:
|
||||
return res
|
||||
|
||||
for item in response:
|
||||
if item is None:
|
||||
res.append(None)
|
||||
continue
|
||||
try:
|
||||
res.append(int(item))
|
||||
except ValueError:
|
||||
try:
|
||||
res.append(float(item))
|
||||
except ValueError:
|
||||
res.append(nativestr(item))
|
||||
item_str = nativestr(item)
|
||||
except TypeError:
|
||||
res.append(None)
|
||||
continue
|
||||
|
||||
if isinstance(item_str, str) and item_str.lower() in special_values:
|
||||
res.append(item_str) # Keep as string
|
||||
else:
|
||||
try:
|
||||
res.append(int(item))
|
||||
except ValueError:
|
||||
try:
|
||||
res.append(float(item))
|
||||
except ValueError:
|
||||
res.append(item_str)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
@@ -64,6 +77,11 @@ def parse_list_to_dict(response):
|
||||
for i in range(0, len(response), 2):
|
||||
if isinstance(response[i], list):
|
||||
res["Child iterators"].append(parse_list_to_dict(response[i]))
|
||||
try:
|
||||
if isinstance(response[i + 1], list):
|
||||
res["Child iterators"].append(parse_list_to_dict(response[i + 1]))
|
||||
except IndexError:
|
||||
pass
|
||||
elif isinstance(response[i + 1], list):
|
||||
res["Child iterators"] = [parse_list_to_dict(response[i + 1])]
|
||||
else:
|
||||
@@ -74,25 +92,6 @@ def parse_list_to_dict(response):
|
||||
return res
|
||||
|
||||
|
||||
def parse_to_dict(response):
|
||||
if response is None:
|
||||
return {}
|
||||
|
||||
res = {}
|
||||
for det in response:
|
||||
if isinstance(det[1], list):
|
||||
res[det[0]] = parse_list_to_dict(det[1])
|
||||
else:
|
||||
try: # try to set the attribute. may be provided without value
|
||||
try: # try to convert the value to float
|
||||
res[det[0]] = float(det[1])
|
||||
except (TypeError, ValueError):
|
||||
res[det[0]] = det[1]
|
||||
except IndexError:
|
||||
pass
|
||||
return res
|
||||
|
||||
|
||||
def random_string(length=10):
|
||||
"""
|
||||
Returns a random N character long string.
|
||||
@@ -102,26 +101,6 @@ def random_string(length=10):
|
||||
)
|
||||
|
||||
|
||||
def quote_string(v):
|
||||
"""
|
||||
RedisGraph strings must be quoted,
|
||||
quote_string wraps given v with quotes incase
|
||||
v is a string.
|
||||
"""
|
||||
|
||||
if isinstance(v, bytes):
|
||||
v = v.decode()
|
||||
elif not isinstance(v, str):
|
||||
return v
|
||||
if len(v) == 0:
|
||||
return '""'
|
||||
|
||||
v = v.replace("\\", "\\\\")
|
||||
v = v.replace('"', '\\"')
|
||||
|
||||
return f'"{v}"'
|
||||
|
||||
|
||||
def decode_dict_keys(obj):
|
||||
"""Decode the keys of the given dictionary with utf-8."""
|
||||
newobj = copy.copy(obj)
|
||||
@@ -132,33 +111,6 @@ def decode_dict_keys(obj):
|
||||
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)
|
||||
|
||||
|
||||
def get_protocol_version(client):
|
||||
if isinstance(client, redis.Redis) or isinstance(client, redis.asyncio.Redis):
|
||||
return client.connection_pool.connection_kwargs.get("protocol")
|
||||
|
||||
Reference in New Issue
Block a user