-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efaa0ca
commit 351aa4c
Showing
1 changed file
with
56 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,87 @@ | ||
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 | ||
self.height = 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 |