def mark_work(dirList, week_number, root_dir, dfPlease=True): """Mark the week's exercises.""" results = [] for student_repo in dirList: try: with Timeout(15): # should catch any rogue ∞ loops subprocess.call(['python', './test_shim.py', "week{}.tests".format(week_number), "{}/{}".format(root_dir, student_repo)]) temp_results = open(os.path.join(LOCAL, 'temp_results.json'), 'r') results_dict = json.loads(temp_results.read()) results_dict["bigerror"] = ":)" results.append(results_dict) temp_results.close() except Exception as e: print("\n\nFAARK!", student_repo, e, "\n\n") results.append({"bigerror": str(e).replace(",", "~"), "name": student_repo}) # the comma messes with the csv resultsDF = pd.DataFrame(results) print("\n\nResults:\n", resultsDF) resultsDF.to_csv(os.path.join(CWD, "csv/week{}marks.csv".format(week_number)), index=False) print("\n+-+-+-+-+-+-+-+") if dfPlease: return resultsDF
def test_advanced_guessingGame(exercise3, mockInputs): """Test the advanced_guessingGame function.""" try: with Timeout(3): with mock.patch('builtins.input', side_effect=mockInputs): return exercise3.advancedGuessingGame() == "You got it!" except Exception as e: print("exception:", e)
def test_advanced_guessingGame(path, mockInputs): """Test the advanced_guessingGame function.""" try: path = "{}/week{}/exercise3.py".format(path, WEEK_NUMBER) exercise3 = imp.load_source("exercise3", path) except Exception as e: return syntax_error_message(3, e) try: with Timeout(3): with mock.patch('__builtin__.raw_input', side_effect=mockInputs): return exercise3.advancedGuessingGame() == "You got it!" except Exception as e: print("exception:", e)
def test_advanced_guessingGame(path, mockInputs): """Test the advanced_guessingGame function.""" try: path = "exercise3.py" exercise3 = imp.load_source("exercise3", path) except Exception as e: return syntax_error_message(3, e) try: with Timeout(1000): with mock.patch('builtins.input', side_effect=mockInputs): return exercise3.advancedGuessingGame() == "You got it!" except Exception as e: print("exception:", e)
def test_not_number_rejector(path): """Test the not number rejector function.""" try: path = "{}/week{}/exercise1.py".format(path, WEEK_NUMBER) exercise1 = imp.load_source("exercise1", path) except Exception as e: return syntax_error_message(1, e) mockInputs = ["aword", [1, 2, 3], {"an": "object"}, 40] try: with Timeout(3): with mock.patch('__builtin__.raw_input', side_effect=mockInputs): return exercise1.not_number_rejector("Testing some values:") except Exception as e: print("exception:", e)
def test_stubborn_asker(path, low, high): """Test the stubborn asker function.""" try: path = "{}/week{}/exercise1.py".format(path, WEEK_NUMBER) exercise1 = imp.load_source("exercise1", path) except Exception as e: return syntax_error_message(4, e) mockInputs = range(low - 25, high + 20, 5) try: with Timeout(3): with mock.patch('__builtin__.raw_input', side_effect=mockInputs): return low <= exercise1.stubborn_asker(low, high) <= high except Exception as e: print("exception:", e)
def test_stubborn_asker(path, low, high): """Test the stubborn asker function.""" try: path = "exercise1.py" exercise1 = imp.load_source("exercise1", path) except Exception as e: return syntax_error_message(4, e) mockInputs = list(range(low - 25, high + 20, 5)) try: with Timeout(3): with mock.patch('builtins.input', side_effect=mockInputs): return low <= exercise1.stubborn_asker(low, high) <= high except Exception as e: print("exception:", e)
def test_super_asker(path, low, high): """Test the super asker function.""" try: path = "{}/week{}/exercise1.py".format(path, WEEK_NUMBER) exercise1 = imp.load_source("exercise1", path) except Exception as e: return syntax_error_message(1, e) dirty_things = ["aword", [1, 2, 3], {"an": "object"}] neat_range = range(low - 25, high + 20, 5) mockInputs = dirty_things + neat_range try: with Timeout(3): with mock.patch('__builtin__.raw_input', side_effect=mockInputs): return exercise1.super_asker(low, high) except Exception as e: print("exception:", e)
def test_example_guessingGame(path): """Test the example_guessingGame function. This should always pass becasue it's provided code """ try: path = "{}/week{}/exercise2.py".format(path, WEEK_NUMBER) exercise2 = imp.load_source("exercise2", path) except Exception as e: return syntax_error_message(2, e) upperBound = 5 guesses = range(5 + 1) mockInputs = [upperBound] + guesses try: with Timeout(3): with mock.patch('__builtin__.raw_input', side_effect=mockInputs): return exercise2.exampleGuessingGame() == "You got it!" except Exception as e: print("exception:", e)
def test_binary_search(path, low, high, actual): """Test the binary search function. checks to see that it's searching better than O(log n) """ try: path = "{}/week{}/exercise4.py".format(path, WEEK_NUMBER) exercise4 = imp.load_source("exercise4", path) BASE2 = 2 b = None with Timeout(3): b = exercise4.binary_search(low, high, actual) b["WorstCaseO"] = math.log(high - low, BASE2) print("b", b) if b is not None: print("snuck it in") return b["tries"] < b["WorstCaseO"] else: return False except Exception as e: return syntax_error_message(4, e)
print("exception:", e) def test_not_number_rejector(path): """Test the not number rejector function.""" try: path = "exercise1.py" exercise1 = imp.load_source("exercise1", path) except Exception as e: return syntax_error_message(1, e) mockInputs = ["aword", [1, 2, 3], {"an": "object"}, 40] try: <<<<<<< HEAD #with Timeout(3): ======= with Timeout(3): >>>>>>> a0fec480311b6bb045bdb79ae1c35177118a8ce3 with mock.patch('builtins.input', side_effect=mockInputs): return exercise1.not_number_rejector("Testing some values:") except Exception as e: print("exception:", e) def test_super_asker(path, low, high): """Test the super asker function.""" try: path = "exercise1.py" exercise1 = imp.load_source("exercise1", path) except Exception as e: return syntax_error_message(1, e)
def theTests(path_to_code_to_check="."): """Run the tests.""" print("\nWelcome to week {}!".format(WEEK_NUMBER)) print("May the odds be ever in your favour.\n") # Give each person 10 seconds to complete all tests. with Timeout(10): path = "{}/week{}/exercise1.py".format(path_to_code_to_check, WEEK_NUMBER) print(path) exercise1 = imp.load_source("exercise1", path) testResults = [] # stack the tests here testResults.append( test(test_flake8(path), "Exercise 1: pass the linter")) message = '{"message": "Python and requests are working!"}' testResults.append( test(exercise1.success_is_relative() == message, "Exercise 1: read a file using a relative path")) testDict = { 'lastName': u'hoogmoed', 'password': u'jokers', 'postcodePlusID': 4311240 } testResults.append( test(exercise1.get_some_details() == testDict, "Exercise 1: get some data out of a JSON file")) lengths = [ 3, 5, 7, 9, 11, 13, 15, 17, 19, 20, 18, 16, 14, 12, 10, 8, 6, 4 ] try: testResults.append( test([len(w) for w in exercise1.wordy_pyramid()] == lengths, "Exercise 1: request some simple data from the internet")) except Exception as e: testResults.append(0) print("Exercise 1: request some simple data from the internet", e) weather_results = { 'latitude': u'-33.924206', 'state': u'NSW', 'longitude': u'151.187912', 'local_tz_offset': u'+1100' } try: testResults.append( test( process_wunderground(exercise1.wunderground()) == process_wunderground(weather_results), "Exercise 1: get some data from " + "the weather underground.")) except Exception as e: testResults.append(0) print("Exercise 1: get some data from the weather underground.", e) testResults.append( test(find_lasers(path_to_code_to_check), "Exercise 1: count the lasers.")) print("{0}/{1} (passed/attempted)".format(sum(testResults), len(testResults))) if sum(testResults) == len(testResults): nyan_cat() message = "Rad, you've got all the tests passing!" completion_message(message, len(message) + 2) treat(path_to_code_to_check) return { "of_total": len(testResults), "mark": sum(testResults), "results": testResults }
def theTests(path_to_code_to_check="."): """Run all the tests.""" print("\nWelcome to the exam!") print("May the odds be ever in your favour.\nEspecially today!") testResults = [] ex1path = "{}/week{}/exercise1.py".format(path_to_code_to_check, WEEK_NUMBER) if ex_runs(path_to_code_to_check, exNumber=1, weekNumber=WEEK_NUMBER): exam = imp.load_source("exercise1", ex1path) testResults.append( test(test_flake8(ex1path), "Pass the linter")) testResults.append( test(exam.greet("the Queen") == "Hello the Queen", "greet the Queen")) testResults.append( test(exam.greet("Pr♂nc♀♂") == "Hello Pr♂nc♀♂", "greet Pr♂nc♀♂")) testResults.append( test(exam.three_counter([3, 3, 3, 3, 1]) == 4, "three_counter [3, 3, 3, 3, 1]")) testResults.append( test(exam.three_counter([0, 1, 2, 5, -9]) == 0, "three_counter [0, 1, 2, 5, -9]")) fizza = [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz', 'Fizz', 22, 23, 'Fizz', 'Buzz', 26, 'Fizz', 28, 29, 'FizzBuzz', 31, 32, 'Fizz', 34, 'Buzz', 'Fizz', 37, 38, 'Fizz', 'Buzz', 41, 'Fizz', 43, 44, 'FizzBuzz', 46, 47, 'Fizz', 49, 'Buzz', 'Fizz', 52, 53, 'Fizz', 'Buzz', 56, 'Fizz', 58, 59, 'FizzBuzz', 61, 62, 'Fizz', 64, 'Buzz', 'Fizz', 67, 68, 'Fizz', 'Buzz', 71, 'Fizz', 73, 74, 'FizzBuzz', 76, 77, 'Fizz', 79, 'Buzz', 'Fizz', 82, 83, 'Fizz', 'Buzz', 86, 'Fizz', 88, 89, 'FizzBuzz', 91, 92, 'Fizz', 94, 'Buzz', 'Fizz', 97, 98, 'Fizz', 'Buzz'] testResults.append( test(exam.fizz_buzz() == fizza, "fizz_buzz")) a = exam.put_behind_bars("a serial killer") testResults.append( test(a == "|a| |s|e|r|i|a|l| |k|i|l|l|e|r|", "put_behind_bars")) a = exam.put_behind_bars("a bartender") testResults.append( test(a == "|a| |b|a|r|t|e|n|d|e|r|", "put_behind_bars")) testResults.append( test(exam.pet_filter("x") == ['red fox'], "pet_filter")) testResults.append( test(exam.pet_filter("q") == [], "pet_filter")) testResults.append( test(exam.pet_filter("p") == ['pig', 'sheep', 'guinea pig', 'pigeon', 'alpaca', 'guppy'], "pet_filter")) testResults.append( test(exam.best_letter_for_pets() == "e", "best_letter_for_pets")) testResults.append( test(len(str(exam.make_filler_text_dictionary())) == 175, "make_filler_text_dictionary")) filler1 = exam.random_filler_text(50) testResults.append( test(len(filler1.split(" ")) == 50 and len(filler1) > 3*50, "random_filler_text")) filler2 = exam.fast_filler(1000) testResults.append( test(len(filler2.split(" ")) == 1000 and len(filler2) > 3*1000, "first fast_filler")) testResults.append( test(exam.fast_filler(10)[0] in string.uppercase and exam.fast_filler(10)[1] in string.lowercase, "first fast_filler has a starting capital")) testResults.append( test(exam.fast_filler(10)[-1] == ".", "first fast_filler ends with a .")) try: with Timeout(1): for _ in range(10): exam.fast_filler(1000) testResults.append( test(True, "subsiquent fast_filler")) except Exception as e: print(e) testResults.append( test(False, "subsiquent fast_filler wasn't fast enough")) print("{0}/{1} (passed/attempted)".format(sum(testResults), len(testResults))) if sum(testResults) == len(testResults): print(nyan_cat()) message = "Cowabunga! You've got all the tests passing!" completion_message(message, len(message) + 2) return {"of_total": len(testResults), "mark": sum(testResults), "results": testResults}