Skip to content

Commit 6fbb763

Browse files
committedDec 27, 2020
Use f-strings everywhere where it makes sense
1 parent 3a1355c commit 6fbb763

File tree

5 files changed

+40
-65
lines changed

5 files changed

+40
-65
lines changed
 

‎redisgraph/edge.py

+10-19
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,31 @@ def __init__(self, src_node, relation, dest_node, edge_id=None, properties=None)
2222
self.dest_node = dest_node
2323

2424
def toString(self):
25-
res = ""
25+
res = ''
2626
if self.properties:
27-
props = ','.join(key+':'+str(quote_string(val)) for key, val in sorted(self.properties.items()))
28-
res += '{' + props + '}'
27+
props = ','.join(
28+
f'{key}:{quote_string(val)}'
29+
for key, val in sorted(self.properties.items()))
30+
res = f'{{{props}}}'
2931

3032
return res
3133

3234
def __str__(self):
3335
# Source node.
34-
if isinstance(self.src_node, Node):
35-
res = str(self.src_node)
36-
else:
37-
res = '()'
36+
res = str(self.src_node) if isinstance(self.src_node, Node) else '()'
3837

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

4842
# Dest node.
49-
if isinstance(self.dest_node, Node):
50-
res += str(self.dest_node)
51-
else:
52-
res += '()'
43+
res += f"{self.dest_node if isinstance(self.dest_node, Node) else ''}"
5344

5445
return res
5546

5647
def __eq__(self, rhs):
5748
# Quick positive check, if both IDs are set.
58-
if self.id is not None and rhs.id is not None and self.id == rhs.id:
49+
if self.id and self.id == rhs.id:
5950
return True
6051

6152
# Source and destination nodes should match.

‎redisgraph/graph.py

+11-23
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,11 @@ def commit(self):
105105
"""
106106
Create entire graph.
107107
"""
108-
if len(self.nodes) == 0 and len(self.edges) == 0:
108+
if not (self.nodes or self.edges):
109109
return None
110110

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

123115
def flush(self):
@@ -140,7 +132,7 @@ def build_params_header(self, params):
140132
# Value is None, replace with "null" string.
141133
elif value is None:
142134
value = "null"
143-
params_header += str(key) + "=" + str(value) + " "
135+
params_header += f"{key} = {value} "
144136
return params_header
145137

146138
def query(self, q, params=None, timeout=None, read_only=False):
@@ -152,7 +144,7 @@ def query(self, q, params=None, timeout=None, read_only=False):
152144
query = q
153145

154146
# handle query parameters
155-
if params is not None:
147+
if params:
156148
query = self.build_params_header(params) + query
157149

158150
# construct query command
@@ -174,7 +166,7 @@ def query(self, q, params=None, timeout=None, read_only=False):
174166
except redis.exceptions.ResponseError as e:
175167
if "wrong number of arguments" in str(e):
176168
print ("Note: RedisGraph Python requires server version 2.2.8 or above")
177-
raise e
169+
raise
178170
except VersionMismatchException as e:
179171
# client view over the graph schema is out of sync
180172
# set client version and refresh local schema
@@ -208,22 +200,18 @@ def merge(self, pattern):
208200
"""
209201
Merge pattern.
210202
"""
211-
212-
query = 'MERGE '
213-
query += str(pattern)
214-
215-
return self.query(query)
203+
return self.query(f'MERGE {pattern}')
216204

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

222-
y = kwagrs.get('y', None)
210+
y = kwagrs.get('y')
223211
if y:
224-
q += ' YIELD %s' % ','.join(y)
212+
query += f' YIELD {",".join(y)}'
225213

226-
return self.query(q, read_only=read_only)
214+
return self.query(query, read_only=read_only)
227215

228216
def labels(self):
229217
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

+9-8
Original file line numberDiff line numberDiff line change
@@ -53,14 +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
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 character 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.