From 73db43655e72c649ceb91ca2817293db26f0b00d Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 22 Aug 2023 11:52:16 +0300 Subject: [PATCH 1/4] solution --- app/main.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index 6d375672..f80d860a 100644 --- a/app/main.py +++ b/app/main.py @@ -2,24 +2,67 @@ 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, instance, name: str) -> None: # noqa + self.protected_name = "_" + name + + def __get__(self, instance, owner) -> int: # noqa + return getattr(instance, self.protected_name) + + def __set__(self, instance, value: int) -> None: # noqa + 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 class ChildrenSlideLimitationValidator(SlideLimitationValidator): - pass + age = IntegerRange(min_amount=4, max_amount=14) + height = IntegerRange(min_amount=80, max_amount=120) + weight = IntegerRange(min_amount=20, max_amount=50) + + def __init__(self, age: int, weight: int, height: int) -> None: + super().__init__(age, weight, height) class AdultSlideLimitationValidator(SlideLimitationValidator): - pass + age = IntegerRange(min_amount=14, max_amount=60) + height = IntegerRange(min_amount=120, max_amount=220) + weight = IntegerRange(min_amount=50, max_amount=120) + + def __init__(self, age: int, weight: int, height: int) -> None: + super().__init__(age, weight, height) 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 Exception: + return False + return True From 19c4eccedbfec1e6926836d9f43453cd445b7d36 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 22 Aug 2023 13:51:03 +0300 Subject: [PATCH 2/4] fix --- app/main.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/main.py b/app/main.py index f80d860a..ec10c75a 100644 --- a/app/main.py +++ b/app/main.py @@ -38,24 +38,20 @@ class ChildrenSlideLimitationValidator(SlideLimitationValidator): height = IntegerRange(min_amount=80, max_amount=120) weight = IntegerRange(min_amount=20, max_amount=50) - def __init__(self, age: int, weight: int, height: int) -> None: - super().__init__(age, weight, height) - class AdultSlideLimitationValidator(SlideLimitationValidator): age = IntegerRange(min_amount=14, max_amount=60) height = IntegerRange(min_amount=120, max_amount=220) weight = IntegerRange(min_amount=50, max_amount=120) - def __init__(self, age: int, weight: int, height: int) -> None: - super().__init__(age, weight, height) - class Slide: def __init__( self, name: str, - limitation_class: SlideLimitationValidator + limitation_class: ( + ChildrenSlideLimitationValidator | AdultSlideLimitationValidator + ) ) -> None: self.name = name self.limitation_class = limitation_class From f9f905330d4dd5d08d43b568a512748f96f7af84 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 22 Aug 2023 14:15:34 +0300 Subject: [PATCH 3/4] fix --- app/main.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index ec10c75a..de073ab2 100644 --- a/app/main.py +++ b/app/main.py @@ -49,9 +49,7 @@ class Slide: def __init__( self, name: str, - limitation_class: ( - ChildrenSlideLimitationValidator | AdultSlideLimitationValidator - ) + limitation_class: type[SlideLimitationValidator] ) -> None: self.name = name self.limitation_class = limitation_class From ed8ca8d0eda9d304392b203923e632ddf698a11d Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 22 Aug 2023 15:37:14 +0300 Subject: [PATCH 4/4] fix typing --- app/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index de073ab2..1d038a13 100644 --- a/app/main.py +++ b/app/main.py @@ -6,13 +6,13 @@ def __init__(self, min_amount: int, max_amount: int) -> None: self.min_amount = min_amount self.max_amount = max_amount - def __set_name__(self, instance, name: str) -> None: # noqa + def __set_name__(self, instance: object, name: str) -> None: self.protected_name = "_" + name - def __get__(self, instance, owner) -> int: # noqa + def __get__(self, instance: object, owner: type) -> int: return getattr(instance, self.protected_name) - def __set__(self, instance, value: int) -> None: # noqa + def __set__(self, instance: object, value: int) -> None: if not (self.min_amount <= value <= self.max_amount): raise ValueError setattr(instance, self.protected_name, value)