mirror of
https://github.com/DavidGailleton/42-Piscine_Python.git
synced 2026-03-13 20:56:54 +01:00
FIX: every exercices of module 02
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
def check_temperature(temp_str: str) -> int:
|
def check_temperature(temp_str: str) -> int | None:
|
||||||
print("Testing temperature:", temp_str)
|
"""Test if temperature is correct"""
|
||||||
try:
|
try:
|
||||||
x = int(temp_str)
|
x = int(temp_str)
|
||||||
if x > 40:
|
if x > 40:
|
||||||
@@ -8,20 +8,21 @@ def check_temperature(temp_str: str) -> int:
|
|||||||
raise Exception("is too cold for plants (min 0°C)")
|
raise Exception("is too cold for plants (min 0°C)")
|
||||||
else:
|
else:
|
||||||
print("Temperature " + temp_str + "°C is perfect for plants!")
|
print("Temperature " + temp_str + "°C is perfect for plants!")
|
||||||
|
return x
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("Error: '" + temp_str + "' is not a valid number")
|
print("Error: '" + temp_str + "' is not a valid number")
|
||||||
pass
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print("Error: " + temp_str + "°C", ex)
|
print("Error: " + temp_str + "°C", ex)
|
||||||
pass
|
return (None)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
print("=== Garden Temperature checker ===")
|
||||||
|
print("\nTesting temperature: 25")
|
||||||
check_temperature("25")
|
check_temperature("25")
|
||||||
print("")
|
print("\nTesting temperature: abc")
|
||||||
check_temperature("abc")
|
check_temperature("abc")
|
||||||
print("")
|
print("\nTesting temperature: 100")
|
||||||
check_temperature("100")
|
check_temperature("100")
|
||||||
print("")
|
print("\nTesting temperature: -50")
|
||||||
check_temperature("-50")
|
check_temperature("-50")
|
||||||
print("")
|
print("\nAll tests completed - program didn't crash!")
|
||||||
print("All tests completed - program didn't crash!")
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
def garden_operations(case: str) -> None:
|
def garden_operations(case: str) -> None:
|
||||||
|
"""Test some opereation case"""
|
||||||
if case == "ValueError":
|
if case == "ValueError":
|
||||||
int("abc")
|
int("abc")
|
||||||
elif case == "ZeroDivisionError":
|
elif case == "ZeroDivisionError":
|
||||||
@@ -11,26 +12,29 @@ def garden_operations(case: str) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def test_error_types() -> None:
|
def test_error_types() -> None:
|
||||||
|
"""Tester function to catch errors"""
|
||||||
|
print("=== Garden Error Types Demo ===")
|
||||||
|
print("\nTesting ValueError...")
|
||||||
try:
|
try:
|
||||||
garden_operations("ValueError")
|
garden_operations("ValueError")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("Caught ValueError: invalid literal for int()")
|
print("Caught ValueError: invalid literal for int()")
|
||||||
|
print("\nTesting ZeroDivisionError...")
|
||||||
try:
|
try:
|
||||||
garden_operations("ZeroDivisionError")
|
garden_operations("ZeroDivisionError")
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
print("Caught ZeroDivisionError: division by zero")
|
print("Caught ZeroDivisionError: division by zero")
|
||||||
|
print("\nTesting FileNotFoundError...")
|
||||||
try:
|
try:
|
||||||
garden_operations("FileNotFoundError")
|
garden_operations("FileNotFoundError")
|
||||||
except FileNotFoundError as err:
|
except FileNotFoundError as err:
|
||||||
print("Caught FileNotFoundError:", err)
|
print("Caught FileNotFoundError:", err)
|
||||||
|
print("\nTesting KeyError...")
|
||||||
try:
|
try:
|
||||||
garden_operations("KeyError")
|
garden_operations("KeyError")
|
||||||
except KeyError as err:
|
except KeyError as err:
|
||||||
print("Caught KeyError:", err)
|
print("Caught KeyError:", err)
|
||||||
|
print("\nTesting multiple errors together...")
|
||||||
try:
|
try:
|
||||||
garden_operations("ValueError")
|
garden_operations("ValueError")
|
||||||
except (ValueError, KeyError, ZeroDivisionError, FileNotFoundError):
|
except (ValueError, KeyError, ZeroDivisionError, FileNotFoundError):
|
||||||
@@ -39,4 +43,4 @@ def test_error_types() -> None:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_error_types()
|
test_error_types()
|
||||||
print("All error types tested successfully!")
|
print("\nAll error types tested successfully!")
|
||||||
|
|||||||
@@ -1,33 +1,53 @@
|
|||||||
class GardenError(Exception):
|
from typing_extensions import override
|
||||||
def __init__(self, message):
|
|
||||||
self.message = message
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
|
class GardenError(Exception):
|
||||||
|
def __init__(self, message: str) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.message: str = message
|
||||||
|
|
||||||
|
@override
|
||||||
|
def __str__(self) -> str:
|
||||||
return f"Caught a garden error: {self.message}"
|
return f"Caught a garden error: {self.message}"
|
||||||
|
|
||||||
|
|
||||||
class PlantError(GardenError):
|
class PlantError(GardenError):
|
||||||
def __init__(self, message):
|
def __init__(self, message: str) -> None:
|
||||||
self.message = message
|
super().__init__(message)
|
||||||
|
|
||||||
def __str__(self):
|
@override
|
||||||
|
def __str__(self) -> str:
|
||||||
return f"Caught PlantError: {self.message}"
|
return f"Caught PlantError: {self.message}"
|
||||||
|
|
||||||
|
|
||||||
class WaterError(GardenError):
|
class WaterError(GardenError):
|
||||||
def __init__(self, message):
|
def __init__(self, message: str) -> None:
|
||||||
self.message = message
|
super().__init__(message)
|
||||||
|
|
||||||
def __str__(self):
|
@override
|
||||||
|
def __str__(self) -> str:
|
||||||
return f"Caught WaterError: {self.message}"
|
return f"Caught WaterError: {self.message}"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
print("=== Custom Garden Errors Demo ===")
|
||||||
|
print("\n Testing PlantError...")
|
||||||
try:
|
try:
|
||||||
raise PlantError("The tomato plant is wilting!")
|
raise PlantError("The tomato plant is wilting!")
|
||||||
except PlantError as err:
|
except PlantError as err:
|
||||||
print(err)
|
print(err)
|
||||||
|
print("\nTesting WaterError...")
|
||||||
try:
|
try:
|
||||||
raise WaterError("Not enough water in the tank!")
|
raise WaterError("Not enough water in the tank!")
|
||||||
except WaterError as err:
|
except WaterError as err:
|
||||||
print(err)
|
print(err)
|
||||||
|
print("\nTesting catching all garden errors...")
|
||||||
|
try:
|
||||||
|
raise GardenError("The tomato plant is wilting")
|
||||||
|
except GardenError as err:
|
||||||
|
print(err)
|
||||||
|
try:
|
||||||
|
raise GardenError("Not enough water in the tank")
|
||||||
|
except GardenError as err:
|
||||||
|
print(err)
|
||||||
|
print("\nAll custom error types work correctly !")
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
def water_plants(plant_list: []) -> None:
|
def water_plants(plant_list: list[str | None]) -> None:
|
||||||
|
"""Display watering plant if plant is a string"""
|
||||||
for plant in plant_list:
|
for plant in plant_list:
|
||||||
if plant == None:
|
if plant == None:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
@@ -6,7 +7,10 @@ def water_plants(plant_list: []) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def test_watering_system() -> None:
|
def test_watering_system() -> None:
|
||||||
|
"""Tester for water_plants() function"""
|
||||||
plants = ["tomato", "lettuce", "carrots", None]
|
plants = ["tomato", "lettuce", "carrots", None]
|
||||||
|
print("=== Garden Watering System ===")
|
||||||
|
print("\nTesting normal watering...")
|
||||||
try:
|
try:
|
||||||
print("Opening watering system")
|
print("Opening watering system")
|
||||||
water_plants(plants)
|
water_plants(plants)
|
||||||
@@ -14,6 +18,7 @@ def test_watering_system() -> None:
|
|||||||
print("Error: Cannot water None - invalid plant!")
|
print("Error: Cannot water None - invalid plant!")
|
||||||
finally:
|
finally:
|
||||||
print("Closing watering system (cleanup)")
|
print("Closing watering system (cleanup)")
|
||||||
|
print("\nCleanup always happens, even with errors")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
def check_plant_health(plant_name, water_level, sunlight_hours):
|
def check_plant_health(plant_name: str, water_level: int, sunlight_hours: int) -> None:
|
||||||
if not plant_name or not plant_name[0]:
|
if not plant_name or not plant_name[0]:
|
||||||
raise ValueError("Error: Plant name cannot be empty")
|
raise ValueError("Error: Plant name cannot be empty")
|
||||||
elif water_level < 1:
|
elif water_level < 1:
|
||||||
@@ -13,24 +13,29 @@ def check_plant_health(plant_name, water_level, sunlight_hours):
|
|||||||
print("Plant '" + plant_name + "' is healthy!")
|
print("Plant '" + plant_name + "' is healthy!")
|
||||||
|
|
||||||
|
|
||||||
def test_plant_checks():
|
def test_plant_checks() -> None:
|
||||||
|
print("=== Garden Plant Health Checker ===")
|
||||||
|
print("\nTesting good values...")
|
||||||
try:
|
try:
|
||||||
check_plant_health("tomato", 5, 5)
|
check_plant_health("tomato", 5, 5)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
print(err)
|
print(err)
|
||||||
|
print("\nTesting bad water level...")
|
||||||
try:
|
try:
|
||||||
check_plant_health("salade", 0, 5)
|
check_plant_health("salade", 0, 5)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
print(err)
|
print(err)
|
||||||
|
print("\nTesting bad sunlight hours...")
|
||||||
try:
|
try:
|
||||||
check_plant_health("carrots", 5, 20)
|
check_plant_health("carrots", 5, 20)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
print(err)
|
print(err)
|
||||||
|
print("\nTesting empty plant name...")
|
||||||
try:
|
try:
|
||||||
check_plant_health("", 5, 5)
|
check_plant_health("", 5, 5)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
print(err)
|
print(err)
|
||||||
print("All error raising tests completed!")
|
print("\nAll error raising tests completed!")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -1,98 +1,170 @@
|
|||||||
from logging import raiseExceptions
|
from typing_extensions import override
|
||||||
|
|
||||||
|
|
||||||
|
class GardenError(Exception):
|
||||||
|
def __init__(self, message: str) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.message: str = message
|
||||||
|
|
||||||
|
@override
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return f"Caught a garden error: {self.message}"
|
||||||
|
|
||||||
|
|
||||||
|
class SunlightError(GardenError):
|
||||||
|
def __init__(self, message: str) -> None:
|
||||||
|
super().__init__(message)
|
||||||
|
|
||||||
|
@override
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return f"Caught SunlightError: {self.message}"
|
||||||
|
|
||||||
|
|
||||||
|
class WaterError(GardenError):
|
||||||
|
def __init__(self, message: str) -> None:
|
||||||
|
super().__init__(message)
|
||||||
|
|
||||||
|
@override
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return f"Caught WaterError: {self.message}"
|
||||||
|
|
||||||
|
|
||||||
class Plant:
|
class Plant:
|
||||||
__name: str
|
__name: str
|
||||||
__water: int
|
__water_level: int
|
||||||
__sun: int
|
__sunlight_hours: int
|
||||||
|
|
||||||
@classmethod
|
def __init__(self, name: str, water_level: int, sunlight_hours: int) -> None:
|
||||||
def set_name(cls, name: str) -> None:
|
if name == "":
|
||||||
if not name or not name[0]:
|
raise Exception("Plant name cannot be empty")
|
||||||
raise ValueError("Name cannot be empty")
|
|
||||||
cls.__name = name
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def set_water(cls, water: int) -> None:
|
|
||||||
if water < 0:
|
|
||||||
raise ValueError("Water level cannot be negative")
|
|
||||||
cls.__water = water
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def set_sun(cls, sun: int) -> None:
|
|
||||||
if sun < 0:
|
|
||||||
raise ValueError("Sun level cannot be negative")
|
|
||||||
cls.__sun = sun
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_name(cls) -> str:
|
|
||||||
return cls.__name
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_water(cls) -> int:
|
|
||||||
return cls.__water
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_sun(cls) -> int:
|
|
||||||
return cls.__sun
|
|
||||||
|
|
||||||
def __init__(self, name: str, water: int, sun: int) -> None:
|
|
||||||
self.set_name(name)
|
self.set_name(name)
|
||||||
self.set_water(water)
|
self.__water_level = water_level
|
||||||
self.set_sun(sun)
|
self.__sunlight_hours = sunlight_hours
|
||||||
|
print(f"Added {name} successfully")
|
||||||
|
|
||||||
|
def get_name(self) -> str:
|
||||||
|
return self.__name
|
||||||
|
|
||||||
|
def get_water_level(self) -> int:
|
||||||
|
return self.__water_level
|
||||||
|
|
||||||
|
def get_sunlight_hours(self) -> int:
|
||||||
|
return self.__sunlight_hours
|
||||||
|
|
||||||
|
def set_name(self, name: str) -> None:
|
||||||
|
if name == "":
|
||||||
|
raise Exception("Name cannot be empty")
|
||||||
|
self.__name = name
|
||||||
|
|
||||||
|
def set_water_level(self, water_level: int) -> None:
|
||||||
|
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)")
|
||||||
|
self.__water_level = water_level
|
||||||
|
|
||||||
|
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)")
|
||||||
|
if sunlight_hours > 12:
|
||||||
|
raise SunlightError(f"Sunlight hours {sunlight_hours} is too high (max 12)")
|
||||||
|
self.__sunlight_hours = sunlight_hours
|
||||||
|
|
||||||
|
def water_plant(self) -> None:
|
||||||
|
self.__water_level += 1
|
||||||
|
print(f"Watering {self.__name} - success")
|
||||||
|
|
||||||
|
|
||||||
class GardenManager:
|
class GardenManager:
|
||||||
__plants: list[Plant]
|
garden: list[Plant]
|
||||||
__water_tank_level: int
|
__water_tank_level: int
|
||||||
|
|
||||||
@classmethod
|
class WaterTankError(Exception):
|
||||||
def get_water_tank_level(cls) -> int:
|
def __init__(self, message: str) -> None:
|
||||||
return cls.__water_tank_level
|
super().__init__()
|
||||||
|
self.message: str = message
|
||||||
|
|
||||||
@classmethod
|
@override
|
||||||
def set_water_tank_level(cls, level: int) -> None:
|
def __str__(self) -> str:
|
||||||
cls.__water_tank_level = level
|
return f"Caught WaterTankError: {self.message}"
|
||||||
|
|
||||||
def add_plant(self, name: str, water: int, sun: int) -> None:
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.garden = []
|
||||||
|
self.__water_tank_level = 0
|
||||||
|
|
||||||
|
def get_water_tank_level(self) -> int:
|
||||||
|
return self.__water_tank_level
|
||||||
|
|
||||||
|
def set_water_tank_level(self, water_level: int) -> None:
|
||||||
try:
|
try:
|
||||||
new_plant = Plant(name, water, sun)
|
if water_level < 0:
|
||||||
i = 0
|
raise self.WaterTankError("Water level cannot be negative")
|
||||||
for n in self.__plants:
|
self.__water_tank_level = water_level
|
||||||
i = i + 1
|
except self.WaterTankError as err:
|
||||||
new_lst: list[Plant | None] = [None] * (i + 1)
|
print(err)
|
||||||
i = 0
|
|
||||||
for n in self.__plants:
|
def add_plants_to_garden(self, plants: list[tuple[str, int, int]]) -> None:
|
||||||
new_lst[i] = n
|
try:
|
||||||
i = i + 1
|
for a, b, c in plants:
|
||||||
new_lst[i] = new_plant
|
self.garden.append(Plant(a, b, c))
|
||||||
print("Added", name, "successfully")
|
except Exception as err:
|
||||||
except ValueError as err:
|
print("Error adding plant:", err)
|
||||||
|
|
||||||
|
def check_plants_health(self) -> None:
|
||||||
|
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)")
|
||||||
|
elif plant.get_water_level() > 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)")
|
||||||
|
elif plant.get_sunlight_hours() > 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()})")
|
||||||
|
except (WaterError, SunlightError) as err:
|
||||||
print(err)
|
print(err)
|
||||||
|
|
||||||
def water_plants(self) -> None:
|
def water_plants(self) -> None:
|
||||||
print("Watering plants...")
|
"""Watering all plants"""
|
||||||
try:
|
try:
|
||||||
print("Opening watering system")
|
print("Opening watering system")
|
||||||
for n in self.__plants:
|
for plant in self.garden:
|
||||||
if self.__water_tank_level <= 0:
|
if self.__water_tank_level > 0:
|
||||||
raise Exception("Caught GardenError: Not enough water in tank")
|
plant.water_plant()
|
||||||
n.set_water(n.get_water() + 1)
|
self.__water_tank_level -= 1
|
||||||
self.__water_tank_level = self.__water_tank_level - 1;
|
else:
|
||||||
print("Watering", n.get_name(), "- success")
|
raise self.WaterTankError("Water tank is empty")
|
||||||
except Exception as err:
|
except self.WaterTankError as err:
|
||||||
print(err);
|
print(err)
|
||||||
finally:
|
finally:
|
||||||
print("Closing watering system (cleanup)")
|
print("Closing watering system (cleanup)")
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self.__plants = []
|
def test_garden_management() -> None:
|
||||||
self.__water_tank_level = 0
|
manager = GardenManager()
|
||||||
|
print("=== Garden Management System ===\n")
|
||||||
|
print("Adding plants to garden...")
|
||||||
|
manager.add_plants_to_garden([
|
||||||
|
("Rose", 5, 5),
|
||||||
|
("Oak", 7, 2),
|
||||||
|
("Chrysantem", 2, 6),
|
||||||
|
("Tomato", 10, 3),
|
||||||
|
("", 8, 7)
|
||||||
|
])
|
||||||
|
print("\nWatering plants...")
|
||||||
|
manager.set_water_tank_level(10)
|
||||||
|
manager.water_plants()
|
||||||
|
print("\nChecking plant health...")
|
||||||
|
manager.check_plants_health()
|
||||||
|
print("\nTesting error recovery...")
|
||||||
|
manager.set_water_tank_level(0)
|
||||||
|
manager.water_plants()
|
||||||
|
print("System recovered and continuing")
|
||||||
|
print("\nGarden management system test complet!")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
manager = GardenManager()
|
test_garden_management()
|
||||||
manager.set_water_tank_level(10)
|
|
||||||
manager.add_plant("Tomato", 2, 10)
|
|
||||||
manager.add_plant("Lettuce", 5, 7)
|
|
||||||
manager.water_plants()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user