Make quotes consistent, misc. pylint

This commit is contained in:
Corder Guy 2018-04-17 14:59:35 -05:00
parent bd461c9be9
commit c4a23c02f4

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" '''
Handle API requests to the database Handle API requests to the database
""" '''
import json import json
import sqlite3 import sqlite3
from os import path from os import path
@ -16,16 +16,16 @@ if not path.exists(DATABASE):
def get_users(username_list): def get_users(username_list):
""" '''
Gets a list of users searching by name. Gets a list of users searching by name.
This can also easily be done by user_id. This can also easily be done by user_id.
""" '''
conn = sqlite3.connect(DATABASE) conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
cursor = conn.cursor() cursor = conn.cursor()
output = [] output = []
for usr in username_list: 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 += cursor.fetchall()
output = json.dumps([dict(row) for row in output]) output = json.dumps([dict(row) for row in output])
@ -38,22 +38,22 @@ def get_users(username_list):
return output return output
def get_top_N(search_parameter, desc, N): def get_top_n(search_parameter, desc, num):
""" '''
In progress. In progress.
Currently the query seems to work, but returning a dict does not preserve order. 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? Store an orderd id list and put that in the json?
""" '''
conn = sqlite3.connect(DATABASE) conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
cursor = conn.cursor() cursor = conn.cursor()
if desc == True: if desc:
cursor.execute("select * from users order by ? desc limit ?", cursor.execute('select * from users order by ? desc limit ?',
(search_parameter, N)) (search_parameter, num))
else: else:
cursor.execute("select * from users order by ? asc limit ?", cursor.execute('select * from users order by ? asc limit ?',
(search_parameter, N)) (search_parameter, num))
output = cursor.fetchall() output = cursor.fetchall()
#print(output) #print(output)
@ -69,36 +69,40 @@ def get_top_N(search_parameter, desc, N):
# add new parameters as needed # 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() print()
def insert_row(user_id, username): def insert_row(user_id, username):
""" '''
Inserts a row for a NEW user with given parameters Inserts a row for a NEW user with given parameters
This may work better with AUTOINCREMENT to avoid arbitrary ids and duplicates This may work better with AUTOINCREMENT to avoid arbitrary ids and duplicates
""" '''
conn = sqlite3.connect(DATABASE) conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("insert into users values (?,?)", (user_id, username)) cursor.execute('insert into users values (?,?)', (user_id, username))
conn.commit() conn.commit()
conn.close() conn.close()
def fetch_user(user_id): def fetch_user(user_id):
""" '''
Returns a JSON object containing the requested user 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 = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
cursor = conn.cursor() cursor = conn.cursor()
if user_id != '*': # must use (?), (item,) format 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: else:
cursor.execute("SELECT * FROM users") cursor.execute('SELECT * FROM users')
output = cursor.fetchall() output = cursor.fetchall()
output = json.dumps([dict(row) for row in output]) output = json.dumps([dict(row) for row in output])
@ -111,18 +115,21 @@ def fetch_user(user_id):
return output return output
def fetch_top_n(n): def fetch_top_n(num):
output = get_top_N('score', True, n) '''
Retrieves the top n users by score.
'''
output = get_top_n('score', True, num)
return output return output
def process_request(uri): def process_request(uri):
""" '''
Handles the API endpoint. Handles the API endpoint.
Currently supports: 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 - /mocha/users/* Returns JSON list of all users
""" '''
parts = uri.split('/')[1:] parts = uri.split('/')[1:]
assert parts[0] == 'mocha' assert parts[0] == 'mocha'
@ -141,9 +148,9 @@ def process_request(uri):
def application(environ, start_response): def application(environ, start_response):
""" '''
mod_wsgi entry point mod_wsgi entry point
""" '''
status = '200 OK' status = '200 OK'
output = process_request(environ['REQUEST_URI']) output = process_request(environ['REQUEST_URI'])