From c7f3202b8839ff3d5690d9eeecbad32d3d66d063 Mon Sep 17 00:00:00 2001 From: Whodiduexpect <53096512+Whodiduexpect@users.noreply.github.com> Date: Tue, 8 Oct 2019 20:16:48 -0700 Subject: [PATCH] Fix style and typo in error message --- assignment_list.py | 72 ++++++++++++++++--------------- studentvue_parser.py | 23 +++++----- tests/help_screen_test.py | 6 +-- tests/invalid_credentials_test.py | 10 ++--- 4 files changed, 56 insertions(+), 55 deletions(-) diff --git a/assignment_list.py b/assignment_list.py index 7172992..3323531 100644 --- a/assignment_list.py +++ b/assignment_list.py @@ -1,19 +1,21 @@ #!/usr/bin/env python3 -import sys import os +import random +import sys +from pathlib import Path + import click import pandas as pd -from pathlib import Path + import studentvue_parser -import random # Define functions -def openData(file, mode): +def open_data(file, mode): return open(Path('data/%s' % file), mode) -def listAssignments(assignments): +def list_assignments(assignments): if not len(assignments): click.echo('No assignments to do!') else: @@ -22,7 +24,7 @@ def listAssignments(assignments): click.echo(assignments_view) -def listCompleted(assignments): +def list_completed(assignments): if not len(assignments): click.echo('No assignments completed yet.') else: @@ -31,27 +33,27 @@ def listCompleted(assignments): click.echo(assignments_view) -def getDataFromFile(filename, split_char): - file = openData(filename, 'r') +def get_data_from_file(filename, split_char): + file = open_data(filename, 'r') output = file.read().split(split_char) file.close() return output -def updateCsv(assignments): +def update_csv(assignments): assignments.to_csv( Path('data/studentvue_assignments.csv'), sep='/', index=False) -def createIfNotExist(filename): +def create_if_not_exist(filename): if not os.path.exists(Path('data/%s' % filename)): - file = openData(filename, 'w') + file = open_data(filename, 'w') file.close() -def pointProblematicFile(path): +def point_problematic_file(path): click.echo('Full path to problematic file: %s' % Path(path).absolute()) @@ -64,39 +66,39 @@ def main(): # Create one os.mkdir('data') # Now that we know we have a data folder, let's set the credentials - studentvue_parser.setCredentials() + studentvue_parser.set_credentials() # Read the credential file - credentials = getDataFromFile('studentvue_credentials', ',') + credentials = get_data_from_file('studentvue_credentials', ',') # Define commands @click.group() def cli(): - ''' + """ A Student Vue assignment manager - ''' + """ @cli.command() def reset(): - ''' + """ Reset credentials - ''' - studentvue_parser.setCredentials() + """ + studentvue_parser.set_credentials() @cli.command() @click.argument('category', required=False) def list(category): - ''' + """ List assignments by CATEGORY (defaults to 'current') - ''' + """ if not category: category = 'current' if category == 'current': - assignments = studentvue_parser.getAssignments(credentials) - listAssignments(assignments) + assignments = studentvue_parser.get_assignments(credentials) + list_assignments(assignments) elif category == 'completed': - assignments = studentvue_parser.getAssignments(credentials) - listCompleted(assignments) + assignments = studentvue_parser.get_assignments(credentials) + list_completed(assignments) else: click.echo( '"%s" is not a valid category. Currently, the only valid category is current' % @@ -105,16 +107,16 @@ def list(category): @cli.command() @click.argument('id') def complete(id): - ''' + """ Complete an assignment by assignment ID - ''' - assignments = studentvue_parser.getAssignments(credentials) + """ + assignments = studentvue_parser.get_assignments(credentials) try: assignments.at[assignments.loc[assignments['Assignment ID'].isin( [id])].index, 'is_completed'] = True - updateCsv(assignments) + update_csv(assignments) except BaseException: - click.echo('Failed to complete assignemnt #%s' % id) + click.echo('Failed to complete assignment #%s' % id) sys.exit() click.echo('Marked assignment #%s as complete' % id) @@ -122,9 +124,9 @@ def complete(id): @click.argument('title') @click.argument('date') def add(title, date): - ''' + """ Add a new assignment with the TITLE and DATE - ''' + """ # Get schedule classes = studentvue_parser.getSchedule(credentials) @@ -160,7 +162,7 @@ def add(title, date): classes[class_period - 1].period) # Generate a assignment list 8 digit id, and make sure it's unique - assignments = studentvue_parser.getAssignments(credentials) + assignments = studentvue_parser.get_assignments(credentials) id_unique = False while not id_unique: @@ -174,11 +176,11 @@ def add(title, date): # Make the assignment based on data we have collected/generated assignment_df = pd.DataFrame({'Assignment ID': [generated_id], 'Class Name': [ - class_name], 'Due Date': [date], 'Assignment': [title], 'is_completed': ['False']}) + class_name], 'Due Date': [date], 'Assignment': [title], 'is_completed': ['False']}) assignments = assignments.append(assignment_df, sort=True) convert_dict = {'Assignment ID': int} assignments = assignments.astype(convert_dict) - updateCsv(assignments) + update_csv(assignments) click.echo('Successfully added assignment "{0}"'.format(title)) # Execute command diff --git a/studentvue_parser.py b/studentvue_parser.py index 82144dc..22acc6b 100644 --- a/studentvue_parser.py +++ b/studentvue_parser.py @@ -7,19 +7,19 @@ from getpass import getpass -def setCredentials(): - assignment_list.createIfNotExist('studentvue_credentials') +def set_credentials(): + assignment_list.create_if_not_exist('studentvue_credentials') click.echo('-- Credentials setup/reset --') student_id = input('Please enter Student ID: ') password = getpass('Please enter password (hidden): ') district_url = input('Please enter district login url: ') - file = assignment_list.openData('studentvue_credentials', 'w') + file = assignment_list.open_data('studentvue_credentials', 'w') file.write(student_id + ',' + password + ',' + district_url) file.close() return True -def getAssignments(credentials): +def get_assignments(credentials): # Login to Student Vue try: sv = StudentVue(credentials[0], credentials[1], credentials[2]) @@ -28,11 +28,10 @@ def getAssignments(credentials): 'Invalid credentials. You might want to use:\n python assignment_list.py reset') sys.exit() - # Create CSV file if it dosen't exist - assignment_list.createIfNotExist('studentvue_assignments.csv') - assignments = [] + # Create CSV file if it doesn't exist + assignment_list.create_if_not_exist('studentvue_assignments.csv') try: - assignments = pd.read_csv(Path('data/studentvue_assignments.csv'), # reads 'assignment_id', 'class_name', 'date', 'name', 'is_completed' + assignments = pd.read_csv(Path('data/studentvue_assignments.csv'), sep='/') except BaseException: assignments = pd.DataFrame({'Assignment ID': [], 'Class Name': [], 'Due Date': [ @@ -40,10 +39,10 @@ def getAssignments(credentials): click.echo('Invalid or empty file detected... discarding file') try: studentvue_assignments = sv.get_assignments() - except Exception as e: + except BaseException: click.echo( - 'Failed to get Student Vue assignments... Please see https://github.com/kajchang/StudentVue for help on this issue.\n The specific error is:', - sys.exc_info()[1]) + 'Failed to get Student Vue assignments... Please see https://github.com/kajchang/StudentVue for help on ' + 'this issue.') sys.exit() found_duplicate = False for sv_assignment in studentvue_assignments: @@ -63,7 +62,7 @@ def getAssignments(credentials): assignments = assignments.append(assignment_df, sort=True) convert_dict = {'Assignment ID': int} assignments = assignments.astype(convert_dict) - assignment_list.updateCsv(assignments) + assignment_list.update_csv(assignments) return assignments diff --git a/tests/help_screen_test.py b/tests/help_screen_test.py index 31ba961..9b61b06 100644 --- a/tests/help_screen_test.py +++ b/tests/help_screen_test.py @@ -1,7 +1,7 @@ -import pytest +import os import subprocess import sys -import os + sys.path.append("") import assignment_list @@ -9,7 +9,7 @@ def test_answer(): if not os.path.exists("data"): os.mkdir("data") - assignment_list.createIfNotExist("studentvue_credentials") + assignment_list.create_if_not_exist("studentvue_credentials") result = subprocess.run( ['python3', 'assignment_list.py'], stdout=subprocess.PIPE) assert "Usage:" in result.stdout.decode("utf-8") diff --git a/tests/invalid_credentials_test.py b/tests/invalid_credentials_test.py index 32014db..d1a0887 100644 --- a/tests/invalid_credentials_test.py +++ b/tests/invalid_credentials_test.py @@ -1,7 +1,7 @@ -import pytest +import os import subprocess import sys -import os + sys.path.append('') import assignment_list @@ -9,14 +9,14 @@ def test_answer(): if not os.path.exists('data'): os.mkdir('data') - file = assignment_list.openData('studentvue_credentials', 'r') + file = assignment_list.open_data('studentvue_credentials', 'r') contents = file.read() file.close() - file = assignment_list.openData('studentvue_credentials', 'w+') + file = assignment_list.open_data('studentvue_credentials', 'w+') file.close() result = subprocess.run( ['python3', 'assignment_list.py', 'list'], stdout=subprocess.PIPE) - file = assignment_list.openData('studentvue_credentials', 'w+') + file = assignment_list.open_data('studentvue_credentials', 'w+') file.write(contents) file.close() assert "python assignment_list.py reset" in result.stdout.decode("utf-8")