-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradingScript.py
349 lines (294 loc) · 8.17 KB
/
gradingScript.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""
1. Navigate through folders
Use os.listdir() and loop through my students
Use recursion?
2. Call functions from files in script
Use sys.path.append(path)
2a. Account for getMediaPath()
Use I/O to place getMediaPath() in each file
3. Read output.txt files and ensure they are similar to test case files
Read file cmp docs
4. Record how many are right vs wrong
5. Print score
"""
import sys,os,subprocess,platform
from os import path
from time import sleep
#################### Set Up ####################
currentPathList = [
"D:/Users/Ricky/Desktop/Coding/Projects/gradingScript/",
"C:/Users/Ricky/Desktop/coding/Projects/gradingScript/",
"C:/Users/rbarillas3/Documents/GitHub/gradingScript/",
"Users/$Users/Documents/coding/Projects/gradingScript/gradingScript.py"
]
currentPath = ""
bulkPathList = [
"C:/Users/%USERNAME%/Downloads/Bulk Download/",
"D:/Users/%USERNAME%/Downloads/Bulk Download/",
"~/Downloads/Bulk Download/"
]
bulkDownload = ""
isWindows = platform.system() == "Windows"
#don't delete
hwPy = ""
answers = []
#to make it compatible with any developing computer
for bulkPath in bulkPathList:
bulkPath = path.expanduser(bulkPath)
if path.exists(bulkPath):
bulkDownload = bulkPath
for path in currentPathList:
if os.path.exists(path):
currentPath = path
#List of student directories
students = os.listdir(bulkDownload)
#################### Function Definitions ####################
def findFile(path, desiredFile):
#if the current directory is empty return none
if not os.path.isdir(path) or len(os.listdir(path)) <= 0:
return (False, "")
items = os.listdir(path)
newPath = path + "/" + items[0]
#if the file is there return the path of its directory
#if it doesnt exist and we can go deeper then go deeper
#else return none
if desiredFile in items:
return (True, path)
elif os.path.isdir(newPath):
return findFile(newPath, desiredFile)
else:
return (False, "")
def findFileExt(path, desiredExtension):
#if the current directory is empty return none
if not os.path.exists(path):
return (False, "", )
if len(os.listdir(path)) <= 0:
return (False, "", )
items = os.listdir(path)
newPath = path + "/" + items[0]
#if the file is there return the path of its directory
#if it doesnt exist and we can go deeper then go deeper
#else return none
#Ternary = "desired outcome" if condition else "other outcome"
for item in items:
if desiredExtension in item:
return (True, item, path)
if os.path.isdir(newPath):
return findFileExt(newPath, desiredExtension)
else:
return (False, "", )
def getMediaPath():
textPathList = [
"C:/Users/Ricky/Downloads/Bulk Download/Media Sources/text/",
"D:/Users/Ricky/Desktop/Classes/CS 1315 TA/text/",
"C:/Users/rbarillas3/Downloads/Bulk Download/Media Sources/text/"
]
for path in textPathList:
if os.path.exists(path):
return path
def navigateAndStore(desiredFile):
counter = 0
studentFilePaths = []
global hwPy
hwPy = desiredFile
#cycle through every student
for student in students:
#makes a path based on current student
path = bulkDownload + student
#lastName = student.split(",")[0].strip()
if desiredFile[0] == ".":
if findFileExt(path, desiredFile)[0]:
counter = counter + 1
studentFilePaths.append(findFileExt(path, desiredFile))
else:
if findFile(path, desiredFile)[0]:
counter = counter + 1
#string type for homework file path
studentFilePaths.append(findFile(path, desiredFile)[1] + "/")
return studentFilePaths
def getNewCode():
codeSource = open("gradingScript.py", "rt")
newCode = "import os\n"
sentinal = ""
while not sentinal == "def getMediaPath():\n":
sentinal = codeSource.readline()
newCode = newCode + sentinal
sentinal = codeSource.readline()
while not sentinal == "\t\t\treturn path\n":
newCode = newCode + sentinal
sentinal = codeSource.readline()
return newCode + sentinal
def setMediaPath2(filePath, addCode):
fileName = hwPy if os.path.exists(filePath + hwPy) else hwPy
testFile = open(filePath + fileName, "r+")
code = testFile.read().replace(getNewCode(), "")
#print code[:50] + "\n"
if addCode:
code = getNewCode() + code
#print code[:50] + "GETTTT\n"
testFile.close()
testFile = open(filePath + fileName, "w")
testFile.write(code)
testFile.close()
def checkAnswer(x):
print x == answers[0]
if not x == answers[0]:
print x
#openHw()
del answers[0]
def problem6(filePath):
output = ""
inputLines = open(filePath + hwPy, "rt").readlines()
print
printBool = False
for line in inputLines:
if line == "# Big O\n" or line == "# problem6\n":
print line
printBool = True
elif printBool:
print line
def debugApostropheProblem(filePath):
returnBool = False
string = ""
with open(filePath + hwPy) as fp:
for i, line in enumerate(fp):
#print line
#raw_input()
if "\x92" in line or "\xe2" in line:
returnBool = True
print
print "Yes it was the apostrope problem\nNow we try to fix it"
sleep(5)
line = line.replace("\x92", " ")
line = line.replace("\xe2", " ")
string = string + line
fp.close()
fp = open(filePath + hwPy, "w")
fp.write(string)
fp.close
return returnBool
def callFunctions(filePath):
sys.path.append(filePath)
global openHw
openHw = lambda : (os.startfile(filePath + hwPy) if isWindows else subprocess.call(["open", filePath + hwPy]))
global answers
answers = [14,14910,"a*b*c","s*m*a*s*h*m*o*u*t*h","(abd)","(1623)",4,7]
try:
hw = __import__(hwPy[:len(hwPy) - 3])
openHw()
reload(hw)
except ImportError, e:
openHw()
hw = __import__(hwPy[:len(hwPy) - 3])
reload(hw)
except SyntaxError, e:
print "Their code had a SyntaxError..."
sleep(1)
print "We are going to see if it's that apostrophe problem"
sleep(1)
if not debugApostropheProblem(filePath):
print "hm it wasnt that"
sleep(3)
print e
openHw()
else:
return
try:
print
print "makePassword:",
hw.makePassword("purple", "%", 442)
print "spell:",
hw.spell("stanchion")
print "babyName:",
hw.babyName("Arthur")
print "babyName:",
hw.babyName("Frank")
print "process:",
hw.process(7)
print "process:",
hw.process(16)
print "getLegalString:",
hw.getLegalString("The dictator is a super cool dude", "tca")
sys.path.remove(filePath)
except AttributeError, e:
print "AttributeError"
print e
sleep(3)
openHw()
except TypeError, e:
print "TypeError"
print e
except NameError, e:
print "NameError"
print e
except IndexError, e:
print "IndexError"
print e
except Exception, e:
raise e
print
raw_input("Oh no their code messed up...")
openHw()
def scriptRunner():
fileList = navigateAndStore("hw.py")
counter = 0
clear = lambda : os.system("cls")
done = False
for filePath in fileList:
lastName = filePath.split("/")[5]
prompt = "Do you want to grade " + lastName[:len(lastName) - 34] + "?"
print lastName[:len(lastName) - 34]
if str(raw_input(prompt)) == "y":
setMediaPath2(filePath, True)
while not done:
callFunctions(filePath)
setMediaPath2(filePath, False)
counter = counter + 1
clear()
#navigateAndStore("hw08.py")
#scriptRunner()
#print getNewCode()
#print open(currentPath + "gradingScript.py", "r").read()
#for item, key in sys.modules.iteritems():
# print item, key
# raw_input()
"""
DEBUG:
Cleaned up Exceptions.. Kinda (commit):
Doesn't reset the command prompt and calls from former students
To re-enact do daniel seal then reba sellers
FIXED:
used __import__("module") and reload(module)
"""
"""
For some reason in the Note: Suppress Return commit it prints none
Learn how to suppress unwanted return statements
"""
"""
change reference of:
from HW01 import *
to:
from HWa import *
Make HWa point to an item from a list of references
var = __import__("x")
module x contains function test()
var.test() doesnt throw errer
"""
"""
Merge findFile and findFileExt for eloquence
"""
"""
Make this file a library
"""
"""
Maybe make gradingScript a class and call functions from it to make it more dynamic
"""
"""
Dynamic grading. For the first several students I input
my comments like normal but I will never repeat my self.
The script will know that some error equals an equivalent comment
and it will autofill the comment for me.
"""
"""
Import JES's library.... duh
"""