From cc5ce3ab98316b56e03880fb6eb571cc0d731d40 Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Sat, 3 Nov 2018 23:55:23 +0200 Subject: [PATCH] Correct issues highlighted by Flake8 Flake8 validates code style against PEP 8 in order to encourage the writing of idiomatic Python. For reference, I am currently ignoring errors about line length (E501) because I feel it makes code harder to read. This is the invocation I am using: $ flake8 --ignore E501 dspace_statistics_api --- dspace_statistics_api/app.py | 18 +++++--- dspace_statistics_api/database.py | 4 +- dspace_statistics_api/indexer.py | 71 ++++++++++++++++--------------- dspace_statistics_api/solr.py | 1 + 4 files changed, 52 insertions(+), 42 deletions(-) diff --git a/dspace_statistics_api/app.py b/dspace_statistics_api/app.py index 4cc6e27..6ca55f5 100644 --- a/dspace_statistics_api/app.py +++ b/dspace_statistics_api/app.py @@ -4,6 +4,7 @@ import falcon db = database_connection() db.set_session(readonly=True) + class RootResource: def on_get(self, req, resp): resp.status = falcon.HTTP_200 @@ -11,6 +12,7 @@ class RootResource: with open('dspace_statistics_api/docs/index.html', 'r') as f: resp.body = f.read() + class AllItemsResource: def on_get(self, req, resp): """Handles GET requests""" @@ -33,19 +35,20 @@ class AllItemsResource: # iterate over results and build statistics object for item in cursor: - statistics.append({ 'id': item['id'], 'views': item['views'], 'downloads': item['downloads'] }) + statistics.append({'id': item['id'], 'views': item['views'], 'downloads': item['downloads']}) cursor.close() message = { - 'currentPage': page, - 'totalPages': pages, - 'limit': limit, - 'statistics': statistics + 'currentPage': page, + 'totalPages': pages, + 'limit': limit, + 'statistics': statistics } resp.media = message + class ItemResource: def on_get(self, req, resp, item_id): """Handles GET requests""" @@ -54,8 +57,8 @@ class ItemResource: cursor.execute('SELECT views, downloads FROM items WHERE id={}'.format(item_id)) if cursor.rowcount == 0: raise falcon.HTTPNotFound( - title='Item not found', - description='The item with id "{}" was not found.'.format(item_id) + title='Item not found', + description='The item with id "{}" was not found.'.format(item_id) ) else: results = cursor.fetchone() @@ -70,6 +73,7 @@ class ItemResource: cursor.close() + api = application = falcon.API() api.add_route('/', RootResource()) api.add_route('/items', AllItemsResource()) diff --git a/dspace_statistics_api/database.py b/dspace_statistics_api/database.py index ddcd9b0..183815f 100644 --- a/dspace_statistics_api/database.py +++ b/dspace_statistics_api/database.py @@ -3,7 +3,9 @@ from .config import DATABASE_USER from .config import DATABASE_PASS from .config import DATABASE_HOST from .config import DATABASE_PORT -import psycopg2, psycopg2.extras +import psycopg2 +import psycopg2.extras + def database_connection(): connection = psycopg2.connect("dbname={} user={} password={} host={} port={}".format(DATABASE_NAME, DATABASE_USER, DATABASE_PASS, DATABASE_HOST, DATABASE_PORT), cursor_factory=psycopg2.extras.DictCursor) diff --git a/dspace_statistics_api/indexer.py b/dspace_statistics_api/indexer.py index 1d43d62..1adf1f6 100644 --- a/dspace_statistics_api/indexer.py +++ b/dspace_statistics_api/indexer.py @@ -34,6 +34,7 @@ import json import psycopg2.extras from .solr import solr_connection + def index_views(): # get total number of distinct facets for items with a minimum of 1 view, # otherwise Solr returns all kinds of weird ids that are actually not in @@ -42,16 +43,16 @@ def index_views(): # # see: https://lucene.apache.org/solr/guide/6_6/the-stats-component.html res = solr.query('statistics', { - 'q':'type:2', - 'fq':'isBot:false AND statistics_type:view', - 'facet':True, - 'facet.field':'id', - 'facet.mincount':1, - 'facet.limit':1, - 'facet.offset':0, - 'stats':True, - 'stats.field':'id', - 'stats.calcdistinct':True + 'q': 'type:2', + 'fq': 'isBot:false AND statistics_type:view', + 'facet': True, + 'facet.field': 'id', + 'facet.mincount': 1, + 'facet.limit': 1, + 'facet.offset': 0, + 'stats': True, + 'stats.field': 'id', + 'stats.calcdistinct': True }, rows=0) # get total number of distinct facets (countDistinct) @@ -71,13 +72,13 @@ def index_views(): print('Indexing item views (page {} of {})'.format(results_current_page, results_num_pages)) res = solr.query('statistics', { - 'q':'type:2', - 'fq':'isBot:false AND statistics_type:view', - 'facet':True, - 'facet.field':'id', - 'facet.mincount':1, - 'facet.limit':results_per_page, - 'facet.offset':results_current_page * results_per_page + 'q': 'type:2', + 'fq': 'isBot:false AND statistics_type:view', + 'facet': True, + 'facet.field': 'id', + 'facet.mincount': 1, + 'facet.limit': results_per_page, + 'facet.offset': results_current_page * results_per_page }, rows=0) # SolrClient's get_facets() returns a dict of dicts @@ -98,19 +99,20 @@ def index_views(): cursor.close() + def index_downloads(): # get the total number of distinct facets for items with at least 1 download res = solr.query('statistics', { - 'q':'type:0', - 'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL', - 'facet':True, - 'facet.field':'owningItem', - 'facet.mincount':1, - 'facet.limit':1, - 'facet.offset':0, - 'stats':True, - 'stats.field':'owningItem', - 'stats.calcdistinct':True + 'q': 'type:0', + 'fq': 'isBot:false AND statistics_type:view AND bundleName:ORIGINAL', + 'facet': True, + 'facet.field': 'owningItem', + 'facet.mincount': 1, + 'facet.limit': 1, + 'facet.offset': 0, + 'stats': True, + 'stats.field': 'owningItem', + 'stats.calcdistinct': True }, rows=0) # get total number of distinct facets (countDistinct) @@ -130,13 +132,13 @@ def index_downloads(): print('Indexing item downloads (page {} of {})'.format(results_current_page, results_num_pages)) res = solr.query('statistics', { - 'q':'type:0', - 'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL', - 'facet':True, - 'facet.field':'owningItem', - 'facet.mincount':1, - 'facet.limit':results_per_page, - 'facet.offset':results_current_page * results_per_page + 'q': 'type:0', + 'fq': 'isBot:false AND statistics_type:view AND bundleName:ORIGINAL', + 'facet': True, + 'facet.field': 'owningItem', + 'facet.mincount': 1, + 'facet.limit': results_per_page, + 'facet.offset': results_current_page * results_per_page }, rows=0) # SolrClient's get_facets() returns a dict of dicts @@ -157,6 +159,7 @@ def index_downloads(): cursor.close() + db = database_connection() solr = solr_connection() diff --git a/dspace_statistics_api/solr.py b/dspace_statistics_api/solr.py index 96a5a68..322e21c 100644 --- a/dspace_statistics_api/solr.py +++ b/dspace_statistics_api/solr.py @@ -1,6 +1,7 @@ from .config import SOLR_SERVER from SolrClient import SolrClient + def solr_connection(): connection = SolrClient(SOLR_SERVER)