From ce5b1e0ddb9ba21e5aaae0620f65ab4b4253136b Mon Sep 17 00:00:00 2001 From: Serhii Date: Sat, 12 Oct 2024 20:15:34 +0300 Subject: [PATCH 1/5] solutionend --- app/main.py | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index 6d375672..7fe87f62 100644 --- a/app/main.py +++ b/app/main.py @@ -1,25 +1,64 @@ from abc import ABC +from typing import Any class IntegerRange: - pass + def __init__(self, min_amount: int, max_amount: int): + self.min_amount = min_amount + self.max_amount = max_amount + + def __set_name__(self, owner: Any, name: str) -> None: + self.protected_name = "_" + name + + def __get__(self, instance: Any, owner: Any) -> Any: + return getattr(instance, self.protected_name) + + def __set__(self, instance: Any, value: Any) -> None: + if not isinstance(value, int): + raise TypeError + if not (self.min_amount <= value <= self.max_amount): + raise ValueError + 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 + + def validate(self) -> None: + pass class ChildrenSlideLimitationValidator(SlideLimitationValidator): - pass + age = IntegerRange(4, 14) + weight = IntegerRange(20, 50) + height = IntegerRange(80, 120) class AdultSlideLimitationValidator(SlideLimitationValidator): - pass + age = IntegerRange(14, 60) + weight = IntegerRange(50, 120) + height = IntegerRange(120, 220) 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(visitor.age, visitor.weight, visitor.height) + except (ValueError, TypeError): + return False + return True \ No newline at end of file From 7f45294419ca4d1bdcca79cdcd18c1e9f5d3053a Mon Sep 17 00:00:00 2001 From: Serhii Date: Sat, 12 Oct 2024 20:18:17 +0300 Subject: [PATCH 2/5] solution 2 --- app/main.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 7fe87f62..2d4ff6d6 100644 --- a/app/main.py +++ b/app/main.py @@ -3,7 +3,7 @@ class IntegerRange: - def __init__(self, min_amount: int, max_amount: int): + def __init__(self, min_amount: int, max_amount: int) -> None: self.min_amount = min_amount self.max_amount = max_amount @@ -30,7 +30,7 @@ def __init__(self, name: str, age: int, weight: int, height: int) -> None: class SlideLimitationValidator(ABC): - def __init__(self, age: int, weight: int, height: int) -> None: + def __init__(self, age: int, weight: int, height: int) -> None: self.age = age self.weight = weight self.height = height @@ -52,7 +52,8 @@ class AdultSlideLimitationValidator(SlideLimitationValidator): class Slide: - def __init__(self, name: str, limitation_class: SlideLimitationValidator) -> None: + def __init__(self, name: str, + limitation_class: SlideLimitationValidator) -> None: self.name = name self.limitation_class = limitation_class @@ -61,4 +62,4 @@ def can_access(self, visitor: Visitor) -> bool: self.limitation_class(visitor.age, visitor.weight, visitor.height) except (ValueError, TypeError): return False - return True \ No newline at end of file + return True From 0c6c8188d1a204b3505a05c58ee6092d12b11888 Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 14 Oct 2024 09:22:57 +0300 Subject: [PATCH 3/5] solution 3 --- app/main.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index 2d4ff6d6..2b9af128 100644 --- a/app/main.py +++ b/app/main.py @@ -15,9 +15,9 @@ def __get__(self, instance: Any, owner: Any) -> Any: def __set__(self, instance: Any, value: Any) -> None: if not isinstance(value, int): - raise TypeError + raise TypeError(f"{value} should be integer!") if not (self.min_amount <= value <= self.max_amount): - raise ValueError + raise ValueError(f"{value} should be between {self.min_amount} and {self.max_amount}") setattr(instance, self.protected_name, value) @@ -35,9 +35,6 @@ def __init__(self, age: int, weight: int, height: int) -> None: self.weight = weight self.height = height - def validate(self) -> None: - pass - class ChildrenSlideLimitationValidator(SlideLimitationValidator): age = IntegerRange(4, 14) From 75624daba262b8984895e85918e3620b82d40f94 Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 14 Oct 2024 14:12:12 +0300 Subject: [PATCH 4/5] solution 4 --- app/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 2b9af128..0a20de93 100644 --- a/app/main.py +++ b/app/main.py @@ -56,7 +56,9 @@ def __init__(self, name: str, def can_access(self, visitor: Visitor) -> bool: try: - self.limitation_class(visitor.age, visitor.weight, visitor.height) + self.limitation_class(visitor.age, + visitor.weight, + visitor.height) except (ValueError, TypeError): return False return True From 7bcc74f876570a7873ae4e720635be85a132701e Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 14 Oct 2024 14:13:37 +0300 Subject: [PATCH 5/5] solution 5 --- app/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 0a20de93..87375b0a 100644 --- a/app/main.py +++ b/app/main.py @@ -17,7 +17,9 @@ def __set__(self, instance: Any, value: Any) -> None: if not isinstance(value, int): raise TypeError(f"{value} should be integer!") if not (self.min_amount <= value <= self.max_amount): - raise ValueError(f"{value} should be between {self.min_amount} and {self.max_amount}") + raise ValueError(f"{value} should be between " + f"{self.min_amount} and " + f"{self.max_amount}") setattr(instance, self.protected_name, value)