From 726c7f3b9f3cb1df14b5ba186378d1541a65fdd7 Mon Sep 17 00:00:00 2001 From: Vadym Sulim Date: Wed, 16 Oct 2024 02:55:24 +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..dd37301a 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: object, name: str) -> None: + self.name = name + self.protected_name = f"_{name}" + + def __get__(self, instance: object, owner: object) -> int: + return getattr(instance, self.protected_name) + + def __set__(self, instance: object, value: int) -> None: + if not self.min_amount <= value <= self.max_amount: + raise ValueError(f"must be in range:" # noqa + f" {self.min_amount} - {self.max_amount}") + setattr(instance, self.protected_name, value) 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, + weight: int, + height: 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.weight, visitor.height) + return True + except ValueError: + return False From c43eb74a3cb8e64458e78c909d05c174092557f9 Mon Sep 17 00:00:00 2001 From: Vadym Sulim Date: Wed, 16 Oct 2024 18:27:36 +0300 Subject: [PATCH 2/3] fixed validation --- app/main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index dd37301a..40529962 100644 --- a/app/main.py +++ b/app/main.py @@ -2,6 +2,8 @@ class IntegerRange: + """Descriptor data validation""" + def __init__(self, min_amount: int, max_amount: int) -> None: self.min_amount = min_amount self.max_amount = max_amount @@ -15,7 +17,7 @@ def __get__(self, instance: object, owner: object) -> int: def __set__(self, instance: object, value: int) -> None: if not self.min_amount <= value <= self.max_amount: - raise ValueError(f"must be in range:" # noqa + raise ValueError(f"{value} must be in range:" # noqa f" {self.min_amount} - {self.max_amount}") setattr(instance, self.protected_name, value) @@ -62,6 +64,6 @@ def __init__(self, name: str, limitation_class: type) -> None: def can_access(self, visitor: Visitor) -> bool: try: self.limitation_class(visitor.age, visitor.weight, visitor.height) - return True except ValueError: return False + return True From 2d789b460c847d88c7839da967f3f5963933d201 Mon Sep 17 00:00:00 2001 From: Vadym Sulim Date: Wed, 16 Oct 2024 18:32:50 +0300 Subject: [PATCH 3/3] fixed solution --- app/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 40529962..983b16e6 100644 --- a/app/main.py +++ b/app/main.py @@ -16,6 +16,8 @@ def __get__(self, instance: object, owner: object) -> int: return getattr(instance, self.protected_name) def __set__(self, instance: object, value: int) -> None: + if not isinstance(value, int): + raise TypeError(f"{self.protected_name} must be an integer") if not self.min_amount <= value <= self.max_amount: raise ValueError(f"{value} must be in range:" # noqa f" {self.min_amount} - {self.max_amount}") @@ -64,6 +66,6 @@ def __init__(self, name: str, limitation_class: type) -> None: def can_access(self, visitor: Visitor) -> bool: try: self.limitation_class(visitor.age, visitor.weight, visitor.height) - except ValueError: + except (ValueError, TypeError): return False return True