diff --git a/redisgraph/edge.py b/redisgraph/edge.py index 8d0d2cd..f2720b7 100644 --- a/redisgraph/edge.py +++ b/redisgraph/edge.py @@ -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. diff --git a/redisgraph/graph.py b/redisgraph/graph.py index f4d802f..171ee3c 100644 --- a/redisgraph/graph.py +++ b/redisgraph/graph.py @@ -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): @@ -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): @@ -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 @@ -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 @@ -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 diff --git a/redisgraph/node.py b/redisgraph/node.py index 76db17c..c3ec7c5 100644 --- a/redisgraph/node.py +++ b/redisgraph/node.py @@ -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: diff --git a/redisgraph/path.py b/redisgraph/path.py index 903ec7c..f4096c9 100644 --- a/redisgraph/path.py +++ b/redisgraph/path.py @@ -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})>'