From eab1167091adb070e76631c67470ed9afe8e68fc Mon Sep 17 00:00:00 2001 From: David GAILLETON Date: Mon, 2 Feb 2026 14:41:10 +0100 Subject: [PATCH] FIX: Make 02 flake8 complient --- 02/ex0/ft_first_exception.py | 1 + 02/ex2/ft_custom_errors.py | 6 +++--- 02/ex3/ft_finally_block.py | 4 ++-- 02/ex4/ft_raise_errors.py | 15 ++++++++++----- 02/ex5/ft_garden_management.py | 31 +++++++++++++++++++------------ 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/02/ex0/ft_first_exception.py b/02/ex0/ft_first_exception.py index 32b43f2..41bb7a9 100644 --- a/02/ex0/ft_first_exception.py +++ b/02/ex0/ft_first_exception.py @@ -15,6 +15,7 @@ def check_temperature(temp_str: str) -> int | None: print("Error: " + temp_str + "°C", ex) return (None) + if __name__ == "__main__": print("=== Garden Temperature checker ===") print("\nTesting temperature: 25") diff --git a/02/ex2/ft_custom_errors.py b/02/ex2/ft_custom_errors.py index a06b9ae..68ea997 100644 --- a/02/ex2/ft_custom_errors.py +++ b/02/ex2/ft_custom_errors.py @@ -8,7 +8,7 @@ class GardenError(Exception): @override def __str__(self) -> str: - return f"Caught a garden error: {self.message}" + return f"Caught a garden error: {self.message}" class PlantError(GardenError): @@ -17,7 +17,7 @@ class PlantError(GardenError): @override def __str__(self) -> str: - return f"Caught PlantError: {self.message}" + return f"Caught PlantError: {self.message}" class WaterError(GardenError): @@ -26,7 +26,7 @@ class WaterError(GardenError): @override def __str__(self) -> str: - return f"Caught WaterError: {self.message}" + return f"Caught WaterError: {self.message}" if __name__ == "__main__": diff --git a/02/ex3/ft_finally_block.py b/02/ex3/ft_finally_block.py index bf6e4f0..90e5499 100644 --- a/02/ex3/ft_finally_block.py +++ b/02/ex3/ft_finally_block.py @@ -1,7 +1,7 @@ def water_plants(plant_list: list[str | None]) -> None: """Display watering plant if plant is a string""" for plant in plant_list: - if plant == None: + if not plant: raise ValueError print("Watering", plant) @@ -14,7 +14,7 @@ def test_watering_system() -> None: try: print("Opening watering system") water_plants(plants) - except: + except ValueError: print("Error: Cannot water None - invalid plant!") finally: print("Closing watering system (cleanup)") diff --git a/02/ex4/ft_raise_errors.py b/02/ex4/ft_raise_errors.py index 5ea3d67..087b9af 100644 --- a/02/ex4/ft_raise_errors.py +++ b/02/ex4/ft_raise_errors.py @@ -1,14 +1,19 @@ -def check_plant_health(plant_name: str, water_level: int, sunlight_hours: int) -> None: +def check_plant_health(plant_name: str, water_level: int, + sunlight_hours: int) -> None: if not plant_name or not plant_name[0]: raise ValueError("Error: Plant name cannot be empty") elif water_level < 1: - raise ValueError(f"Error: Water level {water_level} is too low (min 1)") + raise ValueError(f"Error: Water level {water_level}\ + is too low (min 1)") elif water_level > 10: - raise ValueError(f"Error: Water level {water_level} is too high (max 10)") + raise ValueError(f"Error: Water level {water_level}\ + is too high (max 10)") elif sunlight_hours < 2: - raise ValueError(f"Error: Sunlight hours {sunlight_hours} is too low (min 2)") + raise ValueError(f"Error: Sunlight hours {sunlight_hours}\ + is too low (min 2)") elif sunlight_hours > 12: - raise ValueError(f"Error: Sunlight hours {sunlight_hours} is too high (max 12)") + raise ValueError(f"Error: Sunlight hours {sunlight_hours}\ + is too high (max 12)") else: print("Plant '" + plant_name + "' is healthy!") diff --git a/02/ex5/ft_garden_management.py b/02/ex5/ft_garden_management.py index 5907856..5b46a4a 100644 --- a/02/ex5/ft_garden_management.py +++ b/02/ex5/ft_garden_management.py @@ -8,7 +8,7 @@ class GardenError(Exception): @override def __str__(self) -> str: - return f"Caught a garden error: {self.message}" + return f"Caught a garden error: {self.message}" class SunlightError(GardenError): @@ -17,7 +17,7 @@ class SunlightError(GardenError): @override def __str__(self) -> str: - return f"Caught SunlightError: {self.message}" + return f"Caught SunlightError: {self.message}" class WaterError(GardenError): @@ -34,7 +34,8 @@ class Plant: __water_level: int __sunlight_hours: int - def __init__(self, name: str, water_level: int, sunlight_hours: int) -> None: + def __init__(self, name: str, water_level: int, + sunlight_hours: int) -> None: if name == "": raise Exception("Plant name cannot be empty") self.set_name(name) @@ -57,7 +58,7 @@ class Plant: self.__name = name def set_water_level(self, water_level: int) -> None: - if water_level < 1: + if water_level < 1: raise WaterError(f"Water level {water_level} is too low (min 1)") if water_level > 10: raise WaterError(f"Water level {water_level} is too high (max 10)") @@ -65,9 +66,11 @@ class Plant: def set_sunlight_hours(self, sunlight_hours: int) -> None: if sunlight_hours < 2: - raise SunlightError(f"Sunlight hours {sunlight_hours} is too low (min 2)") + raise SunlightError(f"Sunlight hours {sunlight_hours}\ + is too low (min 2)") if sunlight_hours > 12: - raise SunlightError(f"Sunlight hours {sunlight_hours} is too high (max 12)") + raise SunlightError(f"Sunlight hours {sunlight_hours}\ + is too high (max 12)") self.__sunlight_hours = sunlight_hours def water_plant(self) -> None: @@ -88,7 +91,6 @@ class GardenManager: def __str__(self) -> str: return f"Caught WaterTankError: {self.message}" - def __init__(self) -> None: self.garden = [] self.__water_tank_level = 0 @@ -115,15 +117,20 @@ class GardenManager: try: for plant in self.garden: if plant.get_water_level() < 1: - raise WaterError(f"{plant.get_name()}: Water level {plant.get_water_level()} is too low (min 1)") + raise WaterError(f"{plant.get_name()}: Water level\ + {plant.get_water_level()} is too low (min 1)") elif plant.get_water_level() > 10: - raise WaterError(f"{plant.get_name()}: Water level {plant.get_water_level()} is too high (max 10)") + raise WaterError(f"{plant.get_name()}: Water level\ + {plant.get_water_level()} is too high (max 10)") elif plant.get_sunlight_hours() < 2: - raise SunlightError(f"{plant.get_name()}: Sunlight hours {plant.get_sunlight_hours()} is too low (min 2)") + raise SunlightError(f"{plant.get_name()}: Sunlight hours\ + {plant.get_sunlight_hours()} is too low (min 2)") elif plant.get_sunlight_hours() > 12: - raise SunlightError(f"{plant.get_name()}: Sunlight hours {plant.get_sunlight_hours()} is too high (max 12)") + raise SunlightError(f"{plant.get_name()}: Sunlight hours\ + {plant.get_sunlight_hours()} is too high (max 12)") else: - print(f"{plant.get_name()}: healthy (water: {plant.get_water_level()}, sun: {plant.get_sunlight_hours()})") + print(f"{plant.get_name()}: healthy (water:\ + {plant.get_water_level()}, sun: {plant.get_sunlight_hours()})") except (WaterError, SunlightError) as err: print(err)