Skip to content

Use f-strings everywhere where it makes sense #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions redisgraph/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,31 @@ def __init__(self, src_node, relation, dest_node, edge_id=None,
self.dest_node = dest_node

def toString(self):
res = ""
res = ''
if self.properties:
props = ','.join(key+':'+str(quote_string(val)) for key, val in sorted(self.properties.items()))
res += '{' + props + '}'
props = ','.join(
f'{key}:{quote_string(val)}'
for key, val in sorted(self.properties.items()))
res = f'{{{props}}}'

return res

def __str__(self):
# Source node.
if isinstance(self.src_node, Node):
res = str(self.src_node)
else:
res = '()'
res = str(self.src_node) if isinstance(self.src_node, Node) else '()'

# Edge
res += "-["
if self.relation:
res += ":" + self.relation
if self.properties:
props = ','.join(key+':'+str(quote_string(val)) for key, val in sorted(self.properties.items()))
res += '{' + props + '}'
res += ']->'
edge_relation = f':{self.relation}' if self.relation else ''
res += f'-[{edge_relation} {self.toString()}]->'

# Dest node.
if isinstance(self.dest_node, Node):
res += str(self.dest_node)
else:
res += '()'
res += f"{self.dest_node if isinstance(self.dest_node, Node) else ''}"

return res

def __eq__(self, rhs):
# Quick positive check, if both IDs are set.
if self.id is not None and rhs.id is not None and self.id == rhs.id:
if self.id and self.id == rhs.id:
return True

# Source and destination nodes should match.
Expand Down
36 changes: 12 additions & 24 deletions redisgraph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,11 @@ def commit(self):
"""
Create entire graph.
"""
if len(self.nodes) == 0 and len(self.edges) == 0:
if not (self.nodes or self.edges):
return None

query = 'CREATE '
for _, node in self.nodes.items():
query += str(node) + ','

query += ','.join([str(edge) for edge in self.edges])

# Discard leading comma.
if query[-1] == ',':
query = query[:-1]

query = f'CREATE '
query += ','.join(str(v) for v in [*self.nodes.values(), *self.edges])
return self.query(query)

def flush(self):
Expand All @@ -142,7 +134,7 @@ def build_params_header(self, params):
# Value is None, replace with "null" string.
elif value is None:
value = "null"
params_header += str(key) + "=" + str(value) + " "
params_header += f"{key} = {value} "
return params_header

def query(self, q, params=None, timeout=None, read_only=False):
Expand All @@ -154,7 +146,7 @@ def query(self, q, params=None, timeout=None, read_only=False):
query = q

# handle query parameters
if params is not None:
if params:
query = self.build_params_header(params) + query

# construct query command
Expand All @@ -176,8 +168,8 @@ def query(self, q, params=None, timeout=None, read_only=False):
return QueryResult(self, response)
except redis.exceptions.ResponseError as e:
if "wrong number of arguments" in str(e):
print("Note: RedisGraph Python requires server version 2.2.8 or above")
raise e
print ("Note: RedisGraph Python requires server version 2.2.8 or above")
raise
except VersionMismatchException as e:
# client view over the graph schema is out of sync
# set client version and refresh local schema
Expand Down Expand Up @@ -211,22 +203,18 @@ def merge(self, pattern):
"""
Merge pattern.
"""

query = 'MERGE '
query += str(pattern)

return self.query(query)
return self.query(f'MERGE {pattern}')

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

y = kwagrs.get('y', None)
y = kwagrs.get('y')
if y:
q += ' YIELD %s' % ','.join(y)
query += f' YIELD {",".join(y)}'

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

def labels(self):
return self.call_procedure("db.labels", read_only=True).result_set
Expand Down
25 changes: 9 additions & 16 deletions redisgraph/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,23 @@ def __init__(self, node_id=None, alias=None, label=None, properties=None):
self.properties = properties or {}

def toString(self):
res = ""
res = ''
if self.properties:
props = ','.join(key+':'+str(quote_string(val)) for key, val in sorted(self.properties.items()))
res += '{' + props + '}'
props = ','.join(
f'{key}:{quote_string(val)}'
for key, val in sorted(self.properties.items()))
res = f'{{{props}}}'

return res

def __str__(self):
res = '('
if self.alias:
res += self.alias
if self.label:
res += ':' + self.label
if self.properties:
props = ','.join(key+':'+str(quote_string(val)) for key, val in sorted(self.properties.items()))
res += '{' + props + '}'
res += ')'

return res
label = f':{self.label}' if label else ''
return f'({self.alias or ""}{label} {self.toString()})'

def __eq__(self, rhs):
# Quick positive check, if both IDs are set.
if self.id is not None and rhs.id is not None and self.id != rhs.id:
return False
if self.id and self.id == rhs.id:
return True

# Label should match.
if self.label != rhs.label:
Expand Down
17 changes: 9 additions & 8 deletions redisgraph/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ def __eq__(self, other):
return self.nodes() == other.nodes() and self.edges() == other.edges()

def __str__(self):
res = "<"
res = ""
edge_count = self.edge_count()
for i in range(0, edge_count):
for i in range(edge_count):
node_id = self.get_node(i).id
res += "(" + str(node_id) + ")"
res += f"({node_id})"
edge = self.get_relationship(i)
res += "-[" + str(int(edge.id)) + "]->" if edge.src_node == node_id else "<-[" + str(int(edge.id)) + "]-"
node_id = self.get_node(edge_count).id
res += "(" + str(node_id) + ")"
res += ">"
return res
if edge.src_node == node_id:
res = f"{res}-[{edge.id}]->"
else:
res = f"{res}<-[{edge.id}]-"

return f'<{res}({self.get_node(edge_count).id})>'