]> Untitled Git - axy/ft/python01.git/commitdiff
all except last
authorAxy <gilliardmarthey.axel@gmail.com>
Tue, 6 Jan 2026 17:42:46 +0000 (18:42 +0100)
committerAxy <gilliardmarthey.axel@gmail.com>
Tue, 6 Jan 2026 17:42:46 +0000 (18:42 +0100)
ex1/ft_garden_data.py
ex2/ft_plant_growth.py
ex3/ft_plant_factory.py
ex4/ft_garden_security.py [new file with mode: 0644]
ex5/ft_plant_types.py [new file with mode: 0644]
pyproject.toml [new file with mode: 0644]

index 206d32f6cf055ff654624c3083abbf49d588917e..cb016e8227dd76776c06701082da9d07ea4b624b 100644 (file)
@@ -1,4 +1,8 @@
 class Plant:
+    name: str
+    height_cm: int
+    age_days: int
+
     def __init__(self, name: str, height_cm: int, age_days: int) -> None:
         self.name = name
         self.height_cm = height_cm
index 1fefef0d2b033fe1681f261b6f28b3038b85f81c..576959505fedb019cf26eab20e63f5199b2f9532 100644 (file)
@@ -1,4 +1,8 @@
 class Plant:
+    name: str
+    height_cm: int
+    age_days: int
+
     def __init__(self, name: str, height_cm: int, age_days: int) -> None:
         self.name = name
         self.height_cm = height_cm
index 5f1bb25a20b3b480fb7da1a49bc28b8b153e11b4..f64a217a29cd05b49824d29a430e95a5378801ed 100644 (file)
@@ -1,4 +1,8 @@
 class Plant:
+    name: str
+    height_cm: int
+    age_days: int
+
     def __init__(self, name: str, height_cm: int, age_days: int) -> None:
         self.name = name
         self.height_cm = height_cm
@@ -21,6 +25,8 @@ class Plant:
 
 
 class PlantFactory:
+    plants: list[Plant]
+
     def __init__(self):
         self.plants = []
 
diff --git a/ex4/ft_garden_security.py b/ex4/ft_garden_security.py
new file mode 100644 (file)
index 0000000..ae69fb5
--- /dev/null
@@ -0,0 +1,47 @@
+class SecurePlant:
+    __name: str
+    __height_cm: int = 0
+    __age_days: int = 0
+
+    def __init__(self, name: str, height_cm: int, age_days: int) -> None:
+        self.__name = name.capitalize()
+        print(f"Plant created: {self.__name}")
+        self.set_height(height_cm)
+        self.set_age(age_days)
+
+    def __repr__(self) -> str:
+        return f"{self.__name}: ({self.__height_cm}cm, {self.__age_days} days)"
+
+    @staticmethod
+    def __reject_with_message(operation: str, msg: str):
+        print(f"\nInvalid operation attempted: {operation} [REJECTED]")
+        print(f"Security: {msg}")
+
+    @staticmethod
+    def __accept_update(field: str, value: str):
+        print(f"{field} updated: {value} [OK]")
+
+    def set_height(self, height_cm: int):
+        if height_cm < 0:
+            self.__reject_with_message(
+                f"height {height_cm}cm", "Negative height rejected"
+            )
+        else:
+            self.__accept_update("Height", f"{height_cm}cm")
+            self.__height_cm = height_cm
+
+    def set_age(self, age_days: int):
+        if age_days < 0:
+            self.__reject_with_message(
+                f"age {age_days} days", "Negative age rejected"
+            )
+        else:
+            self.__accept_update("Age", f"{age_days} days")
+            self.__age_days = age_days
+
+
+if __name__ == "__main__":
+    print("=== Garden Security System ===")
+    plant = SecurePlant("rose", 25, 30)
+    plant.set_height(-5)
+    print(f"\nCurrent plant: {plant}")
diff --git a/ex5/ft_plant_types.py b/ex5/ft_plant_types.py
new file mode 100644 (file)
index 0000000..d94d822
--- /dev/null
@@ -0,0 +1,124 @@
+class Plant:
+    name: str
+    height_cm: int
+    age_days: int
+
+    def __init__(self, name: str, height_cm: int, age_days: int) -> None:
+        self.name = name.capitalize()
+        self.height_cm = height_cm
+        self.age_days = age_days
+
+    def get_kind(self) -> str:
+        return "Plant"
+
+    def get_attributes(self) -> list[str]:
+        return [f"{self.height_cm}cm", f"{self.age_days} days"]
+
+    def __repr__(self) -> str:
+        def join(sep: str, elements: list[str]) -> str:
+            res: str | None = None
+            for s in elements:
+                if res is None:
+                    res = s
+                else:
+                    res += sep + s
+            if res is not None:
+                return res
+            return ""
+
+        return f"{self.name} ({self.get_kind()}): " + join(
+            ", ", self.get_attributes()
+        )
+
+    def action(self):
+        pass
+
+
+class Flower(Plant):
+    color: str
+
+    def __init__(self, name: str, height_cm: int, age_days: int, color: str):
+        super().__init__(name, height_cm, age_days)
+        self.color = color
+
+    def get_kind(self) -> str:
+        return "Flower"
+
+    def get_attributes(self) -> list[str]:
+        return super().get_attributes() + [f"{self.color} color"]
+
+    def bloom(self):
+        print(f"{self.name} is blooming beautifully!")
+
+    def action(self):
+        self.bloom()
+
+
+class Tree(Plant):
+    trunk_diameter_cm: int
+
+    def __init__(
+        self, name: str, height_cm: int, age_days: int, trunk_diameter_cm: int
+    ):
+        super().__init__(name, height_cm, age_days)
+        self.trunk_diameter_cm = trunk_diameter_cm
+
+    def get_kind(self) -> str:
+        return "Tree"
+
+    def get_attributes(self) -> list[str]:
+        return super().get_attributes() + [
+            f"{self.trunk_diameter_cm}cm diameter"
+        ]
+
+    def produce_shade(self):
+        shade = round(self.trunk_diameter_cm * self.height_cm / 3.2 / 100)
+        print(f"{self.name} provides {shade} square meters of shade")
+
+    def action(self):
+        self.produce_shade()
+
+
+class Vegetable(Plant):
+    harvest_season: str
+    nutritional_value: str
+
+    def __init__(
+        self,
+        name: str,
+        height_cm: int,
+        age_days: int,
+        harvest_season: str,
+        nutritional_value: str,
+    ):
+        super().__init__(name, height_cm, age_days)
+        self.harvest_season = harvest_season
+        self.nutritional_value = nutritional_value
+
+    def get_kind(self) -> str:
+        return "Vegetable"
+
+    def get_attributes(self) -> list[str]:
+        return super().get_attributes() + [f"{self.harvest_season} harvest"]
+
+    def nutritional_info(self):
+        print(f"{self.name} is rich in {self.nutritional_value}")
+
+    def action(self):
+        self.nutritional_info()
+
+
+if __name__ == "__main__":
+    print("=== Garden Plant Types ===")
+    plants: list[Plant] = [
+        Flower("rose", 25, 30, "red"),
+        Tree("oak", 500, 1825, 50),
+        Vegetable("tomato", 80, 90, "summer", "vitamin C"),
+        Flower("dandelion", 10, 20, "yellow"),
+        Tree("cherry tree", 410, 2450, 27),
+        Vegetable("pumkin", 95, 110, "fall", "vitamin A"),
+    ]
+    for plant in plants:
+        print()
+        print(plant)
+        plant.action()
diff --git a/pyproject.toml b/pyproject.toml
new file mode 100644 (file)
index 0000000..a8f43fe
--- /dev/null
@@ -0,0 +1,2 @@
+[tool.black]
+line-length = 79