-
Notifications
You must be signed in to change notification settings - Fork 612
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 #616
base: master
Are you sure you want to change the base?
solution #616
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 |
---|---|---|
|
@@ -2,24 +2,70 @@ | |
|
||
|
||
class IntegerRange: | ||
pass | ||
"""Descriptor data validation""" | ||
|
||
def __init__(self, min_amount: int, max_amount: int) -> None: | ||
self.min_amount = min_amount | ||
self.max_amount = max_amount | ||
|
||
def __set_name__(self, owner: object, name: str) -> None: | ||
self.name = name | ||
self.protected_name = f"_{name}" | ||
|
||
def __get__(self, instance: object, owner: object) -> int: | ||
return getattr(instance, self.protected_name) | ||
|
||
def __set__(self, instance: object, value: int) -> None: | ||
if not isinstance(value, int): | ||
raise TypeError(f"{self.protected_name} must be an integer") | ||
if not self.min_amount <= value <= self.max_amount: | ||
raise ValueError(f"{value} must be in range:" # noqa | ||
f" {self.min_amount} - {self.max_amount}") | ||
setattr(instance, self.protected_name, value) | ||
|
||
|
||
class Visitor: | ||
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. The 'Visitor' class could use a docstring to explain its purpose and usage. |
||
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 | ||
Comment on lines
27
to
+36
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 a design issue. The |
||
|
||
|
||
class SlideLimitationValidator(ABC): | ||
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. The 'SlideLimitationValidator' class could use a docstring to explain its purpose and usage. 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. The SlideLimitationValidator class is declared as an abstract base class (ABC) but it doesn't have any abstract methods. If you intend to use it as an interface, you should define some abstract methods. |
||
pass | ||
def __init__(self, | ||
age: int, | ||
weight: int, | ||
height: int) -> None: | ||
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. The SlideLimitationValidator class and its subclasses are not using the IntegerRange descriptor for age, weight, and height attributes. You should use them in these classes. |
||
self.age = age | ||
self.weight = weight | ||
self.height = height | ||
|
||
|
||
class ChildrenSlideLimitationValidator(SlideLimitationValidator): | ||
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. The 'ChildrenSlideLimitationValidator' class could use a docstring to explain its purpose and usage. |
||
pass | ||
age = IntegerRange(4, 14) | ||
height = IntegerRange(80, 120) | ||
weight = IntegerRange(20, 50) | ||
|
||
|
||
class AdultSlideLimitationValidator(SlideLimitationValidator): | ||
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. The 'AdultSlideLimitationValidator' class could use a docstring to explain its purpose and usage. |
||
pass | ||
age = IntegerRange(14, 60) | ||
height = IntegerRange(120, 220) | ||
weight = IntegerRange(50, 120) | ||
|
||
|
||
class Slide: | ||
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. The 'Slide' class could use a docstring to explain its purpose and usage. |
||
pass | ||
def __init__(self, name: str, limitation_class: type) -> 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) | ||
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 line seems to be creating a new instance of a limitation class but does nothing with it. This is likely a bug, as it doesn't validate anything. You probably wanted to create an instance and then call a method on it to validate the visitor's parameters. 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. The limitation_class is being instantiated with visitor's age, weight, and height. However, the SlideLimitationValidator class and its subclasses don't have a logic to check these values in the constructor. You should move the logic from IntegerRange's set method to init method or add a new method to check these values. |
||
except (ValueError, TypeError): | ||
return False | ||
return True |
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.
It is a good practice to provide a docstring for the class to explain its purpose and how it should be used.