-
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 #614
base: master
Are you sure you want to change the base?
Solution #614
Conversation
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.
Great job so far! However, there are a few changes that need to be made. Please consider improving the efficiency of the can_access
method in the Slide
class. Also, it would be good to rename the SlideLimitationValidator
class and its subclasses to reflect their actual purpose. Lastly, don't forget to use the IntegerRange
descriptor for the age, weight and height attributes in the Visitor
class. Keep up the good work! 👍
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
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 |
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.
The Visitor
class does not use the IntegerRange
descriptor for age, weight and height attributes. Therefore, there is no validation when an instance of Visitor
is created. Consider using the IntegerRange
descriptor for these attributes as well.
class SlideLimitationValidator(ABC): | ||
pass | ||
age = IntegerRange(4, 60) | ||
weight = IntegerRange(20, 120) | ||
height = IntegerRange(80, 220) | ||
|
||
def __init__(self, age: int, height: int, weight: int) -> None: | ||
self.age = age | ||
self.weight = weight | ||
self.height = height |
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.
The SlideLimitationValidator
class and its subclasses (ChildrenSlideLimitationValidator
and AdultSlideLimitationValidator
) are not used as validators but as data holders. Consider renaming these classes to reflect their actual purpose. For example, you could rename SlideLimitationValidator
to SlideLimitation
, ChildrenSlideLimitationValidator
to ChildrenSlideLimitation
, and AdultSlideLimitationValidator
to AdultSlideLimitation
.
def can_access(self, visitor: Visitor) -> bool: | ||
try: | ||
self.limitation_class( | ||
age=visitor.age, | ||
height=visitor.height, | ||
weight=visitor.weight | ||
) | ||
return True | ||
except ValueError: | ||
return False |
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.
The can_access
method in the Slide
class is not very efficient because it creates a new instance of the SlideLimitationValidator
subclass every time it is called. Consider changing the approach to avoid the unnecessary creation of new instances. For example, you could make the SlideLimitationValidator
subclass a property of the Slide
class and initialize it in the Slide
constructor. Then, in the can_access
method, you could use this property to validate the visitor's attributes.
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.
Great job on your code! It's well-structured and the logic is clear. However, there are a few minor issues that need to be addressed. The can_access
method could be refactored for efficiency, and the SlideLimitationValidator
class should inherit from ABC
if it's not intended to be instantiated. Also, consider adding data validation to the Visitor
class and docstrings to your classes and methods. Keep up the good work! 😊
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
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, owner: type, name: str) -> None: | ||
self.protected_name = f"_{name}" | ||
|
||
def __get__(self, instance: object, owner: type) -> 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} should be an integer.") | ||
if not (self.min_amount <= value <= self.max_amount): | ||
raise ValueError( | ||
f"{self.protected_name} should " | ||
f"be in range {self.min_amount} " | ||
f"to {self.max_amount}." | ||
) | ||
setattr(instance, self.protected_name, value) |
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.
This is a good use of Python's descriptor protocol to enforce data validation. However, it's recommended to add docstrings to your classes and methods to explain their purpose and usage.
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 |
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.
The Visitor
class does not perform any validation on the input parameters. Consider using the IntegerRange
descriptor for age, weight, and height fields to enforce data validation in the Visitor
class as well.
class SlideLimitationValidator(ABC): | ||
pass | ||
age = IntegerRange(4, 60) | ||
weight = IntegerRange(20, 120) | ||
height = IntegerRange(80, 220) |
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.
The ABC
(Abstract Base Class) is imported but not used. If SlideLimitationValidator
is not intended to be instantiated, it should inherit from ABC
.
try: | ||
self.limitation_class( | ||
age=visitor.age, | ||
height=visitor.height, | ||
weight=visitor.weight | ||
) |
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.
In the can_access
method, you are creating a new instance of SlideLimitationValidator
(or its subclasses) every time it's called. This could be inefficient if this method is called frequently. Consider refactoring this to reuse instances.
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.
Great job overall! There are just a few areas that could use some improvement. Remember to validate inputs in your Visitor
class and consider optimizing the can_access
method in the Slide
class. Also, clarify the usage of ABC
in SlideLimitationValidator
and add a docstring for the IntegerRange
class. Keep up the good work and continue refining your skills. 👍
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
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, owner: type, name: str) -> None: | ||
self.protected_name = f"_{name}" | ||
|
||
def __get__(self, instance: object, owner: type) -> 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} should be an integer.") | ||
if not (self.min_amount <= value <= self.max_amount): | ||
raise ValueError( | ||
f"{self.protected_name} should " | ||
f"be in range {self.min_amount} " | ||
f"to {self.max_amount}." | ||
) | ||
setattr(instance, self.protected_name, value) |
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.
Your IntegerRange
class is a descriptor, which is a great use of this Python feature. However, it's a good practice to add a docstring to explain what this class does and how it should be used, especially since descriptors can be a bit tricky to understand for those who are new to them.
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 |
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.
The Visitor
class doesn't use any validation for the provided parameters. It would be a good idea to add some checks to ensure that the values are valid. For example, age, weight, and height should be positive integers, and name should be a non-empty string.
class SlideLimitationValidator(ABC): | ||
pass | ||
age = IntegerRange(4, 60) | ||
weight = IntegerRange(20, 120) | ||
height = IntegerRange(80, 220) |
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.
The SlideLimitationValidator
class is an interesting approach to encapsulate the validation rules for different slides. However, it's not clear why it inherits from ABC
(Abstract Base Class) as it doesn't use any abstract methods or properties. If there's no specific reason for this, it's better to drop the inheritance.
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( | ||
age=visitor.age, | ||
height=visitor.height, | ||
weight=visitor.weight | ||
) | ||
return True | ||
except ValueError: | ||
return False |
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.
In the Slide
class, the can_access
method creates a new instance of limitation_class
every time it's called. This can be inefficient if the method is called frequently. A possible solution would be to create an instance of limitation_class
during the initialization of the Slide
instance and reuse it in the can_access
method.
No description provided.