Advanced Python Development Course
Chapter
>
Level
Exception Handling
Customizing Exception Classes
Objective
Verify collected rain water and ensure it’s being processed and transferred correctly by customizing exception classes.
At the far end of the roof complex there is a cistern of collected rainwater, this water is filtered and transferred throughout the building for various purposes. Some valves on the upper wall control the water pressure flowing, meanwhile the last storeroom houses various water heaters. Operating machinery can be tricky however their operation can be streamlined effectively by outlining error codes and setting up effective countermeasures. This is achieved by customizing exception classes that you create for your custom exceptions.
There are two custom exception classes set up in the code editor, your ability to use them will be essential to troubleshooting any machinery issues you encounter. The first is an exception to troubleshoot water pressure. It’s meant to be used with the valves to ensure water is flowing properly into the complex or if the pressure needs to be adjusted. Depending if the pressure is is below 100 or over 300 a different action is taken by the exception, the pressure is an argument that the user must provide. Once triggered a custom pop up holding an error code and message is displayed.
class PressureError(Exception):
def __init__(self, pressure, message="Water pressure is offset, pressure is: ", error_code=404):
self.pressure = pressure
self.message = message
self.error_code = error_code
super().__init__(self.message)
async def __str__(self):
await player.write("[Error: %s {%s %s}]" % (self.error_code , self.message, self.pressure))
if self.pressure < 100:
return ("Valve has been closed")
if self.pressure > 360:
return ("Valve has been opened")
The second custom exception class is meant to modulate temperature in the water heaters, if the temperature is too low or too high you must enter a new temperature as an argument in order to correct the error once it occurs.
class TemperatureError(Exception):
def __init__(self, temp, message="Temperature is not appropiate, enter new temperature: ", error_code=808):
self.temp = temp
self.message = message
self.error_code = error_code
super().__init__(self.message)
async def __str__(self):
await player.write("[Error: %s {%s %s}]" % (self.error_code , self.message, self.temp))
return ("Temperature Corrected")
To start off, walk to the light X mark over the green carpet and face the tank with a valve, there are four (4) of these regulating the pressure of the water being pumped from the cistern. There are four (4) variable constants: green_pressure , red_pressure , blue_pressure and orange_pressure , these hold the pressure of each of the tanks of the corresponding colored carpets.
On all of the light X marks, use the try /except and else code block sequences in order to verify if the pressure on each tank is appropriate. If the pressure is below 100 or if it’s above 300 raise the custom PressureError exception and add the pressure as an argument. If activated it will raise the error code and issue a correction, otherwise the else clause is triggered confirming the pressure in the tank is appropriate. For example:
try: if green_pressure < 100 or green_pressure > 360: raise PressureError(green_pressure) except PressureError as x: player.speak(x) else: player.speak( "Pressure is appropriate" )
Do this on all light X marks, using the variable constants that correspond to each of the colored carpets and either verify or adjust the pressure valves as needed.
Once done, walk to the gold X mark and use the open() function to access the storeroom with the water heater, once inside, walk to the gold X mark that’s inside the storeroom and use the read() function to read the memo on the counter. This memo will provide you with information on what the ideal temperature for each of the water heaters is.
Once you have the information on each of the temperatures walk over to the dark X mark over the purple carpet, here we will use the try/except and else code sequence to verify the the water heaters are the correct temperature. There are four (4) more variable constants associated with the colored carpets: purple_temperature , yellow_temperature , pink_temperature and white_temperature.
In the sequence we will verify if the temperature is below 100, if it is we will raise the TemperatureError exception. As a customized exception, it takes an argument, in this case you must enter the temperature read in the memo in order to properly execute the exceptions countermeasure to correct the issue. For example:
try: if purple_temperature < 100: raise TemperatureError(120) except TemperatureError as x: player.speak(x) else: player.speak( "Temperature is ok" )
Do this for all water heaters, on each X mark while facing the heaters, execute the sequence using the variable constants corresponding to the correct colored carpets in order to complete the level.