Project 5 · Practice
Quiz & exercises
Answer each question to see the explanation. Your best score is saved in this browser.
Quiz
Coding exercises
Write your code, then press Check (or Shift+Enter) to run the tests. Drafts are saved as you type.
Exercise 1: Goal 1: one member per error ✓ Passed
Complete the AppException enumeration so that each member stores three things from its tuple:
- its code, which must be the member's value (so AppException(201) returns AppException.NotAList), also available as a read-only code property, - its exception class, as the attribute exception, - its default message, as the attribute message.
Lookup by name (AppException['Timeout']) must keep working, and there must be exactly four members.
from enum import Enum
class GenericException(Exception):
pass
class Timeout(Exception):
pass
class AppException(Enum):
Generic = (100, GenericException, 'Application exception.')
Timeout = (101, Timeout, 'Timeout connecting to resource.')
NotAnInteger = (200, ValueError, 'Value must be an integer.')
NotAList = (201, ValueError, 'Value must be a list.')
# your code here
def test_value_is_code():
"""the value of each member is its code"""
values = [m.value for m in AppException]
assert values == [100, 101, 200, 201], f"got {values}"
def test_lookup_by_code():
"""AppException(code) finds the member"""
assert AppException(201) is AppException.NotAList
assert AppException(101) is AppException.Timeout
def test_lookup_by_name():
"""AppException[name] finds the member"""
assert AppException['NotAnInteger'] is AppException.NotAnInteger
def test_code_property():
"""code property matches the value"""
assert AppException.Generic.code == 100, f"got {AppException.Generic.code!r}"
def test_exception_and_message():
"""exception class and message are stored"""
assert AppException.Timeout.exception is Timeout
assert AppException.NotAList.exception is ValueError
assert AppException.Generic.message == 'Application exception.'
assert AppException.NotAnInteger.message == 'Value must be an integer.'
def test_four_members():
"""exactly four members, no extra ones"""
assert len(AppException) == 4, f"got {list(AppException)}"
Hints (3)
- Define
__new__(cls, ex_code, ex_class, ex_message): each member's tuple is unpacked into these arguments. - Create the member with
object.__new__(cls), setmember._value_ = ex_code, attach the other two attributes and return it. - A
@propertynamedcodecan simply returnself.value.
Show a solution
from enum import Enum
class GenericException(Exception):
pass
class Timeout(Exception):
pass
class AppException(Enum):
Generic = (100, GenericException, 'Application exception.')
Timeout = (101, Timeout, 'Timeout connecting to resource.')
NotAnInteger = (200, ValueError, 'Value must be an integer.')
NotAList = (201, ValueError, 'Value must be a list.')
def __new__(cls, ex_code, ex_class, ex_message):
member = object.__new__(cls)
member._value_ = ex_code
member.exception = ex_class
member.message = ex_message
return member
@property
def code(self):
return self.valueExercise 2: Goal 2: raise with one call ✓ Passed
Add a throw(message=None) method to AppException. It must raise the member's exception class with the message '<code> - <message>', using the member's default message when no message is given.
For example, AppException.Timeout.throw() raises Timeout('101 - Timeout connecting to resource.'), and AppException.Timeout.throw('Database timeout.') raises Timeout('101 - Database timeout.').
Use the default only when message is None: an explicitly passed empty string is kept as is.
from enum import Enum
class GenericException(Exception):
pass
class Timeout(Exception):
pass
class AppException(Enum):
Generic = (100, GenericException, 'Application exception.')
Timeout = (101, Timeout, 'Timeout connecting to resource.')
NotAnInteger = (200, ValueError, 'Value must be an integer.')
NotAList = (201, ValueError, 'Value must be a list.')
def __new__(cls, ex_code, ex_class, ex_message):
member = object.__new__(cls)
member._value_ = ex_code
member.exception = ex_class
member.message = ex_message
return member
@property
def code(self):
return self.value
def throw(self, message=None):
# your code here
raise Exception(self.message)
def _raised(member, *args):
try:
member.throw(*args)
except Exception as ex:
return ex
raise AssertionError(f"{member.name}.throw() did not raise")
def test_exception_type():
"""raises the member's exception class"""
assert type(_raised(AppException.Timeout)) is Timeout
assert type(_raised(AppException.NotAList)) is ValueError
assert type(_raised(AppException.Generic)) is GenericException
def test_default_message():
"""default message is '<code> - <message>'"""
ex = _raised(AppException.Timeout)
assert str(ex) == '101 - Timeout connecting to resource.', f"got {str(ex)!r}"
ex = _raised(AppException.NotAnInteger)
assert str(ex) == '200 - Value must be an integer.', f"got {str(ex)!r}"
def test_custom_message():
"""a custom message replaces the default"""
ex = _raised(AppException.Timeout, 'Database timeout.')
assert str(ex) == '101 - Database timeout.', f"got {str(ex)!r}"
def test_empty_message_kept():
"""an explicit empty message is not replaced by the default"""
ex = _raised(AppException.NotAList, '')
assert str(ex) == '201 - ', f"got {str(ex)!r}"
def test_catchable():
"""callers can catch it with a normal except clause"""
try:
AppException.NotAList.throw()
except ValueError:
pass
Hints (3)
self.exceptionis a class: calling it,self.exception('text'), creates an exception instance you canraise.- Build the text with an f-string:
f'{self.code} - {message}'. message or self.messagewould also replace''. Testmessage is Noneinstead.
Show a solution
from enum import Enum
class GenericException(Exception):
pass
class Timeout(Exception):
pass
class AppException(Enum):
Generic = (100, GenericException, 'Application exception.')
Timeout = (101, Timeout, 'Timeout connecting to resource.')
NotAnInteger = (200, ValueError, 'Value must be an integer.')
NotAList = (201, ValueError, 'Value must be a list.')
def __new__(cls, ex_code, ex_class, ex_message):
member = object.__new__(cls)
member._value_ = ex_code
member.exception = ex_class
member.message = ex_message
return member
@property
def code(self):
return self.value
def throw(self, message=None):
if message is None:
message = self.message
raise self.exception(f'{self.code} - {message}')Exercise 3: Goal 3: list the errors ✓ Passed
Add two class methods to AppException:
- catalog() returns a list of (name, code, message) tuples for every member, in definition order. - raising(exc_class) returns a list of the members whose exception class is exc_class or a subclass of it, in definition order.
Adding these methods must not add members to the enumeration.
from enum import Enum
class GenericException(Exception):
pass
class Timeout(Exception):
pass
class AppException(Enum):
Generic = (100, GenericException, 'Application exception.')
Timeout = (101, Timeout, 'Timeout connecting to resource.')
NotAnInteger = (200, ValueError, 'Value must be an integer.')
NotAList = (201, ValueError, 'Value must be a list.')
def __new__(cls, ex_code, ex_class, ex_message):
member = object.__new__(cls)
member._value_ = ex_code
member.exception = ex_class
member.message = ex_message
return member
@property
def code(self):
return self.value
@classmethod
def catalog(cls):
# your code here
return []
@classmethod
def raising(cls, exc_class):
# your code here
return []
def test_catalog():
"""catalog lists (name, code, message) in definition order"""
expected = [
('Generic', 100, 'Application exception.'),
('Timeout', 101, 'Timeout connecting to resource.'),
('NotAnInteger', 200, 'Value must be an integer.'),
('NotAList', 201, 'Value must be a list.'),
]
assert AppException.catalog() == expected, f"got {AppException.catalog()}"
def test_raising_exact():
"""raising(ValueError) finds both ValueError members"""
result = AppException.raising(ValueError)
assert result == [AppException.NotAnInteger, AppException.NotAList], f"got {result}"
def test_raising_custom():
"""raising(Timeout) finds only the Timeout member"""
assert AppException.raising(Timeout) == [AppException.Timeout]
def test_raising_base_class():
"""raising(Exception) includes subclasses, so every member matches"""
assert AppException.raising(Exception) == list(AppException)
def test_raising_none():
"""an unused exception class gives an empty list"""
assert AppException.raising(KeyError) == []
def test_still_four_members():
"""class methods are not members"""
assert len(AppException) == 4
Hints (2)
- Inside a class method,
clsis the enumeration, and iterating it yields the members in definition order. issubclass(ex.exception, exc_class)isTruefor the class itself and for its subclasses.
Show a solution
from enum import Enum
class GenericException(Exception):
pass
class Timeout(Exception):
pass
class AppException(Enum):
Generic = (100, GenericException, 'Application exception.')
Timeout = (101, Timeout, 'Timeout connecting to resource.')
NotAnInteger = (200, ValueError, 'Value must be an integer.')
NotAList = (201, ValueError, 'Value must be a list.')
def __new__(cls, ex_code, ex_class, ex_message):
member = object.__new__(cls)
member._value_ = ex_code
member.exception = ex_class
member.message = ex_message
return member
@property
def code(self):
return self.value
@classmethod
def catalog(cls):
return [(ex.name, ex.code, ex.message) for ex in cls]
@classmethod
def raising(cls, exc_class):
return [ex for ex in cls if issubclass(ex.exception, exc_class)]