There are two parts to this exercise. For each part, there is a single file you must submit — make sure that you read the submission instructions carefully!
For all of the exercises from now on, the first test will will run will be a style checker. You can run the style checker yourself:
>>> import pep8 >>> pep8.Checker('e2a.py', ignore=('W2','W3')).check_all()
BEFORE you begin coding, make sure that you understand everything in this handout. Remember that the point of these exercises is not to get a "working" program by any means necessary — it is to ensure that you understand how to write the desired code. If you succeed in writing something that works but you do not understand why, then you have failed, no matter what grade you receive!
In particular, if there is something you do not quite understand (for example, what we mean by "a function object" in Part B below), please, for your own sake, take the time to read the online textbook and the course notes and readings, do some searching on Google and ask on the course forum or during office hours to make sure you find out.
Submit a file called "e2a.py
" that defines the following
functions and other features.
E2Error
: an exception class that is a subclass of
Exception
.
E2OddError
: an exception class that is a subclass of
E2Error
.
raiser
, a function that takes one parameter x
,
with the following behaviour:
x == 'Go sue me!'
, then raiser(x)
raises an E2Error
with the exception message
"Big Apple
".
x
is not 'Go sue me!'
, but
x
is a string not convertible to an
int
(by calling int(x)
), then
raiser(x)
raises a ValueError
, without any
requirement on the exception message (this is extremely easy to do;
it does not even require you to use the raise
keyword
explicitly).
x
converts to an even int
rather than an
odd one, then raiser(x)
raises an
E2OddError
, without any requirement on the
exception message.
raiser(x)
does nothing (no
return
, no print
, nothing at all).
Submit a file called "e2b.py
" that imports the names defined
in the module e2a
and then defines a function
"reporter
" that takes two parameters, a function f
and a value x
. Note: The
parameter f
refers to a
function object, not the result of a function call. In
other words, when you call reporter
, you must give it the
name of a function as its first parameter.
Here is what reporter(f, x)
does:
f(x)
raises a ValueError
, then
reporter(f, x)
returns the string
"Value
".
f(x)
raises an E2OddError
, then
reporter(f, x)
returns the string
"E2Even
".
f(x)
raises a more general E2Error
,
then reporter(f, x)
returns the string
"E2
".
f(x)
raises an Exception
that is not one of
the more specific ones above, then reporter(f, x)
returns the string "E
".
f(x)
terminates normally — that is,
without raising an exception — then
reporter(f, x)
returns the string "okay
".
Your function reporter
must always return normally, so it
must catch all exceptions raised while it is executing and must never raise
an exception itself. Note: reporter
never
print
s anything! Also, pay attention to return strings
exactly as specified above — including
the capitalization, spaces (or lack thereof), etc.
You should import names from e2a
using the form "from
e2a import ...
". You do not really need to import
raiser
, but you may find it helpful to have it available while
you are debugging your e2b
module.