Skip to content

Commit 0975e68

Browse files
committedDec 26, 2020
Use f-strings everywhere where it makes sense
1 parent 6110412 commit 0975e68

File tree

5 files changed

+45
-71
lines changed

5 files changed

+45
-71
lines changed
 

‎redisgraph/edge.py

+11-20
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,37 @@ def __init__(self, src_node, relation, dest_node, edge_id=None, properties=None)
1919
self.dest_node = dest_node
2020

2121
def toString(self):
22-
res = ""
22+
res = ''
2323
if self.properties:
24-
props = ','.join(key+':'+str(quote_string(val)) for key, val in sorted(self.properties.items()))
25-
res += '{' + props + '}'
24+
props = ','.join(
25+
f'{key}:{quote_string(val)}'
26+
for key, val in sorted(self.properties.items()))
27+
res = f'{{{props}}}'
2628

2729
return res
2830

2931
def __str__(self):
3032
# Source node.
31-
if isinstance(self.src_node, Node):
32-
res = str(self.src_node)
33-
else:
34-
res = '()'
33+
res = str(self.src_node) if isinstance(self.src_node, Node) else '()'
3534

3635
# Edge
37-
res += "-["
38-
if self.relation:
39-
res += ":" + self.relation
40-
if self.properties:
41-
props = ','.join(key+':'+str(quote_string(val)) for key, val in sorted(self.properties.items()))
42-
res += '{' + props + '}'
43-
res += ']->'
36+
edge_relation = f':{self.relation}' if self.relation else ''
37+
res += f'-[{edge_relation} {self.toString()}]->'
4438

4539
# Dest node.
46-
if isinstance(self.dest_node, Node):
47-
res += str(self.dest_node)
48-
else:
49-
res += '()'
40+
res += f"{self.dest_node if isinstance(self.dest_node, Node) else ''}"
5041

5142
return res
5243

5344
def __eq__(self, rhs):
5445
# Quick positive check, if both IDs are set.
55-
if self.id is not None and rhs.id is not None and self.id == rhs.id:
46+
if self.id and self.id == rhs.id:
5647
return True
5748

5849
# Source and destination nodes should match.
5950
if self.src_node != rhs.src_node:
6051
return False
61-
52+
6253
if self.dest_node != rhs.dest_node:
6354
return False
6455

‎redisgraph/graph.py

+14-26
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,11 @@ def commit(self):
104104
"""
105105
Create entire graph.
106106
"""
107-
if len(self.nodes) == 0 and len(self.edges) == 0:
107+
if not (self.nodes or self.edges):
108108
return None
109109

110-
query = 'CREATE '
111-
for _, node in self.nodes.items():
112-
query += str(node) + ','
113-
114-
query += ','.join([str(edge) for edge in self.edges])
115-
116-
# Discard leading comma.
117-
if query[-1] == ',':
118-
query = query[:-1]
119-
110+
query = f'CREATE '
111+
query += ','.join(str(v) for v in [*self.nodes.values(), *self.edges])
120112
return self.query(query)
121113

122114
def flush(self):
@@ -138,7 +130,7 @@ def build_params_header(self, params):
138130
# Value is None, replace with "null" string.
139131
elif value is None:
140132
value = "null"
141-
params_header += str(key) + "=" + str(value) + " "
133+
params_header += f"{key} = {value} "
142134
return params_header
143135

144136
def query(self, q, params=None, timeout=None, read_only=False):
@@ -150,7 +142,7 @@ def query(self, q, params=None, timeout=None, read_only=False):
150142
query = q
151143

152144
# handle query parameters
153-
if params is not None:
145+
if params:
154146
query = self.build_params_header(params) + query
155147

156148
# construct query command
@@ -172,7 +164,7 @@ def query(self, q, params=None, timeout=None, read_only=False):
172164
except redis.exceptions.ResponseError as e:
173165
if "wrong number of arguments" in str(e):
174166
print ("Note: RedisGraph Python requires server version 2.2.8 or above")
175-
raise e
167+
raise
176168
except VersionMismatchException as e:
177169
# client view over the graph schema is out of sync
178170
# set client version and refresh local schema
@@ -189,10 +181,10 @@ def execution_plan(self, query, params=None):
189181
Get the execution plan for given query,
190182
GRAPH.EXPLAIN returns an array of operations.
191183
"""
192-
184+
193185
if params is not None:
194186
query = self.build_params_header(params) + query
195-
187+
196188
plan = self.redis_con.execute_command("GRAPH.EXPLAIN", self.name, query, query)
197189
return self._execution_plan_to_string(plan)
198190

@@ -202,27 +194,23 @@ def delete(self):
202194
"""
203195
self._clear_schema()
204196
return self.redis_con.execute_command("GRAPH.DELETE", self.name)
205-
197+
206198
def merge(self, pattern):
207199
"""
208200
Merge pattern.
209201
"""
210-
211-
query = 'MERGE '
212-
query += str(pattern)
213-
214-
return self.query(query)
202+
return self.query(f'MERGE {pattern}'
215203

216204
# Procedures.
217205
def call_procedure(self, procedure, read_only=False, *args, **kwagrs):
218206
args = [quote_string(arg) for arg in args]
219-
q = 'CALL %s(%s)' % (procedure, ','.join(args))
207+
query = f'CALL {procedure}({",".join(args)})'
220208

221-
y = kwagrs.get('y', None)
209+
y = kwagrs.get('y')
222210
if y:
223-
q += ' YIELD %s' % ','.join(y)
211+
query += f' YIELD {",".join(y)}'
224212

225-
return self.query(q, read_only=read_only)
213+
return self.query(query, read_only=read_only)
226214

227215
def labels(self):
228216
return self.call_procedure("db.labels", read_only=True).result_set

‎redisgraph/node.py

+8-15
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,22 @@ def __init__(self, node_id=None, alias=None, label=None, properties={}):
1414
self.properties = properties
1515

1616
def toString(self):
17-
res = ""
17+
res = ''
1818
if self.properties:
19-
props = ','.join(key+':'+str(quote_string(val)) for key, val in sorted(self.properties.items()))
20-
res += '{' + props + '}'
19+
props = ','.join(
20+
f'{key}:{quote_string(val)}'
21+
for key, val in sorted(self.properties.items()))
22+
res = f'{{{props}}}'
2123

2224
return res
2325

2426
def __str__(self):
25-
res = '('
26-
if self.alias:
27-
res += self.alias
28-
if self.label:
29-
res += ':' + self.label
30-
if self.properties:
31-
props = ','.join(key+':'+str(quote_string(val)) for key, val in sorted(self.properties.items()))
32-
res += '{' + props + '}'
33-
res += ')'
34-
35-
return res
27+
label = f':{self.label}' if label else ''
28+
return f'({self.alias or ""}{label} {self.toString()})'
3629

3730
def __eq__(self, rhs):
3831
# Quick positive check, if both IDs are set.
39-
if self.id is not None and rhs.id is not None and self.id == rhs.id:
32+
if self.id and self.id == rhs.id:
4033
return True
4134

4235
# Label should match.

‎redisgraph/path.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def last_node(self):
3333

3434
def edge_count(self):
3535
return len(self._edges)
36-
36+
3737
def nodes_count(self):
3838
return len(self._nodes)
3939

@@ -53,15 +53,15 @@ def __eq__(self, other):
5353
return self.nodes() == other.nodes() and self.edges() == other.edges()
5454

5555
def __str__(self):
56-
res = "<"
56+
res = ""
5757
edge_count = self.edge_count()
58-
for i in range(0, edge_count):
58+
for i in range(edge_count):
5959
node_id = self.get_node(i).id
60-
res += "(" + str(node_id) + ")"
60+
res += f"({node_id})"
6161
edge = self.get_relationship(i)
62-
res += "-[" + str(int(edge.id)) + "]->" if edge.src_node == node_id else "<-[" + str(int(edge.id)) + "]-"
63-
node_id = self.get_node(edge_count).id
64-
res += "(" + str(node_id) + ")"
65-
res += ">"
66-
return res
67-
62+
if edge.src_node == node_id:
63+
res = f"{res}-[{edge.id}]->"
64+
else:
65+
res = f"{res}<-[{edge.id}]-"
66+
67+
return f'<{res}({self.get_node(edge_count).id})>'

‎redisgraph/util.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
__all__ = ['random_string', 'quote_string']
55

6+
67
def random_string(length=10):
78
"""
89
Returns a random N chracter long string.
910
"""
1011
return ''.join(random.choice(string.ascii_lowercase) for x in range(length))
1112

13+
1214
def quote_string(v):
1315
"""
1416
RedisGraph strings must be quoted,

0 commit comments

Comments
 (0)
Failed to load comments.