From f7f23be4a7c145fb30eb954b4e9ca9855da815f7 Mon Sep 17 00:00:00 2001 From: user Date: Fri, 11 Oct 2024 13:04:01 +0300 Subject: [PATCH 1/3] Solution --- app/main.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index 6d375672..ea02942a 100644 --- a/app/main.py +++ b/app/main.py @@ -2,24 +2,66 @@ class IntegerRange: - pass + def __init__(self, min_amount: int, max_amount: int) -> None: + self.min_amount = min_amount + self.max_amount = max_amount + + def __set_name__(self, owner: type, name: str) -> None: + self.protected_name = "_" + name + + def __get__(self, instance: type, owner: type) -> int: + return getattr(instance, self.protected_name) + + def __set__(self, instance: type, value: int) -> None: + if self.min_amount <= value <= self.max_amount: + setattr(instance, self.protected_name, value) + else: + raise ValueError class Visitor: - pass + def __init__( + self, + name: str, + age: int, + weight: int, + height: int, + ) -> None: + self.name = name + self.age = age + self.weight = weight + self.height = height class SlideLimitationValidator(ABC): - pass + def __init__(self, age: int, height: int, weight: int) -> None: + self.age = age + self.weight = weight + self.height = height class ChildrenSlideLimitationValidator(SlideLimitationValidator): - pass + age = IntegerRange(4, 14) + height = IntegerRange(80, 120) + weight = IntegerRange(20, 50) class AdultSlideLimitationValidator(SlideLimitationValidator): - pass + age = IntegerRange(14, 60) + height = IntegerRange(120, 220) + weight = IntegerRange(50, 120) class Slide: - pass + + def __init__(self, name: str, limitation_class: type) -> None: + self.name = name + self.limitation_class = limitation_class + + def can_access(self, visitor: Visitor) -> bool: + try: + self.limitation_class(visitor.age, visitor.height, visitor.weight) + except ValueError: + return False + else: + return True From 19767b7adc558e5dac9306448646e885ba7479d2 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 14 Oct 2024 12:46:24 +0300 Subject: [PATCH 2/3] Provided error messages --- app/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index ea02942a..7afd4d28 100644 --- a/app/main.py +++ b/app/main.py @@ -16,7 +16,9 @@ def __set__(self, instance: type, value: int) -> None: if self.min_amount <= value <= self.max_amount: setattr(instance, self.protected_name, value) else: - raise ValueError + raise ValueError(f"{self.protected_name}" + f" must be lesser than {self.min_amount}" + f" and more than {self.max_amount}") class Visitor: From 018b448a680af2b995b2b5258ec792beae38f037 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 14 Oct 2024 12:55:52 +0300 Subject: [PATCH 3/3] fixed error message --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 7afd4d28..4c3c4133 100644 --- a/app/main.py +++ b/app/main.py @@ -17,8 +17,8 @@ def __set__(self, instance: type, value: int) -> None: setattr(instance, self.protected_name, value) else: raise ValueError(f"{self.protected_name}" - f" must be lesser than {self.min_amount}" - f" and more than {self.max_amount}") + f" must be lesser than {self.max_amount}" + f" and more than {self.min_amount}") class Visitor: