From d8c2a1bbb34ec42ad698eaf2552849cd6d5238a3 Mon Sep 17 00:00:00 2001 From: Kostiantin Pastukh Date: Sat, 14 Sep 2024 18:00:41 +0300 Subject: [PATCH 1/3] Solution --- app/main.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index 6d375672..93e4b47b 100644 --- a/app/main.py +++ b/app/main.py @@ -1,12 +1,35 @@ from abc import ABC +from typing import Any class IntegerRange: - pass + def __init__(self, min_amount: int, max_amount: int) -> None: + self.min_amount = min_amount + self.max_amount = max_amount + + def __get__(self, instance: Any, owner: Any) -> Any: + return getattr(instance, self.protected_name) + + def __set__(self, instance: Any, value: int) -> Any: + 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"{self.protected_name} " + f"must be between {self.min_amount} " + f"and {self.max_amount}") + setattr(instance, self.protected_name, value) + + def __set_name__(self, owner: Any, name: str) -> None: + self.protected_name = "_" + name 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): @@ -14,12 +37,31 @@ class SlideLimitationValidator(ABC): 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: SlideLimitationValidator + ) -> None: + self.name = name + self.limitation_class = limitation_class() + + def can_access(self, visitor: Visitor) -> bool: + try: + self.limitation_class.age = visitor.age + self.limitation_class.height = visitor.height + self.limitation_class.weight = visitor.weight + except (TypeError, ValueError): + return False + return True From d4a512859ff950a910cb36d3b28ee777e82f935f Mon Sep 17 00:00:00 2001 From: Kostiantin Pastukh Date: Sat, 14 Sep 2024 19:19:13 +0300 Subject: [PATCH 2/3] New Solution --- app/main.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app/main.py b/app/main.py index 93e4b47b..becdee69 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,5 @@ from abc import ABC -from typing import Any +from typing import Any, Type class IntegerRange: @@ -7,7 +7,7 @@ def __init__(self, min_amount: int, max_amount: int) -> None: self.min_amount = min_amount self.max_amount = max_amount - def __get__(self, instance: Any, owner: Any) -> Any: + def __get__(self, instance: Any, owner: type) -> Any: return getattr(instance, self.protected_name) def __set__(self, instance: Any, value: int) -> Any: @@ -20,7 +20,7 @@ def __set__(self, instance: Any, value: int) -> Any: f"and {self.max_amount}") setattr(instance, self.protected_name, value) - def __set_name__(self, owner: Any, name: str) -> None: + def __set_name__(self, owner: type, name: str) -> None: self.protected_name = "_" + name @@ -33,7 +33,10 @@ def __init__(self, name: str, age: int, weight: int, height: int) -> None: 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): @@ -52,16 +55,14 @@ class Slide: def __init__( self, name: str, - limitation_class: SlideLimitationValidator + limitation_class: Type[SlideLimitationValidator] ) -> None: self.name = name - self.limitation_class = limitation_class() + self.limitation_class = limitation_class def can_access(self, visitor: Visitor) -> bool: try: - self.limitation_class.age = visitor.age - self.limitation_class.height = visitor.height - self.limitation_class.weight = visitor.weight + self.limitation_class(visitor.age, visitor.height, visitor.weight) except (TypeError, ValueError): return False return True From d6d89a85a8ccd6e75f07c0c6f166605df0f17cc1 Mon Sep 17 00:00:00 2001 From: Kostiantin Pastukh Date: Sat, 14 Sep 2024 19:54:04 +0300 Subject: [PATCH 3/3] Another Solution --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index becdee69..676a3757 100644 --- a/app/main.py +++ b/app/main.py @@ -7,10 +7,10 @@ def __init__(self, min_amount: int, max_amount: int) -> None: self.min_amount = min_amount self.max_amount = max_amount - def __get__(self, instance: Any, owner: type) -> Any: + def __get__(self, instance: object, owner: type) -> Any: return getattr(instance, self.protected_name) - def __set__(self, instance: Any, value: int) -> Any: + def __set__(self, instance: object, value: int) -> Any: if not isinstance(value, int): raise TypeError(f"{self.protected_name} must be an integer") if not (self.min_amount <= value <= self.max_amount):