Skip to content

Commit 85732f4

Browse files
committed
fix review comments
1 parent 2e9cab8 commit 85732f4

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

redis/commands/graph/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def call_procedure(self, procedure, *args, read_only=False, **kwagrs):
148148

149149
y = kwagrs.get("y", None)
150150
if y is not None:
151-
q += f" YIELD {','.join(y)}"
151+
q += f"YIELD {','.join(y)}"
152152

153153
return self.query(q, read_only=read_only)
154154

@@ -240,7 +240,7 @@ async def call_procedure(self, procedure, *args, read_only=False, **kwagrs):
240240

241241
y = kwagrs.get("y", None)
242242
if y is not None:
243-
f" YIELD {','.join(y)}"
243+
f"YIELD {','.join(y)}"
244244
return await self.query(q, read_only=read_only)
245245

246246
async def labels(self):

redis/commands/graph/commands.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
from .execution_plan import ExecutionPlan
66
from .query_result import AsyncQueryResult, QueryResult
77

8+
PROFILE_CMD = "GRAPH.PROFILE"
9+
RO_QUERY_CMD = "GRAPH.RO_QUERY"
10+
QUERY_CMD = "GRAPH.QUERY"
11+
DELETE_CMD = "GRAPH.DELETE"
12+
SLOWLOG_CMD = "GRAPH.SLOWLOG"
13+
CONFIG_CMD = "GRAPH.CONFIG"
14+
LIST_CMD = "GRAPH.LIST"
15+
EXPLAIN_CMD = "GRAPH.EXPLAIN"
16+
817

918
class GraphCommands:
1019
"""RedisGraph Commands"""
@@ -58,26 +67,22 @@ def query(self, q, params=None, timeout=None, read_only=False, profile=False):
5867
# ask for compact result-set format
5968
# specify known graph version
6069
if profile:
61-
cmd = "GRAPH.PROFILE"
70+
cmd = PROFILE_CMD
6271
else:
63-
cmd = "GRAPH.RO_QUERY" if read_only else "GRAPH.QUERY"
72+
cmd = RO_QUERY_CMD if read_only else QUERY_CMD
6473
command = [cmd, self.name, query, "--compact"]
6574

6675
# include timeout is specified
67-
if timeout:
68-
if not isinstance(timeout, int):
69-
raise Exception("Timeout argument must be a positive integer")
70-
command += ["timeout", timeout]
76+
if isinstance(timeout, int):
77+
command.extend(["timeout", timeout])
78+
elif timeout is not None:
79+
raise Exception("Timeout argument must be a positive integer")
7180

7281
# issue query
7382
try:
7483
response = self.execute_command(*command)
7584
return QueryResult(self, response, profile)
7685
except ResponseError as e:
77-
if "wrong number of arguments" in str(e):
78-
print(
79-
"Note: RedisGraph Python requires server version 2.2.8 or above"
80-
) # noqa
8186
if "unknown command" in str(e) and read_only:
8287
# `GRAPH.RO_QUERY` is unavailable in older versions.
8388
return self.query(q, params, timeout, read_only=False)
@@ -105,7 +110,7 @@ def delete(self):
105110
For more information see `DELETE <https://redis.io/commands/graph.delete>`_. # noqa
106111
"""
107112
self._clear_schema()
108-
return self.execute_command("GRAPH.DELETE", self.name)
113+
return self.execute_command(DELETE_CMD, self.name)
109114

110115
# declared here, to override the built in redis.db.flush()
111116
def flush(self):
@@ -145,7 +150,7 @@ def slowlog(self):
145150
3. The issued query.
146151
4. The amount of time needed for its execution, in milliseconds.
147152
"""
148-
return self.execute_command("GRAPH.SLOWLOG", self.name)
153+
return self.execute_command(SLOWLOG_CMD, self.name)
149154

150155
def config(self, name, value=None, set=False):
151156
"""
@@ -169,14 +174,14 @@ def config(self, name, value=None, set=False):
169174
raise DataError(
170175
"``value`` can be provided only when ``set`` is True"
171176
) # noqa
172-
return self.execute_command("GRAPH.CONFIG", *params)
177+
return self.execute_command(CONFIG_CMD, *params)
173178

174179
def list_keys(self):
175180
"""
176181
Lists all graph keys in the keyspace.
177182
For more information see `GRAPH.LIST <https://redis.io/commands/graph.list>`_. # noqa
178183
"""
179-
return self.execute_command("GRAPH.LIST")
184+
return self.execute_command(LIST_CMD)
180185

181186
def execution_plan(self, query, params=None):
182187
"""
@@ -189,7 +194,7 @@ def execution_plan(self, query, params=None):
189194
"""
190195
query = self._build_params_header(params) + query
191196

192-
plan = self.execute_command("GRAPH.EXPLAIN", self.name, query)
197+
plan = self.execute_command(EXPLAIN_CMD, self.name, query)
193198
if isinstance(plan[0], bytes):
194199
plan = [b.decode() for b in plan]
195200
return "\n".join(plan)
@@ -206,7 +211,7 @@ def explain(self, query, params=None):
206211
"""
207212
query = self._build_params_header(params) + query
208213

209-
plan = self.execute_command("GRAPH.EXPLAIN", self.name, query)
214+
plan = self.execute_command(EXPLAIN_CMD, self.name, query)
210215
return ExecutionPlan(plan)
211216

212217

@@ -241,26 +246,22 @@ async def query(self, q, params=None, timeout=None, read_only=False, profile=Fal
241246
# ask for compact result-set format
242247
# specify known graph version
243248
if profile:
244-
cmd = "GRAPH.PROFILE"
249+
cmd = PROFILE_CMD
245250
else:
246-
cmd = "GRAPH.RO_QUERY" if read_only else "GRAPH.QUERY"
251+
cmd = RO_QUERY_CMD if read_only else QUERY_CMD
247252
command = [cmd, self.name, query, "--compact"]
248253

249254
# include timeout is specified
250-
if timeout:
251-
if not isinstance(timeout, int):
252-
raise Exception("Timeout argument must be a positive integer")
253-
command += ["timeout", timeout]
255+
if isinstance(timeout, int):
256+
command.extend(["timeout", timeout])
257+
elif timeout is not None:
258+
raise Exception("Timeout argument must be a positive integer")
254259

255260
# issue query
256261
try:
257262
response = await self.execute_command(*command)
258263
return await AsyncQueryResult().initialize(self, response, profile)
259264
except ResponseError as e:
260-
if "wrong number of arguments" in str(e):
261-
print(
262-
"Note: RedisGraph Python requires server version 2.2.8 or above"
263-
) # noqa
264265
if "unknown command" in str(e) and read_only:
265266
# `GRAPH.RO_QUERY` is unavailable in older versions.
266267
return await self.query(q, params, timeout, read_only=False)
@@ -284,7 +285,7 @@ async def execution_plan(self, query, params=None):
284285
"""
285286
query = self._build_params_header(params) + query
286287

287-
plan = await self.execute_command("GRAPH.EXPLAIN", self.name, query)
288+
plan = await self.execute_command(EXPLAIN_CMD, self.name, query)
288289
if isinstance(plan[0], bytes):
289290
plan = [b.decode() for b in plan]
290291
return "\n".join(plan)
@@ -300,7 +301,7 @@ async def explain(self, query, params=None):
300301
"""
301302
query = self._build_params_header(params) + query
302303

303-
plan = await self.execute_command("GRAPH.EXPLAIN", self.name, query)
304+
plan = await self.execute_command(EXPLAIN_CMD, self.name, query)
304305
return ExecutionPlan(plan)
305306

306307
async def flush(self):

redis/commands/graph/query_result.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from collections import OrderedDict
21
import sys
2+
from collections import OrderedDict
3+
from distutils.util import strtobool
34

45
# from prettytable import PrettyTable
56
from redis import ResponseError
6-
from distutils.util import strtobool
77

88
from .edge import Edge
99
from .exceptions import VersionMismatchException
@@ -293,7 +293,7 @@ def parse_unknown(self, cell):
293293
"""
294294
Parse a cell of unknown type.
295295
"""
296-
print("Unknown type\n")
296+
sys.stderr.write("Unknown type\n")
297297
return None
298298

299299
def parse_scalar(self, cell):

0 commit comments

Comments
 (0)