This notebook is developed as part of the KIPAC/StatisticalMethods course, (c) 2019 Adam Mantz, licensed under the GPLv2.
Tutorial notebooks from KIPAC/StatisticalMethods will all start with these definitions:
In [ ]:
class SolutionMissingError(Exception):
def __init__(self):
Exception.__init__(self,"You need to complete the solution for this code to work!")
def REPLACE_WITH_YOUR_SOLUTION():
raise SolutionMissingError
REMOVE_THIS_LINE = REPLACE_WITH_YOUR_SOLUTION
You'll then see cells that look something like this:
In [ ]:
# Set x equal to something
try:
exec(open('solutions/setx.py').read())
except IOError:
x = REPLACE_WITH_YOUR_SOLUTION()
Go ahead and try to run it. You'll get an error traceback, the end of which points out that you've neglected to provide a solution to the posed problem. This is our preferred method of providing incomplete code, since an alternative like
x = # set x equal to something
will throw a different and less informative error if you accidentally run the cell before completing it.
The try-except wrapper is there so that we, the developers, can easily verify that the entire notebook runs if provided with a correct solution. There is no need for you to write solutions for each cell in separate files, and doing so will just make this notebook harder for you to use later. Instead, we suggest removing the try-except construction entirely, so your completed notebook cell would look like
In [ ]:
# Set x equal to something
x = 5.0
You'll also see cells in this format:
In [ ]:
# Define a function that does stuff
try:
exec(open('solutions/func.py').read())
except IOError:
REMOVE_THIS_LINE()
def myfunc(a, b):
c = REPLACE_WITH_YOUR_SOLUTION()
return REPLACE_WITH_YOUR_SOLUTION()
The idea is exactly the same, except that the calls to REPLACE_WITH_YOUR_SOLUTION within the function skeleton wouldn't throw an exception when the cell is run, since the function isn't called at that time. Hence the inclusion of REMOVE_THIS_LINE(), which ensures that an error is thrown immediately.
Hopefully all this is easy enough to deal with. Although it seems like extra work, this approach actually saves time, since trying to maintain separate problem and solution notebooks inevitably results in bugs creeping in. If in doubt, just DO_WHAT_THE_CAPITAL_LETTERS_SAY.