-
Notifications
You must be signed in to change notification settings - Fork 620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution #577
Solution #577
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,25 +1,90 @@ | ||||||||
from abc import ABC | ||||||||
from abc import ABC, abstractmethod | ||||||||
|
||||||||
|
||||||||
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, cls: object, name: str) -> None: | ||||||||
self.private_name = "_" + name | ||||||||
|
||||||||
def __get__(self, instance: object, cls: object) -> (int, str): | ||||||||
return getattr(instance, self.private_name) | ||||||||
|
||||||||
def __set__(self, instance: object, value: (int, str)) -> None: | ||||||||
"""if not (self.min_amount <= value <= self.max_amount): | ||||||||
raise ValueError("ValueError")""" | ||||||||
setattr(instance, self.private_name, value) | ||||||||
|
||||||||
|
||||||||
class Visitor: | ||||||||
pass | ||||||||
age = IntegerRange(4, 60) | ||||||||
weight = IntegerRange(20, 120) | ||||||||
height = IntegerRange(80, 220) | ||||||||
|
||||||||
def __init__(self, | ||||||||
name: str, | ||||||||
age: int, | ||||||||
height: float, | ||||||||
weight: float) -> None: | ||||||||
self.name = name | ||||||||
self.age = age | ||||||||
self.weight = weight | ||||||||
self.height = height | ||||||||
|
||||||||
|
||||||||
class SlideLimitationValidator(ABC): | ||||||||
pass | ||||||||
def __init__(self, age: int, weight: float, height: float) -> None: | ||||||||
self.age = age | ||||||||
self.weight = weight | ||||||||
self.height = height | ||||||||
|
||||||||
@abstractmethod | ||||||||
def validate(self) -> None: | ||||||||
pass | ||||||||
|
||||||||
|
||||||||
class ChildrenSlideLimitationValidator(SlideLimitationValidator): | ||||||||
pass | ||||||||
def __init__(self, age: int, weight: float, height: float) -> None: | ||||||||
super().__init__(age, weight, height) | ||||||||
|
||||||||
def validate(self) -> bool: | ||||||||
if ((not (4 <= self.age <= 14)) | ||||||||
or (not (20 <= self.weight <= 50)) | ||||||||
or (not (80 <= self.height <= 120))): | ||||||||
return False | ||||||||
return True | ||||||||
|
||||||||
Comment on lines
+53
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DRY |
||||||||
|
||||||||
class AdultSlideLimitationValidator(SlideLimitationValidator): | ||||||||
pass | ||||||||
def __init__(self, age: int, weight: float, height: float) -> None: | ||||||||
super().__init__(age, weight, height) | ||||||||
|
||||||||
def validate(self) -> bool: | ||||||||
if (not (14 <= self.age <= 60) | ||||||||
or not (50 <= self.weight <= 120) | ||||||||
or not (120 <= self.height <= 220)): | ||||||||
return False | ||||||||
return True | ||||||||
|
||||||||
Comment on lines
+64
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DRY |
||||||||
|
||||||||
class Slide: | ||||||||
pass | ||||||||
def __init__(self, | ||||||||
name: str, | ||||||||
limitation_class: [ChildrenSlideLimitationValidator, | ||||||||
AdultSlideLimitationValidator]) -> None: | ||||||||
self.name = name | ||||||||
Comment on lines
+75
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
self.limitation_class = limitation_class | ||||||||
|
||||||||
def can_access(self, visitor: Visitor) -> bool: | ||||||||
try: | ||||||||
self.limitation_class(visitor.age, | ||||||||
visitor.weight, | ||||||||
visitor.height) | ||||||||
except ValueError: | ||||||||
... | ||||||||
obj = self.limitation_class(visitor.age, | ||||||||
visitor.weight, | ||||||||
visitor.height) | ||||||||
return obj.validate() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.