Skip to content

Commit 69fc391

Browse files
authored
Merge pull request #1 from aweyl/change_query_result
change returned value from the query
2 parents 728b2fa + 9f19c7f commit 69fc391

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

redisgraph/client.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,31 @@ class Graph(object):
6060
"""
6161
Graph, collection of nodes and edges.
6262
"""
63+
64+
class QueryResult(object):
65+
def __init__(self, result_set, statistics):
66+
self.result_set = result_set
67+
self.statistics = statistics
68+
69+
"""Prints the data from the query response:
70+
1. First row result_set contains the columns names. Thus the first row in PrettyTable
71+
will contain the columns.
72+
2. The row after that will contain the data returned, or 'No Data returned' if there is none.
73+
3. Prints the statistics of the query.
74+
"""
75+
def pretty_print(self):
76+
tbl = PrettyTable(self.result_set[0])
77+
for row in self.result_set[1:]:
78+
tbl.add_row(row)
79+
80+
if len(self.result_set) == 1:
81+
tbl.add_row(['No data returned.'])
82+
83+
print(str(tbl) + '\n')
84+
85+
for stat in self.statistics:
86+
print(stat)
87+
6388
def __init__(self, name, redis_con):
6489
"""
6590
Create a new graph.
@@ -109,24 +134,10 @@ def query(self, q):
109134
Executes a query against the graph.
110135
"""
111136
response = self.redis_con.execute_command("GRAPH.QUERY", self.name, q)
112-
resultset = response[0]
137+
data = response[0]
113138
statistics = response[1]
114-
columns = resultset[0].split(",")
115-
resultset = resultset[1:]
116-
tbl = PrettyTable(columns)
117-
118-
for idx, result in enumerate(resultset):
119-
tbl.add_row(result.split(","))
120-
121-
if len(resultset) == 0:
122-
tbl.add_row(['No data returned.'])
123-
124-
print tbl
125-
126-
for stat in statistics:
127-
print stat
128-
129-
return tbl
139+
result_set = [res.split(',') for res in data]
140+
return self.QueryResult(result_set, statistics)
130141

131142
def execution_plan(self, query):
132143
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22
setup(
33
name='redisgraph',
4-
version='0.3',
4+
version='0.4',
55

66
description='RedisGraph Python Client',
77
url='https://github.com/swilly22/redisgraph-py',

0 commit comments

Comments
 (0)