-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradeAssignment.py
More file actions
105 lines (89 loc) · 3.1 KB
/
Copy pathgradeAssignment.py
File metadata and controls
105 lines (89 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import sqlite3
import sys
def connect_to_database(database_name):
"""
Connect to database and create a cursor object.
:param database_name: The name of the database to connect
:return: Returns a tuple containing connection and cursor objects
"""
conn = sqlite3.connect(database_name)
cursor = conn.cursor()
return conn, cursor
def tearDown(conn,cursor):
# close cursor and connection objects
cursor.close()
conn.close()
def runStudentQueries(studentFile,cursor):
"""
This Function runs the queries in the Student file or the student queries
:param studentFile: The student text file that contains their SQL solutions
:param cursor: Cursor object
:return: The results of the query solutions
"""
studentSolutions = open(studentFile, 'r')
studentSolutionsLines = studentSolutions.readlines()
studentAnswers = []
studentResults = []
for studentLine in studentSolutionsLines:
studentAnswers.append(studentLine.rstrip())
for answer in studentAnswers:
cursor.execute(answer)
studentResult = cursor.fetchall()
studentResults.append(studentResult)
return studentResults
def runKeyQueries(answerFile,cursor):
"""
This Function runs the queries in the answer file
:param answerFile: The text file that holds the SQL solutions
:param cursor: Cursor object
:return: Returns the results of the SQL queries
"""
solutionFile = open(answerFile, 'r')
solutionLines = solutionFile.readlines()
AnswerKey = []
solutionResults = []
for line in solutionLines:
AnswerKey.append(line.rstrip())
for keyAnswer in AnswerKey:
cursor.execute(keyAnswer)
solutionResult = cursor.fetchall()
solutionResults.append(solutionResult)
return solutionResults
def test_query_results(studentAns, Answers):
"""
This function compares the student queries to the solutions and results of how
were correct or wrong.
:param studentAns:
:param Answers:
:return: None
"""
passed, failed, total = 0, 0, 0
total = len(Answers)
for x, answer in enumerate(Answers):
if answer == studentAns[x]:
passed += 1
print(f"Question {x + 1} is Correct")
else:
failed += 1
print(f"Question {x + 1} is Wrong")
print(f"\n{passed} out of {total} questions are correct.")
print('You received ' + str((passed / total) * 100) + '%')
def main():
if len(sys.argv) < 2:
print('Please specify student solution text file')
elif len(sys.argv) == 2:
studentAnsFileName = sys.argv[1]
AnswersFileName = 'solutions.txt'
elif len(sys.argv) == 3:
studentAnsFileName = sys.argv[1]
AnswersFileName = sys.argv[2]
else:
print('Invalid number of arguments')
print(len(sys.argv))
sys.exit(1)
conn, cursor = connect_to_database("worker_project.db")
studentAns = runStudentQueries(studentAnsFileName,cursor)
Answers = runKeyQueries(AnswersFileName,cursor)
test_query_results(studentAns, Answers)
tearDown(conn,cursor)
main()