-
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 #270
base: master
Are you sure you want to change the base?
Solution #270
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,79 @@ | ||||||
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 __get__(self, instance: object, owner: type) -> None: | ||||||
return instance.__dict__[self.name] | ||||||
|
||||||
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. Use getattr |
||||||
def __set__(self, instance: object, value: int) -> None: | ||||||
if not self.min_amount <= value <= self.max_amount: | ||||||
raise ValueError(f"{self.name} must be between " | ||||||
f"{self.min_amount} and {self.max_amount}") | ||||||
instance.__dict__[self.name] = value | ||||||
|
||||||
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. use setattr 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. RecursionError: maximum recursion depth exceeded 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. In this case you need have two different names: public and protected |
||||||
def __set_name__(self, owner: type, name: str) -> None: | ||||||
self.name = name | ||||||
|
||||||
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
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. Or just fix like this |
||||||
|
||||||
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 | ||||||
|
||||||
age: IntegerRange = IntegerRange(0, 150) | ||||||
weight: IntegerRange = IntegerRange(0, 500) | ||||||
height: IntegerRange = IntegerRange(0, 300) | ||||||
|
||||||
Comment on lines
+29
to
+31
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. This is redundant. Remove this lines |
||||||
|
||||||
class SlideLimitationValidator(ABC): | ||||||
pass | ||||||
def __init__(self, age: tuple, weight: tuple, height: tuple) -> None: | ||||||
self.age = age | ||||||
self.weight = weight | ||||||
self.height = height | ||||||
|
||||||
@abstractmethod | ||||||
def validate(self, visitor: Visitor) -> bool: | ||||||
pass | ||||||
|
||||||
Comment on lines
+40
to
+42
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. This method is redundant 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. Remove in all classes |
||||||
|
||||||
class ChildrenSlideLimitationValidator(SlideLimitationValidator): | ||||||
pass | ||||||
def __init__(self) -> None: | ||||||
super().__init__(age=(4, 14), weight=(20, 50), height=(80, 120)) | ||||||
|
||||||
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. age, weight, height must be class attribute as IntegerRange |
||||||
def validate(self, visitor: Visitor) -> bool: | ||||||
if ( | ||||||
self.age[0] <= visitor.age <= self.age[1] | ||||||
and self.weight[0] <= visitor.weight <= self.weight[1] | ||||||
and self.height[0] <= visitor.height <= self.height[1] | ||||||
): | ||||||
return True | ||||||
return False | ||||||
|
||||||
|
||||||
class AdultSlideLimitationValidator(SlideLimitationValidator): | ||||||
pass | ||||||
def __init__(self) -> None: | ||||||
super().__init__(age=(14, 60), weight=(50, 120), height=(120, 220)) | ||||||
|
||||||
def validate(self, visitor: Visitor) -> bool: | ||||||
if ( | ||||||
self.age[0] <= visitor.age <= self.age[1] | ||||||
and self.weight[0] <= visitor.weight <= self.weight[1] | ||||||
and self.height[0] <= visitor.height <= self.height[1] | ||||||
): | ||||||
return True | ||||||
return False | ||||||
|
||||||
|
||||||
class Slide: | ||||||
pass | ||||||
def __init__(self, name: str, limitation_class: type) -> None: | ||||||
self.name = name | ||||||
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. type of what? Provide inner type |
||||||
self.limitation_class = limitation_class | ||||||
|
||||||
def can_access(self, visitor: Visitor) -> bool: | ||||||
return self.limitation_class().validate(visitor) |
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.
use getattr