Skip to content
Snippets Groups Projects
Commit bceb1c3f authored by tuhe's avatar tuhe
Browse files

Bugfixes

parent dc491e87
Branches
No related tags found
No related merge requests found
Showing
with 552 additions and 6 deletions
......@@ -271,12 +271,15 @@ Finally, notice how one of the tests has a return value. This will be automatica
To use `unitgrade` as part of automatic grading, it is recommended you check the students output locally and use hidden tests. Fortunately, this is very easy.
Let's start with the hidden tests. As usual we write a complete report script (`report3_complete.py`), but this time we use the `@hide`-decorator to mark tests as hidden:
```python
# example_docker/instructor/cs103/report3_complete.py
from unitgrade import UTestCase, Report, hide
from unitgrade import UTestCase, Report
from unitgrade.utils import hide
from unitgrade import evaluate_report_student
import cs103
class AutomaticPass(UTestCase):
def test_automatic_pass(self):
self.assertEqual(2, 2) # For simplicity, this test will always pass
......@@ -285,6 +288,7 @@ class AutomaticPass(UTestCase):
def test_hidden_fail(self):
self.assertEqual(2, 3) # For simplicity, this test will always fail.
class Report3(Report):
title = "CS 101 Report 3"
questions = [(AutomaticPass, 10)] # Include a single question for 10 credits.
......@@ -306,12 +310,15 @@ if __name__ == "__main__":
snip_dir("./", student_directory, exclude=['__pycache__', '*.token', 'deploy.py', 'report3_complete*.py', '.*'])
```
Just to check, let's have a quick look at the students report script `report3.py`:
```python
# example_docker/instructor/cs103/report3.py
from unitgrade import UTestCase, Report, hide
from unitgrade import UTestCase, Report
from unitgrade.utils import hide
from unitgrade import evaluate_report_student
import cs103
class AutomaticPass(UTestCase):
def test_automatic_pass(self):
self.assertEqual(2, 2) # For simplicity, this test will always pass
......
This diff is collapsed.
from cs108.report_devel import Report2
from unitgrade_private.hidden_create_files import setup_grade_file_report
from snipper.snip_dir import snip_dir
if __name__ == "__main__":
# import pickle
# with open("unitgrade_data/Week1.pkl", 'rb') as f:
# rs = pickle.load(f)
#
# r = Report2()
# q = r.questions[0][0]()
# for k,v in rs.items():
# print(k, v)
# from unitgrade_private import load_token
# data, txt = load_token("Report2_handin_38_of_38.token")
# print(data['details'][1]['items'] )
setup_grade_file_report(Report2, with_coverage=True)
snip_dir("./", "../../students/cs108", clean_destination_dir=True, exclude=['*.token', 'deploy.py'])
"""
Example student code. This file is automatically generated from the files in the instructor-directory
"""
def reverse_list(mylist):
import numpy as np
def reverse_list(mylist): #!f
"""
Given a list 'mylist' returns a list consisting of the same elements in reverse order. E.g.
reverse_list([1,2,3]) should return [3,2,1] (as a list).
"""
result = []
for l in mylist:
result = result + [l]
return result
z = 23234
return list(reversed(mylist))
def add(a,b):
return a+b
def add(a,b): #!f
""" Given two numbers `a` and `b` this function should simply return their sum:
> add(a,b) = a+b """
# TODO: 1 lines missing.
# raise NotImplementedError("Implement function body")
> add(a,b) = a+b
Hints:
* Remember basic arithmetics!
"""
x = 234
return a+b
def foo(): #!f
""" Comment. """
bar()
def bar(): #!f
return -1
def linear_regression_weights(X, y): #!f
weights = np.linalg.solve(X.T @ X, X.T @ y)
return weights
def linear_predict(X, w): #!f
y = X @ w
return y
if __name__ == "__main__":
# Problem 1: Write a function which add two numbers
print(f"Your result of 2 + 2 = {add(2,2)}")
print(f"Reversing a small list", reverse_list([2,3,5,7]))
This diff is collapsed.
from unitgrade.framework import Report
from unitgrade.evaluate import evaluate_report_student
from cs108.homework1 import add, reverse_list, linear_regression_weights, linear_predict, foo
from unitgrade import UTestCase, cache
import time
import numpy as np
class Numpy(UTestCase):
def test_weights(self):
"""
Hints:
* Try harder!
"""
n = 3
m = 2
np.random.seed(5)
X = np.random.randn(n, m)
y = np.random.randn(n)
foo()
self.assertL2(linear_regression_weights(X, y), msg="the message")
return "THE RESULT OF THE TEST"
import cs108
class Report2(Report):
title = "CS 101 Report 2"
questions = [
(Numpy, 10),
]
pack_imports = [cs108]
if __name__ == "__main__":
evaluate_report_student(Report2())
This diff is collapsed.
File added
File added
File added
File added
# homework1.py
def reverse_list(mylist): #!f
"""
Given a list 'mylist' returns a list consisting of the same elements in reverse order. E.g.
......@@ -11,6 +12,6 @@ def add(a,b): #!f
return a+b
if __name__ == "__main__":
# Problem 1: Write a function which add two numbers
# Example usage:
print(f"Your result of 2 + 2 = {add(2,2)}")
print(f"Reversing a small list", reverse_list([2,3,5,7]))
\ No newline at end of file
# report2.py
self.title = f"Checking if reverse_list({ls}) = {reverse}" # Programmatically set the title
def ex_test_output_capture(self):
with self.capture() as out:
print("hello world 42") # Genereate some output (i.e. in a homework script)
self.assertEqual(out.numbers[0], 42) # out.numbers is a list of all numbers generated
self.assertEqual(out.output, "hello world 42") # you can also access the raw output.
class Numpy(UTestCase):
def test_weights(self):
"""
* Try harder!
"""
n = 3
m = 2
import numpy as np
np.random.seed(3)
X = np.random.randn(n, m)
y = np.random.randn(n)
self.assertL2(linear_regression_weights(X, y))
# assert False
return "THE RESULT OF THE TEST"
class Question2(UTestCase):
@cache
def my_reversal(self, ls):
# The '@cache' decorator ensures the function is not run on the *students* computer
# Instead the code is run on the teachers computer and the result is passed on with the
# other pre-computed results -- i.e. this function will run regardless of how the student happens to have
# implemented reverse_list.
return reverse_list(ls)
def test_reverse_tricky(self):
ls = (2,4,8)
ls2 = self.my_reversal(tuple(ls)) # This will always produce the right result, [8, 4, 2]
print("The correct answer is supposed to be", ls2) # Show students the correct answer
self.assertEqualC(reverse_list(ls)) # This will actually test the students code.
return "Buy world!" # This value will be stored in the .token file
\ No newline at end of file
# report2.py
class Week1Titles(UTestCase):
# @classmethod
# def setUpClass(cls):
# time.sleep(4)
""" The same problem as before with nicer titles """
def test_add(self):
""" Test the addition method add(a,b) """
self.assertEqualC(add(2,2))
# time.sleep(5)
self.assertEqualC(add(-100, 5))
def test_reverse(self):
ls = [1, 2, 3]
reverse = reverse_list(ls)
self.assertEqualC(reverse)
# Although the title is set after the test potentially fails, it will *always* show correctly for the student.
self.title = f"Checking if reverse_list({ls}) = {reverse}" # Programmatically set the title
\ No newline at end of file
# report2.py
class Question2(UTestCase):
@cache
def my_reversal(self, ls):
# The '@cache' decorator ensures the function is not run on the *students* computer
# Instead the code is run on the teachers computer and the result is passed on with the
# other pre-computed results -- i.e. this function will run regardless of how the student happens to have
# implemented reverse_list.
return reverse_list(ls)
def test_reverse_tricky(self):
ls = (2,4,8)
ls2 = self.my_reversal(tuple(ls)) # This will always produce the right result, [8, 4, 2]
print("The correct answer is supposed to be", ls2) # Show students the correct answer
self.assertEqualC(reverse_list(ls)) # This will actually test the students code.
return "Buy world!" # This value will be stored in the .token file
\ No newline at end of file
# report_devel.py
self.title = f"Checking if reverse_list({ls}) = {reverse}" # Programmatically set the title
def ex_test_output_capture(self):
with self.capture() as out:
print("hello world 42") # Genereate some output (i.e. in a homework script)
self.assertEqual(out.numbers[0], 42) # out.numbers is a list of all numbers generated
self.assertEqual(out.output, "hello world 42") # you can also access the raw output.
class Question2(UTestCase):
@cache
def my_reversal(self, ls):
# The '@cache' decorator ensures the function is not run on the *students* computer
# Instead the code is run on the teachers computer and the result is passed on with the
# other pre-computed results -- i.e. this function will run regardless of how the student happens to have
# implemented reverse_list.
return reverse_list(ls)
def test_reverse_tricky(self):
ls = (2,4,8)
ls2 = self.my_reversal(tuple(ls)) # This will always produce the right result, [8, 4, 2]
print("The correct answer is supposed to be", ls2) # Show students the correct answer
self.assertEqualC(reverse_list(ls)) # This will actually test the students code.
return "Buy world!" # This value will be stored in the .token file
\ No newline at end of file
import numpy as np
def reverse_list(mylist):
"""
Given a list 'mylist' returns a list consisting of the same elements in reverse order. E.g.
reverse_list([1,2,3]) should return [3,2,1] (as a list).
"""
# TODO: 1 lines missing.
raise NotImplementedError("Implement function body")
return list(reversed(mylist))
def add(a,b):
""" Given two numbers `a` and `b` this function should simply return their sum:
> add(a,b) = a+b
Hints:
* Remember basic arithmetics!
"""
# TODO: 1 lines missing.
raise NotImplementedError("Implement function body")
return a+b
def foo():
""" Comment. """
# TODO: 1 lines missing.
raise NotImplementedError("Implement function body")
def bar():
# TODO: 1 lines missing.
raise NotImplementedError("Implement function body")
def linear_regression_weights(X, y):
# TODO: 1 lines missing.
raise NotImplementedError("Implement function body")
return weights
def linear_predict(X, w):
# TODO: 1 lines missing.
raise NotImplementedError("Implement function body")
return y
if __name__ == "__main__":
print(f"Your result of 2 + 2 = {add(2,2)}")
print(f"Reversing a small list", reverse_list([2,3,5,7]))
This diff is collapsed.
from unitgrade.framework import Report
from unitgrade.evaluate import evaluate_report_student
from cs108.homework1 import add, reverse_list, linear_regression_weights, linear_predict, foo
from unitgrade import UTestCase, cache
import time
import numpy as np
class Numpy(UTestCase):
def test_weights(self):
"""
Hints:
* Try harder!
"""
n = 3
m = 2
np.random.seed(5)
X = np.random.randn(n, m)
y = np.random.randn(n)
foo()
self.assertL2(linear_regression_weights(X, y), msg="the message")
return "THE RESULT OF THE TEST"
import cs108
class Report2(Report):
title = "CS 101 Report 2"
questions = [
(Numpy, 10),
]
pack_imports = [cs108]
if __name__ == "__main__":
evaluate_report_student(Report2())
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment