From efaa0ca99226ea4ef1f965d0a0138d72cffe2249 Mon Sep 17 00:00:00 2001 From: yura Date: Fri, 13 Sep 2024 13:14:16 +0300 Subject: [PATCH 1/3] Sol --- app/main.py | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index 6d375672..2dcff5c1 100644 --- a/app/main.py +++ b/app/main.py @@ -2,24 +2,57 @@ class IntegerRange: - pass + def __init__(self, min_amount, max_amount): + self.min_amount = min_amount + self.max_amount = max_amount + + def __set_name__(self, owner, name): + self.name = name + + def __get__(self, instance, owner): + return instance.__dict__.get(self.name) + + def __set__(self, instance, value): + if not (self.min_amount <= value <= self.max_amount): + raise ValueError(f"{self.name} must be between {self.min_amount} and {self.max_amount}") + instance.__dict__[self.name] = value class Visitor: - pass + def __init__(self, name, age, weight, height): + self.name = name + self.age = age + self.weight = weight + self.height = height class SlideLimitationValidator(ABC): - pass + def __init__(self, age_range, weight_range, height_range): + self.age = IntegerRange(*age_range) + self.weight = IntegerRange(*weight_range) + self.height = IntegerRange(*height_range) class ChildrenSlideLimitationValidator(SlideLimitationValidator): - pass + def __init__(self): + super().__init__((4, 14), (20, 50), (80, 120)) class AdultSlideLimitationValidator(SlideLimitationValidator): - pass + def __init__(self): + super().__init__((14, 60), (50, 120), (120, 220)) class Slide: - pass + def __init__(self, name, limitation_class): + self.name = name + self.limitation_class = limitation_class() + + def can_access(self, visitor): + try: + self.limitation_class.age.__set__(visitor, visitor.age) + self.limitation_class.weight.__set__(visitor, visitor.weight) + self.limitation_class.height.__set__(visitor, visitor.height) + return True + except ValueError: + return False From 351aa4c6e739aa3c80101490c6bdc1f7a0bf7807 Mon Sep 17 00:00:00 2001 From: yura Date: Fri, 13 Sep 2024 17:54:36 +0300 Subject: [PATCH 2/3] Solution --- app/main.py | 83 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 27 deletions(-) diff --git a/app/main.py b/app/main.py index 2dcff5c1..ff38f2a3 100644 --- a/app/main.py +++ b/app/main.py @@ -1,25 +1,42 @@ +from __future__ import annotations from abc import ABC class IntegerRange: - def __init__(self, min_amount, max_amount): - self.min_amount = min_amount + def __init__( + self, + min_amount: int, + max_amount: int + ) -> None: self.max_amount = max_amount + self.min_amount = min_amount - def __set_name__(self, owner, name): - self.name = name + def __set_name__(self, owner: object, name: str) -> None: + self.protected_attr = "_" + name - def __get__(self, instance, owner): - return instance.__dict__.get(self.name) + def __set__(self, instance: object, value: int) -> None: + if not (self.max_amount >= value >= self.min_amount): + raise ValueError( + f"" + f"Quantity should not be less than {self.max_amount} " + f"and greater than {self.min_amount}" + ) + if not isinstance(value, int): + raise TypeError("Value should be integer.") + setattr(instance, self.protected_attr, value) - def __set__(self, instance, value): - if not (self.min_amount <= value <= self.max_amount): - raise ValueError(f"{self.name} must be between {self.min_amount} and {self.max_amount}") - instance.__dict__[self.name] = value + def __get__(self, instance: object, owner: object) -> int: + return getattr(instance, self.protected_attr) class Visitor: - def __init__(self, name, age, weight, height): + def __init__( + self, + name: int, + age: int, + weight: int, + height: int + ) -> None: self.name = name self.age = age self.weight = weight @@ -27,32 +44,44 @@ def __init__(self, name, age, weight, height): class SlideLimitationValidator(ABC): - def __init__(self, age_range, weight_range, height_range): - self.age = IntegerRange(*age_range) - self.weight = IntegerRange(*weight_range) - self.height = IntegerRange(*height_range) + def __init__( + self, + age: int, + weight: int, + height: int + ) -> None: + self.age = age + self.weight = weight + self.height = height class ChildrenSlideLimitationValidator(SlideLimitationValidator): - def __init__(self): - super().__init__((4, 14), (20, 50), (80, 120)) + age = IntegerRange(4, 14) + height = IntegerRange(80, 120) + weight = IntegerRange(20, 50) class AdultSlideLimitationValidator(SlideLimitationValidator): - def __init__(self): - super().__init__((14, 60), (50, 120), (120, 220)) + age = IntegerRange(14, 60) + height = IntegerRange(120, 220) + weight = IntegerRange(50, 120) class Slide: - def __init__(self, name, limitation_class): + def __init__( + self, name: str, + limitation_class: SlideLimitationValidator + ) -> None: self.name = name - self.limitation_class = limitation_class() + self.limitation_class = limitation_class - def can_access(self, visitor): + def can_access(self, visitor: Visitor) -> bool: try: - self.limitation_class.age.__set__(visitor, visitor.age) - self.limitation_class.weight.__set__(visitor, visitor.weight) - self.limitation_class.height.__set__(visitor, visitor.height) - return True - except ValueError: + self.limitation_class( + visitor.age, + visitor.weight, + visitor.height + ) + except (ValueError, TypeError): return False + return True From 68a1eb7bf7b0e4d6e6b014a3c4cb8672916e8acd Mon Sep 17 00:00:00 2001 From: yura Date: Sat, 14 Sep 2024 11:09:11 +0300 Subject: [PATCH 3/3] add Type annotation --- app/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index ff38f2a3..884ce224 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,6 @@ from __future__ import annotations from abc import ABC +from typing import Type class IntegerRange: @@ -70,7 +71,7 @@ class AdultSlideLimitationValidator(SlideLimitationValidator): class Slide: def __init__( self, name: str, - limitation_class: SlideLimitationValidator + limitation_class: Type[SlideLimitationValidator] ) -> None: self.name = name self.limitation_class = limitation_class