F01.justanswer.com



# define a function my_join, and take a index parameter for recursion purposedef my_join(strings, char, index=0): # if index + 1 is less than number of strings means next string also available if( index+1 < len(strings)): # if next string available then add char to present string and call the same function with incremented index return strings[index] + char + my_join(strings,char,index+1) # if index+1 eqals length it means last string elif(index+1 == len(strings)): # if it is a last string then return that string return strings[index]TESTER STARTS HERE: # Based on testing harness dated 2017-06-02.# STUDENTS: TO USE:# # The following command will test all test cases on your file:# # python3 <thisfile.py> <your_one_file.py># # # You can also limit the tester to only the functions you want tested.# Just add as many functions as you want tested on to the command line at the end.# Example: to only run tests associated with func1 and func2, run this command:# # python3 <thisfile.py> <your_one_file.py> func1 func2# # You really don't need to read the file any further, except that when# a specific test fails, you'll get a line number - and it's certainly# worth looking at those areas for details on what's being checked. This would# all be the indented block of code starting with "class AllTests".# INSTRUCTOR: TO PREPARE:# - add test cases to class AllTests. The test case functions' names must# be precise - to test a function named foobar, the test must be named "test_foobar_#"# where # may be any digits at the end, such as "test_foobar_13".# - any extra-credit tests must be named "test_extra_credit_foobar_#"# # - name all required definitions in REQUIRED_DEFNS, and all extra credit functions# in EXTRA_CREDIT_DEFNS. Do not include any unofficial helper functions. If you want# to make helper definitions to use while testing, those can also be added there for# clarity.# # - to run on either a single file or all .py files in a folder (recursively):# python3 <thisfile.py> <your_one_file.py># python3 <thisfile.py> <dir_of_files># python3 <thisfile.py> . # current directory# # A work in progress by Mark Snyder, Oct. 2015.# Edited by Yutao Zhong, Spring 2016.# Edited by Raven Russell, Spring 2017.# Edited by Mark Snyder, June 2017.import unittestimport shutilimport sysimport osimport time######################################################################################################################################################### BEGIN SPECIALIZATION SECTION (the only part you need to modify beyond # adding new test cases).# name all expected definitions; if present, their definition (with correct# number of arguments) will be used; if not, a decoy complainer function# will be used, and all tests on that function should fail. REQUIRED_DEFNS = ['my_join', 'skip_sum', 'is_palindrome', 'merge']# for method names in classes that will be testedSUB_DEFNS = []# definitions that are used for extra creditEXTRA_CREDIT_DEFNS = ['largest_sum']# how many points are test cases worth?weight_required = .75weight_extra_credit = 1# don't count extra credit; usually 100% if this is graded entirely by tests.# it's up to you the instructor to do the math and add this up!# TODO: auto-calculate this based on all possible tests.total_points_from_tests = 60# how many seconds to wait between batch-mode gradings? # ideally we could enforce python to wait to open or import# files when the system is ready but we've got a communication# gap going on.DELAY_OF_SHAME = 1# set it to true when you run batch mode... CURRENTLY_GRADING = False# what temporary file name should be used for the student?# This can't be changed without hardcoding imports below, sorry.# That's kind of the whole gimmick here that lets us import from# the command-line argument without having to qualify the names.RENAMED_FILE = "student"# END SPECIALIZATION SECTION######################################################################################################################################################### enter batch mode by giving a directory to work on as the only argument.BATCH_MODE = len(sys.argv)==2 and (sys.argv[1] in ["."] or os.path.isdir(sys.argv[1]))# This class contains multiple "unit tests" that each check# various inputs to specific functions, checking that we get# the correct behavior (output value) from completing the call.class AllTests (unittest.TestCase): ############################################################################ # my_join tests def test_my_join_00(self): self.assertEqual(my_join(['a'], '*'), 'a') def test_my_join_01(self): self.assertEqual(my_join(['a','b'], '*'), 'a*b') def test_my_join_02(self): self.assertEqual(my_join(['x','y','z'], '$'), 'x$y$z') def test_my_join_03(self): self.assertEqual(my_join(['a','a','a'],'a'), 'aaaaa') def test_my_join_04(self): self.assertEqual(my_join(['a','a','a','a'], 'q'), 'aqaqaqa') def test_my_join_05(self): self.assertEqual(my_join(['a','b','c','d','e','f','g'],'@'), 'a@b@c@d@e@f@g') def test_my_join_06(self): self.assertEqual(my_join(['z','y','x','w','v','u','t','s','r'], 'A'), 'zAyAxAwAvAuAtAsAr') def test_my_join_07(self): self.assertEqual(my_join(['1','2','3'],'*'), '1*2*3') def test_my_join_08(self): self.assertEqual(my_join(['1','2','3','4','5','6','7','8','9'],','), '1,2,3,4,5,6,7,8,9') def test_my_join_09(self): self.assertEqual(my_join(['1','2','3','a','b','c'], ''), '123abc') def test_my_join_10(self): self.assertEqual(my_join(['aa'],'|'), 'aa') def test_my_join_11(self): self.assertEqual(my_join(['aa','bb'],'|'), 'aa|bb') def test_my_join_12(self): self.assertEqual(my_join(['abc','def','ghi'], '&'), 'abc&def&ghi') def test_my_join_13(self): self.assertEqual(my_join(['the','quick','brown','fox'], " "), 'the quick brown fox') def test_my_join_14(self): self.assertEqual(my_join(['jumped','over','the','lazy','river'], '8'), 'jumped8over8the8lazy8river') def test_my_join_15(self): self.assertEqual(my_join(['abc','123'], '#'), 'abc#123') def test_my_join_16(self): self.assertEqual(my_join(['ABC123','123abc','4q5r6x'], ''), 'ABC123123abc4q5r6x') def test_my_join_17(self): self.assertEqual(my_join(['ABC123','123abc','4q5r6x'], '0'), 'ABC1230123abc04q5r6x') def test_my_join_18(self): self.assertEqual(my_join(['0ABC1230','0123abc','04q5r6x0'], '0'), '0ABC123000123abc004q5r6x0') def test_my_join_19(self): self.assertEqual(my_join([],'x'), '') # skip_sum tests def test_skip_sum_00(self): self.assertEqual(skip_sum([1,2,3,4,5,6], 2), 9) def test_skip_sum_01(self): self.assertEqual(skip_sum([1,2,3,4,5,6], 3), 5) def test_skip_sum_02(self): self.assertEqual(skip_sum([1,2,3], 1), 6) def test_skip_sum_03(self): self.assertEqual(skip_sum([1,2,3], 3), 1) def test_skip_sum_04(self): self.assertEqual(skip_sum([3, -1, 3, 2, -3], 2),3) def test_skip_sum_05(self): self.assertEqual(skip_sum([3, -1, 3, 2, -3], 4),0) def test_skip_sum_06(self): self.assertEqual(skip_sum([-5, 2, 2, 0, -4, 3, 5, -5, 0, 4], 6), 0) def test_skip_sum_07(self): self.assertEqual(skip_sum([-5, 2, 2, 0, -4, 3, 5, -5, 0, 4], 3), 4) def test_skip_sum_08(self): self.assertEqual(skip_sum([5, -8, 1, -6, 6, -1, -10, -8, 10, -3], 2), 12) def test_skip_sum_09(self): self.assertEqual(skip_sum([5, -8, 1, -6, 6, -1, -10, -8, 10, -3], 9), 2) def test_skip_sum_10(self): self.assertEqual(skip_sum([-1, 15, -16, -1, 14, 16, -15, -11, 6, -14, -10, -10, -8, 16, 10], 1), -9) def test_skip_sum_11(self): self.assertEqual(skip_sum([-1, 15, -16, -1, 14, 16, -15, -11, 6, -14, -10, -10, -8, 16, 10], 10), -11) def test_skip_sum_12(self): self.assertEqual(skip_sum([40, 9, 22, 16, 50, 45, 31, 35, 19, 17, 46, 28, 2, 47, 19, 24, 12, 27, 21, 29], 2), 262) def test_skip_sum_13(self): self.assertEqual(skip_sum([40, 9, 22, 16, 50, 45, 31, 35, 19, 17, 46, 28, 2, 47, 19, 24, 12, 27, 21, 29], 6), 94) def test_skip_sum_14(self): self.assertEqual(skip_sum([84, 12, -39, -59, -93, -3, 26, 74, -72, -25, 33, 57, -43, 89, 14, -27, 3, 6, -44, -26, -83, -73, -93, 79, -61, 13, 36, -30, 6, 57, -9, -41, 12, 22, 27, 13, 24, 99, 55, -8, -51, -31, -72, -52, -68, 74, -90, -50, 65, -89], 7), -43) def test_skip_sum_15(self): self.assertEqual(skip_sum([84, 12, -39, -59, -93, -3, 26, 74, -72, -25, 33, 57, -43, 89, 14, -27, 3, 6, -44, -26, -83, -73, -93, 79, -61, 13, 36, -30, 6, 57, -9, -41, 12, 22, 27, 13, 24, 99, 55, -8, -51, -31, -72, -52, -68, 74, -90, -50, 65, -89], 1), -352) def test_skip_sum_16(self): self.assertEqual(skip_sum([-23, 90, -57, 97, 31, -99, 33, 84, -97, 3, 32, -83, -29, -25, -22, -3, -80, -22, -89, 68, 28, 98, -49, -13, 85, 71, -58, -90, 13, 14, 12, 14, -55, -57, 40, -69, -92, 97, 4, 72, -29, -45, -74, -12, -74, -7, 81, -99, -88, 16], 12), -147) def test_skip_sum_17(self): self.assertEqual(skip_sum([-23, 90, -57, 97, 31, -99, 33, 84, -97, 3, 32, -83, -29, -25, -22, -3, -80, -22, -89, 68, 28, 98, -49, -13, 85, 71, -58, -90, 13, 14, 12, 14, -55, -57, 40, -69, -92, 97, 4, 72, -29, -45, -74, -12, -74, -7, 81, -99, -88, 16], 8), -287) def test_skip_sum_18(self): self.assertEqual(skip_sum([479, -30, 680, 767, -986, -543, -189, -175, 564, -959, 365, -876, -627, -578, 828, -531, 765, 688, -271, 106, 174, 712, 282, -409, 248, -212, -791, -39, -490, -953, -766, 974, -341, 603, -527, -147, -623, -178, -958, -499, 360, -254, -401, 369, -578, -669, 935, 513, -149, 176], 5), -1490) def test_skip_sum_19(self): self.assertEqual(skip_sum([479, -30, 680, 767, -986, -543, -189, -175, 564, -959, 365, -876, -627, -578, 828, -531, 765, 688, -271, 106, 174, 712, 282, -409, 248, -212, -791, -39, -490, -953, -766, 974, -341, 603, -527, -147, -623, -178, -958, -499, 360, -254, -401, 369, -578, -669, 935, 513, -149, 176], 10), 612) # is_palindrome tests def test_is_palindrome_00(self): self.assertEqual(is_palindrome('tacocat'), True) def test_is_palindrome_01(self): self.assertEqual(is_palindrome('CIVIC'), True) def test_is_palindrome_02(self): self.assertEqual(is_palindrome('kayak'), True) def test_is_palindrome_03(self): self.assertEqual(is_palindrome('taCoCat'), True) def test_is_palindrome_04(self): self.assertEqual(is_palindrome('rotator'), True) def test_is_palindrome_05(self): self.assertEqual(is_palindrome('z'), True) def test_is_palindrome_06(self): self.assertEqual(is_palindrome('aa'), True) def test_is_palindrome_07(self): self.assertEqual(is_palindrome('12344321'), True) def test_is_palindrome_08(self): self.assertEqual(is_palindrome('nolemonnomelon'), True) def test_is_palindrome_09(self): self.assertEqual(is_palindrome('redrumsirismurder'), True) def test_is_palindrome_10(self): self.assertEqual(is_palindrome('watermelon'), False) def test_is_palindrome_11(self): self.assertEqual(is_palindrome('python'), False) def test_is_palindrome_12(self): self.assertEqual(is_palindrome('program'), False) def test_is_palindrome_13(self): self.assertEqual(is_palindrome('banana'), False) def test_is_palindrome_14(self): self.assertEqual(is_palindrome('taco cat'), False) def test_is_palindrome_15(self): self.assertEqual(is_palindrome('notapalindrome'), False) def test_is_palindrome_16(self): self.assertEqual(is_palindrome('ABABbaba'), False) def test_is_palindrome_17(self): self.assertEqual(is_palindrome('testset1'), False) def test_is_palindrome_18(self): self.assertEqual(is_palindrome('Tacocat'), False) def test_is_palindrome_19(self): self.assertEqual(is_palindrome('superlongsentencethatisdefinitelynotapalindrome'), False) # merge tests def test_merge_00(self): self.assertEqual(merge([1],[1]), [1,1]) def test_merge_01(self): self.assertEqual(merge([0],[1]), [0,1]) def test_merge_02(self): self.assertEqual(merge([1],[0]), [0,1]) def test_merge_03(self): self.assertEqual(merge([1,2],[0,1]), [0,1,1,2]) def test_merge_04(self): self.assertEqual(merge([0,1],[1,2]), [0,1,1,2]) def test_merge_05(self): self.assertEqual(merge([1,2],[3,4]), [1,2,3,4]) def test_merge_06(self): self.assertEqual(merge([3,4],[1,2]), [1,2,3,4]) def test_merge_07(self): self.assertEqual(merge([1,2,3], [4,5,6]), [1,2,3,4,5,6]) def test_merge_08(self): self.assertEqual(merge([1,2,3], [2,3,4]), [1,2,2,3,3,4]) def test_merge_09(self): self.assertEqual(merge([2,4,6], [1,3,5]), [1,2,3,4,5,6]) def test_merge_10(self): self.assertEqual(merge([1,2],[3,4,5,6]), [1,2,3,4,5,6]) def test_merge_11(self): self.assertEqual(merge([5,6],[1,2,3,4]), [1,2,3,4,5,6]) def test_merge_12(self): self.assertEqual(merge([1,5],[2,3,4]), [1,2,3,4,5]) def test_merge_13(self): self.assertEqual(merge([1,3,5,7,9],[0,2,4,6,8]), [0,1,2,3,4,5,6,7,8,9]) def test_merge_14(self): self.assertEqual(merge([1,2,3,7,8,9],[0,4,5,6]), [0,1,2,3,4,5,6,7,8,9]) def test_merge_15(self): self.assertEqual(merge([-2,-1,4,5],[-5,-3,0,1,2,6,7]), [-5,-3,-2,-1,0,1,2,4,5,6,7]) def test_merge_16(self): self.assertEqual(merge([-9, -7, -4, -2, 2, 3, 5, 5, 6, 7], [-6, -5, -4, 1, 2, 2, 3, 5, 6, 10]), [-9, -7, -6, -5, -4, -4, -2, 1, 2, 2, 2, 3, 3, 5, 5, 5, 6, 6, 7, 10]) def test_merge_17(self): self.assertEqual(merge([-97, -82, -75, -53, -28, -25, -22, -8, 8, 9, 15, 31, 35, 38, 50, 53, 70, 75, 87, 100], [-96, -50, -26, -24, -16, 5, 12, 16, 20, 30, 44, 45, 62, 82, 84]), [-97, -96, -82, -75, -53, -50, -28, -26, -25, -24, -22, -16, -8, 5, 8, 9, 12, 15, 16, 20, 30, 31, 35, 38, 44, 45, 50, 53, 62, 70, 75, 82, 84, 87, 100]) def test_merge_18(self): self.assertEqual(merge([-83, -46, -25, -18, -13, 12, 14, 15, 32, 48, 55, 85, 90, 94, 95], [-99, -79, -76, -73, -68, -56, -48, -22, -3, 16, 21, 23, 35, 37, 44, 53, 59, 60, 71, 81]), [-99, -83, -79, -76, -73, -68, -56, -48, -46, -25, -22, -18, -13, -3, 12, 14, 15, 16, 21, 23, 32, 35, 37, 44, 48, 53, 55, 59, 60, 71, 81, 85, 90, 94, 95]) def test_merge_19(self): self.assertEqual(merge(['a','c','d','h','i','k','l','m','n','p','s','t','v'],['b','e','f','g','j','o','q','r','u','w','x','y','z']), list('abcdefghijklmnopqrstuvwxyz')) # ############################################################################ # # EXTRA CREDIT TESTS # ############################################################################ def test_extra_credit_largest_sum_00(self): self.assertEqual(largest_sum([[0,7,5],[6,-1,4],[-5,5,2]],0,0), 18) def test_extra_credit_largest_sum_01(self): self.assertEqual(largest_sum([[0,7,5],[6,-1,4],[-5,5,2]],1,1), 6) def test_extra_credit_largest_sum_02(self): self.assertEqual(largest_sum([[2,10,10],[1,-100,10],[1,500,10]],0,0), 514) def test_extra_credit_largest_sum_03(self): self.assertEqual(largest_sum([[-30, -24, -67, 25, 20], [8, 49, 33, -96, 56], [20, -83, -33, 99, 3], [44, 35, -31, 35, 70], [62, -94, -59, 57, -76]], 0,0), 231) def test_extra_credit_largest_sum_04(self): self.assertEqual(largest_sum([[-30, -24, -67, 25, 20], [8, 49, 33, -96, 56], [20, -83, -33, 99, 3], [44, 35, -31, 35, 70], [62, -94, -59, 57, -76]], 3,3), 105) ############################################################################# This class digs through AllTests, counts and builds all the tests,# so that we have an entire test suite that can be run as a group.class TheTestSuite (unittest.TestSuite): # constructor. def __init__(self,wants): self.num_req = 0 self.num_ec = 0 # find all methods that begin with "test". fs = [] for w in wants: for func in AllTests.__dict__: # append regular tests # drop any digits from the end of str(func). dropnum = str(func) while dropnum[-1] in "1234567890": dropnum = dropnum[:-1] if dropnum==("test_"+w+"_") and (not (dropnum==("test_extra_credit_"+w+"_"))): fs.append(AllTests(str(func))) if dropnum==("test_extra_credit_"+w+"_") and not BATCH_MODE: fs.append(AllTests(str(func))) # print("TTS ====> ",list(map(lambda f: (f,id(f)),fs))) # call parent class's constructor. unittest.TestSuite.__init__(self,fs)class TheExtraCreditTestSuite (unittest.TestSuite): # constructor. def __init__(self,wants): # find all methods that begin with "test_extra_credit_". fs = [] for w in wants: for func in AllTests.__dict__: if str(func).startswith("test_extra_credit_"+w): fs.append(AllTests(str(func))) # print("TTS ====> ",list(map(lambda f: (f,id(f)),fs))) # call parent class's constructor. unittest.TestSuite.__init__(self,fs)# all (non-directory) file names, regardless of folder depth,# under the given directory 'dir'.def files_list(dir): this_file = __file__ if dir==".": dir = os.getcwd() info = os.walk(dir) filenames = [] for (dirpath,dirnames,filez) in info:# print(dirpath,dirnames,filez) if dirpath==".": continue for file in filez: if file==this_file: continue filenames.append(os.path.join(dirpath,file))# print(dirpath,dirnames,filez,"\n") return filenamesdef main(): if len(sys.argv)<2: raise Exception("needed student's file name as command-line argument:"\ +"\n\t\"python3 testerX.py gmason76_2xx_Px.py\"") if BATCH_MODE: print("BATCH MODE.\n") run_all() return else: want_all = len(sys.argv) <=2 wants = [] # remove batch_mode signifiers from want-candidates. want_candidates = sys.argv[2:] for i in range(len(want_candidates)-1,-1,-1): if want_candidates[i] in ['.'] or os.path.isdir(want_candidates[i]): del want_candidates[i] # set wants and extra_credits to either be the lists of things they want, or all of them when unspecified. wants = [] extra_credits = [] if not want_all: for w in want_candidates: if w in REQUIRED_DEFNS: wants.append(w) elif w in SUB_DEFNS: wants.append(w) elif w in EXTRA_CREDIT_DEFNS: extra_credits.append(w) else: raise Exception("asked to limit testing to unknown function '%s'."%w) else: wants = REQUIRED_DEFNS + SUB_DEFNS extra_credits = EXTRA_CREDIT_DEFNS # now that we have parsed the function names to test, run this one file. run_one(wants,extra_credits) return return # should be unreachable! # only used for non-batch mode, since it does the printing.# it nicely prints less info when no extra credit was attempted.def run_one(wants, extra_credits): has_reqs = len(wants)>0 has_ec = len(extra_credits)>0 # make sure they exist. passed1 = 0 passed2 = 0 tried1 = 0 tried2 = 0 # only run tests if needed. if has_reqs: print("\nRunning required definitions:") (tag, passed1,tried1) = run_file(sys.argv[1],wants,False) if has_ec: print("\nRunning extra credit definitions:") (tag, passed2,tried2) = run_file(sys.argv[1],extra_credits,True) # print output based on what we ran. if has_reqs and not has_ec: print("\n%d/%d Required test cases passed (worth %.2f each)" % (passed1,tried1,weight_required) ) print("\nScore based on test cases: %.2f/%d (%.2f*%d) " % ( passed1*weight_required, total_points_from_tests, passed1, weight_required )) elif has_ec and not has_reqs: print("%d/%d Extra credit test cases passed (worth %d each)" % (passed2, tried2, weight_extra_credit)) else: # has both, we assume. print("\n%d / %d Required test cases passed (worth %.2f each)" % (passed1,tried1,weight_required) ) print("%d / %d Extra credit test cases passed (worth %d each)" % (passed2, tried2, weight_extra_credit)) print("\nScore based on test cases: %.2f / %d ( %.2f * %.2f + %.2f * %.2f) " % ( passed1*weight_required+passed2*weight_extra_credit, total_points_from_tests, passed1, weight_required, passed2, weight_extra_credit )) if CURRENTLY_GRADING: print("( %d %d %d %d )\n%s" % (passed1,tried1,passed2,tried2,tag))# only used for batch mode.def run_all(): filenames = files_list(sys.argv[1]) #print(filenames) wants = REQUIRED_DEFNS + SUB_DEFNS extra_credits = EXTRA_CREDIT_DEFNS results = [] for filename in filenames: print(" Batching on : " +filename) # I'd like to use subprocess here, but I can't get it to give me the output when there's an error code returned... TODO for sure. lines = os.popen("python3 tester1p.py \""+filename+"\"").readlines() # delay of shame... time.sleep(DELAY_OF_SHAME) name = os.path.basename(lines[-1]) stuff =lines[-2].split(" ")[1:-1] print("STUFF: ",stuff, "LINES: ", lines) (passed_req, tried_req, passed_ec, tried_ec) = stuff results.append((lines[-1],int(passed_req), int(tried_req), int(passed_ec), int(tried_ec))) continue print("\n\n\nGRAND RESULTS:\n") for (tag_req, passed_req, tried_req, passed_ec, tried_ec) in results: name = os.path.basename(tag_req).strip() earned = passed_req*weight_required + passed_ec*weight_extra_credit possible = tried_req *weight_required # + tried_ec *weight_extra_credit print("%10s : %3d / %3d = %5.2d %% (%d/%d*%d + %d/%d*%d)" % ( name, earned, possible, (earned/possible)*100, passed_req,tried_req,weight_required, passed_ec,tried_ec,weight_extra_credit ))# only used for batch mode.def run_all_orig(): filenames = files_list(sys.argv[1]) #print(filenames) wants = REQUIRED_DEFNS + SUB_DEFNS extra_credits = EXTRA_CREDIT_DEFNS results = [] for filename in filenames: # wipe out all definitions between users. for fn in REQUIRED_DEFNS+EXTRA_CREDIT_DEFNS : globals()[fn] = decoy(fn) fn = decoy(fn) try: name = os.path.basename(filename) print("\n\n\nRUNNING: "+name) (tag_req, passed_req, tried_req) = run_file(filename,wants,False) (tag_ec, passed_ec, tried_ec ) = run_file(filename,extra_credits,True) results.append((tag_req,passed_req,tried_req,tag_ec,passed_ec,tried_ec)) print(" ###### ", results) except SyntaxError as e: tag = filename+"_SYNTAX_ERROR" results.append((tag,0,len(wants),tag,0,len(extra_credits))) except NameError as e: tag =filename+"_Name_ERROR" results.append((tag,0,len(wants),tag,0,len(extra_credits))) except ValueError as e: tag = filename+"_VALUE_ERROR" results.append((tag,0,len(wants),tag,0,len(extra_credits))) except TypeError as e: tag = filename+"_TYPE_ERROR" results.append((tag,0,len(wants),tag,0,len(extra_credits))) except ImportError as e: tag = filename+"_IMPORT_ERROR_TRY_AGAIN" results.append((tag,0,len(wants),tag,0,len(extra_credits))) except Exception as e: tag = filename+str(e.__reduce__()[0]) results.append((tag,0,len(wants),tag,0,len(extra_credits))) # try:# print("\n |||||||||| scrupe: "+str(scruples))# except Exception as e:# print("NO SCRUPE.",e)# scruples = None print("\n\n\nGRAND RESULTS:\n") for (tag_req, passed_req, tried_req, tag_ec, passed_ec, tried_ec) in results: name = os.path.basename(tag_req) earned = passed_req*weight_required + passed_ec*weight_extra_credit possible = tried_req *weight_required # + tried_ec *weight_extra_credit print("%10s : %3d / %3d = %5.2d %% (%d/%d*%d + %d/%d*%d)" % ( name, earned, possible, (earned/possible)*100, passed_req,tried_req,weight_required, passed_ec,tried_ec,weight_extra_credit ))def try_copy(filename1, filename2, numTries): have_copy = False i = 0 while (not have_copy) and (i < numTries): try: # move the student's code to a valid file. shutil.copy(filename1,filename2) # wait for file I/O to catch up... if(not wait_for_access(filename2, numTries)): return False have_copy = True except PermissionError: print("Trying to copy "+filename1+", may be locked...") i += 1 time.sleep(1) except BaseException as e: print("\n\n\n\n\n\ntry-copy saw: "+e) if(i == numTries): return False return Truedef try_remove(filename, numTries): removed = False i = 0 while os.path.exists(filename) and (not removed) and (i < numTries): try: os.remove(filename) removed = True except OSError: print("Trying to remove "+filename+", may be locked...") i += 1 time.sleep(1) if(i == numTries): return False return Truedef wait_for_access(filename, numTries): i = 0 while (not os.path.exists(filename) or not os.access(filename, os.R_OK)) and i < numTries: print("Waiting for access to "+filename+", may be locked...") time.sleep(1) i += 1 if(i == numTries): return False return True# this will group all the tests together, prepare them as # a test suite, and run them.def run_file(filename,wants=None,checking_ec = False): if wants==None: wants = [] # move the student's code to a valid file. if(not try_copy(filename,"student.py", 5)): print("Failed to copy " + filename + " to student.py.") quit() # import student's code, and *only* copy over the expected functions # for later use. import importlib count = 0 while True: try:# print("\n\n\nbegin attempt:") while True: try: f = open("student.py","a") f.close() break except: pass# print ("\n\nSUCCESS!") import student importlib.reload(student) break except ImportError as e: print("import error getting student... trying again. "+os.getcwd(), os.path.exists("student.py"),e) time.sleep(0.5) while not os.path.exists("student.py"): time.sleep(0.5) count+=1 if count>3: raise ImportError("too many attempts at importing!") except SyntaxError as e: print("SyntaxError in "+filename+":\n"+str(e)) print("Run your file without the tester to see the details") return(filename+"_SYNTAX_ERROR",None, None, None) except NameError as e: print("NameError in "+filename+":\n"+str(e)) print("Run your file without the tester to see the details") return((filename+"_Name_ERROR",0,1)) except ValueError as e: print("ValueError in "+filename+":\n"+str(e)) print("Run your file without the tester to see the details") return(filename+"_VALUE_ERROR",0,1) except TypeError as e: print("TypeError in "+filename+":\n"+str(e)) print("Run your file without the tester to see the details") return(filename+"_TYPE_ERROR",0,1) except ImportError as e: print("ImportError in "+filename+":\n"+str(e)) print("Run your file without the tester to see the details or try again") return((filename+"_IMPORT_ERROR_TRY_AGAIN ",0,1)) except Exception as e: print("Exception in loading"+filename+":\n"+str(e)) print("Run your file without the tester to see the details") return(filename+str(e.__reduce__()[0]),0,1) # make a global for each expected definition. for fn in REQUIRED_DEFNS+EXTRA_CREDIT_DEFNS : globals()[fn] = decoy(fn) try: globals()[fn] = getattr(student,fn) except: if fn in wants: print("\nNO DEFINITION FOR '%s'." % fn) if not checking_ec: # create an object that can run tests. runner = unittest.TextTestRunner() # define the suite of tests that should be run. suite = TheTestSuite(wants) # let the runner run the suite of tests. ans = runner.run(suite) num_errors = len(ans.__dict__['errors']) num_failures = len(ans.__dict__['failures']) num_tests = ans.__dict__['testsRun'] num_passed = num_tests - num_errors - num_failures # print(ans) else: # do the same for the extra credit. runner = unittest.TextTestRunner() suite = TheExtraCreditTestSuite(wants) ans = runner.run(suite) num_errors = len(ans.__dict__['errors']) num_failures = len(ans.__dict__['failures']) num_tests = ans.__dict__['testsRun'] num_passed = num_tests - num_errors - num_failures #print(ans) # remove our temporary file. os.remove("student.py") if os.path.exists("__pycache__"): shutil.rmtree("__pycache__") if(not try_remove("student.py", 5)): print("Failed to remove " + filename + " to student.py.") tag = ".".join(filename.split(".")[:-1]) return (tag, num_passed, num_tests)# make a global for each expected definition.def decoy(name): # this can accept any kind/amount of args, and will print a helpful message. def failyfail(*args, **kwargs): return ("<no '%s' definition was found - missing, or typo perhaps?>" % name) return failyfail# this determines if we were imported (not __main__) or not;# when we are the one file being run, perform the tests! :)if __name__ == "__main__": main() ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download