Skip to content

Commit 2e9cab8

Browse files
committed
fix review comments
1 parent e60ad1c commit 2e9cab8

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

redis/commands/graph/__init__.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
from .node import Node # noqa
55
from .path import Path # noqa
66

7+
DB_LABELS = "DB.LABELS"
8+
DB_RAELATIONSHIPTYPES = "DB.RELATIONSHIPTYPES"
9+
DB_PROPERTYKEYS = "DB.PROPERTYKEYS"
10+
711

812
class Graph(GraphCommands):
913
"""
@@ -102,12 +106,12 @@ def get_property(self, idx):
102106
The index of the property
103107
"""
104108
try:
105-
propertie = self._properties[idx]
109+
p = self._properties[idx]
106110
except IndexError:
107111
# Refresh properties.
108112
self._refresh_attributes()
109-
propertie = self._properties[idx]
110-
return propertie
113+
p = self._properties[idx]
114+
return p
111115

112116
def add_node(self, node):
113117
"""
@@ -127,6 +131,8 @@ def add_edge(self, edge):
127131
self.edges.append(edge)
128132

129133
def _build_params_header(self, params):
134+
if params is None:
135+
return ""
130136
if not isinstance(params, dict):
131137
raise TypeError("'params' must be a dict")
132138
# Header starts with "CYPHER"
@@ -141,19 +147,19 @@ def call_procedure(self, procedure, *args, read_only=False, **kwagrs):
141147
q = f"CALL {procedure}({','.join(args)})"
142148

143149
y = kwagrs.get("y", None)
144-
if y:
150+
if y is not None:
145151
q += f" YIELD {','.join(y)}"
146152

147153
return self.query(q, read_only=read_only)
148154

149155
def labels(self):
150-
return self.call_procedure("db.labels", read_only=True).result_set
156+
return self.call_procedure(DB_LABELS, read_only=True).result_set
151157

152158
def relationship_types(self):
153-
return self.call_procedure("db.relationshipTypes", read_only=True).result_set
159+
return self.call_procedure(DB_RAELATIONSHIPTYPES, read_only=True).result_set
154160

155161
def property_keys(self):
156-
return self.call_procedure("db.propertyKeys", read_only=True).result_set
162+
return self.call_procedure(DB_PROPERTYKEYS, read_only=True).result_set
157163

158164

159165
class AsyncGraph(Graph, AsyncGraphCommands):
@@ -204,12 +210,12 @@ async def get_property(self, idx):
204210
The index of the property
205211
"""
206212
try:
207-
propertie = self._properties[idx]
213+
p = self._properties[idx]
208214
except IndexError:
209215
# Refresh properties.
210216
await self._refresh_attributes()
211-
propertie = self._properties[idx]
212-
return propertie
217+
p = self._properties[idx]
218+
return p
213219

214220
async def get_relation(self, idx):
215221
"""
@@ -233,17 +239,17 @@ async def call_procedure(self, procedure, *args, read_only=False, **kwagrs):
233239
q = f"CALL {procedure}({','.join(args)})"
234240

235241
y = kwagrs.get("y", None)
236-
if y:
237-
q += f" YIELD {','.join(y)}"
242+
if y is not None:
243+
f" YIELD {','.join(y)}"
238244
return await self.query(q, read_only=read_only)
239245

240246
async def labels(self):
241-
return ((await self.call_procedure("DB.LABELS", read_only=True))).result_set
247+
return ((await self.call_procedure(DB_LABELS, read_only=True))).result_set
242248

243249
async def property_keys(self):
244-
return (await self.call_procedure("DB.PROPERTYKEYS", read_only=True)).result_set
250+
return (await self.call_procedure(DB_PROPERTYKEYS, read_only=True)).result_set
245251

246252
async def relationship_types(self):
247253
return (
248-
await self.call_procedure("DB.RELATIONSHIPTYPES", read_only=True)
254+
await self.call_procedure(DB_RAELATIONSHIPTYPES, read_only=True)
249255
).result_set

redis/commands/graph/commands.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ def query(self, q, params=None, timeout=None, read_only=False, profile=False):
5252
query = q
5353

5454
# handle query parameters
55-
if params is not None:
56-
query = self._build_params_header(params) + query
55+
query = self._build_params_header(params) + query
5756

5857
# construct query command
5958
# ask for compact result-set format
@@ -188,8 +187,7 @@ def execution_plan(self, query, params=None):
188187
query: the query that will be executed
189188
params: query parameters
190189
"""
191-
if params is not None:
192-
query = self._build_params_header(params) + query
190+
query = self._build_params_header(params) + query
193191

194192
plan = self.execute_command("GRAPH.EXPLAIN", self.name, query)
195193
if isinstance(plan[0], bytes):
@@ -206,8 +204,7 @@ def explain(self, query, params=None):
206204
query: the query that will be executed
207205
params: query parameters
208206
"""
209-
if params is not None:
210-
query = self._build_params_header(params) + query
207+
query = self._build_params_header(params) + query
211208

212209
plan = self.execute_command("GRAPH.EXPLAIN", self.name, query)
213210
return ExecutionPlan(plan)
@@ -238,8 +235,7 @@ async def query(self, q, params=None, timeout=None, read_only=False, profile=Fal
238235
query = q
239236

240237
# handle query parameters
241-
if params is not None:
242-
query = self._build_params_header(params) + query
238+
query = self._build_params_header(params) + query
243239

244240
# construct query command
245241
# ask for compact result-set format
@@ -286,8 +282,7 @@ async def execution_plan(self, query, params=None):
286282
query: the query that will be executed
287283
params: query parameters
288284
"""
289-
if params is not None:
290-
query = self._build_params_header(params) + query
285+
query = self._build_params_header(params) + query
291286

292287
plan = await self.execute_command("GRAPH.EXPLAIN", self.name, query)
293288
if isinstance(plan[0], bytes):
@@ -303,8 +298,7 @@ async def explain(self, query, params=None):
303298
query: the query that will be executed
304299
params: query parameters
305300
"""
306-
if params is not None:
307-
query = self._build_params_header(params) + query
301+
query = self._build_params_header(params) + query
308302

309303
plan = await self.execute_command("GRAPH.EXPLAIN", self.name, query)
310304
return ExecutionPlan(plan)

redis/commands/graph/query_result.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from collections import OrderedDict
2+
import sys
23

34
# from prettytable import PrettyTable
45
from redis import ResponseError
6+
from distutils.util import strtobool
57

68
from .edge import Edge
79
from .exceptions import VersionMismatchException
@@ -145,13 +147,12 @@ def parse_records(self, raw_result_set):
145147
"""
146148
Parses the result set and returns a list of records.
147149
"""
148-
result_set = raw_result_set[1]
149150
records = [
150151
[
151152
self.parse_record_types[self.header[idx][0]](cell)
152153
for idx, cell in enumerate(row)
153154
]
154-
for row in result_set
155+
for row in raw_result_set[1]
155156
]
156157

157158
return records
@@ -268,12 +269,10 @@ def parse_boolean(self, value):
268269
Parse the cell value as a boolean.
269270
"""
270271
value = value.decode() if isinstance(value, bytes) else value
271-
if value == "true":
272-
scalar = True
273-
elif value == "false":
274-
scalar = False
275-
else:
276-
print("Unknown boolean type\n")
272+
try:
273+
scalar = strtobool(value)
274+
except ValueError:
275+
sys.stderr.write("unknown boolean type\n")
277276
scalar = None
278277
return scalar
279278

@@ -489,8 +488,7 @@ async def parse_records(self, raw_result_set):
489488
Parses the result set and returns a list of records.
490489
"""
491490
records = []
492-
result_set = raw_result_set[1]
493-
for row in result_set:
491+
for row in raw_result_set[1]:
494492
record = [
495493
await self.parse_record_types[self.header[idx][0]](cell)
496494
for idx, cell in enumerate(row)

0 commit comments

Comments
 (0)