From c4a23c02f487f8c3b3ff0432feaf0e3ee777bb64 Mon Sep 17 00:00:00 2001 From: Corder Guy Date: Tue, 17 Apr 2018 14:59:35 -0500 Subject: [PATCH] Make quotes consistent, misc. pylint --- mocha_server.py | 65 +++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/mocha_server.py b/mocha_server.py index ee4b72a..fb10108 100755 --- a/mocha_server.py +++ b/mocha_server.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -""" +''' Handle API requests to the database -""" +''' import json import sqlite3 from os import path @@ -16,16 +16,16 @@ if not path.exists(DATABASE): def get_users(username_list): - """ + ''' Gets a list of users searching by name. This can also easily be done by user_id. - """ + ''' conn = sqlite3.connect(DATABASE) conn.row_factory = sqlite3.Row cursor = conn.cursor() output = [] for usr in username_list: - cursor.execute("select * from users where username=(?)", (usr, )) + cursor.execute('select * from users where username=(?)', (usr, )) output += cursor.fetchall() output = json.dumps([dict(row) for row in output]) @@ -38,22 +38,22 @@ def get_users(username_list): return output -def get_top_N(search_parameter, desc, N): - """ +def get_top_n(search_parameter, desc, num): + ''' In progress. Currently the query seems to work, but returning a dict does not preserve order. Store an orderd id list and put that in the json? - """ + ''' conn = sqlite3.connect(DATABASE) conn.row_factory = sqlite3.Row cursor = conn.cursor() - if desc == True: - cursor.execute("select * from users order by ? desc limit ?", - (search_parameter, N)) + if desc: + cursor.execute('select * from users order by ? desc limit ?', + (search_parameter, num)) else: - cursor.execute("select * from users order by ? asc limit ?", - (search_parameter, N)) + cursor.execute('select * from users order by ? asc limit ?', + (search_parameter, num)) output = cursor.fetchall() #print(output) @@ -69,36 +69,40 @@ def get_top_N(search_parameter, desc, N): # add new parameters as needed -def update_row(user_id, updated_username): +def update_row(_user_id, _updated_username): + ''' + WIP. + Will add a new user. + ''' print() def insert_row(user_id, username): - """ + ''' Inserts a row for a NEW user with given parameters This may work better with AUTOINCREMENT to avoid arbitrary ids and duplicates - """ + ''' conn = sqlite3.connect(DATABASE) conn.row_factory = sqlite3.Row cursor = conn.cursor() - cursor.execute("insert into users values (?,?)", (user_id, username)) + cursor.execute('insert into users values (?,?)', (user_id, username)) conn.commit() conn.close() def fetch_user(user_id): - """ + ''' Returns a JSON object containing the requested user - Also can return a list of all users if user_id == "*" - """ + Also can return a list of all users if user_id == '*' + ''' conn = sqlite3.connect(DATABASE) conn.row_factory = sqlite3.Row cursor = conn.cursor() if user_id != '*': # must use (?), (item,) format - cursor.execute("SELECT * FROM users WHERE user_id=(?)", (user_id, )) + cursor.execute('SELECT * FROM users WHERE user_id=(?)', (user_id, )) else: - cursor.execute("SELECT * FROM users") + cursor.execute('SELECT * FROM users') output = cursor.fetchall() output = json.dumps([dict(row) for row in output]) @@ -111,18 +115,21 @@ def fetch_user(user_id): return output -def fetch_top_n(n): - output = get_top_N('score', True, n) +def fetch_top_n(num): + ''' + Retrieves the top n users by score. + ''' + output = get_top_n('score', True, num) return output def process_request(uri): - """ + ''' Handles the API endpoint. Currently supports: - - /mocha/users/"user_id" Returns JSON of the specified user + - /mocha/users/'user_id' Returns JSON of the specified user - /mocha/users/* Returns JSON list of all users - """ + ''' parts = uri.split('/')[1:] assert parts[0] == 'mocha' @@ -141,9 +148,9 @@ def process_request(uri): def application(environ, start_response): - """ + ''' mod_wsgi entry point - """ + ''' status = '200 OK' output = process_request(environ['REQUEST_URI'])