From eaca5354d3f41a195ef8a4e47fc7b8915ef7f4b3 Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Thu, 27 Sep 2018 11:03:44 +0300 Subject: [PATCH] app.py: Iterate directly on cursor We don't need to create an intermediate variable for the results of the SQL query because psycopg2's cursor is iterable. See: http://initd.org/psycopg/docs/cursor.html --- app.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index f3833c2..e2794c4 100644 --- a/app.py +++ b/app.py @@ -22,16 +22,16 @@ class AllItemsResource: # get statistics, ordered by id, and use limit and offset to page through results cursor.execute('SELECT id, views, downloads FROM items ORDER BY id ASC LIMIT {} OFFSET {}'.format(limit, offset)) - results = cursor.fetchmany(limit) - cursor.close() # create a list to hold dicts of item stats statistics = list() # iterate over results and build statistics object - for item in results: + for item in cursor: statistics.append({ 'id': item['id'], 'views': item['views'], 'downloads': item['downloads'] }) + cursor.close() + message = { 'currentPage': page, 'totalPages': pages,