Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Solution #614

wants to merge 1 commit into from

Conversation

matweyes
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a 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! ✨

Comment on lines 27 to +32
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

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.

Comment on lines 35 to +43
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

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.

Comment on lines +67 to +76
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

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a 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! ✨

Comment on lines 4 to +24
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)

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.

Comment on lines 27 to +32
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

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.

Comment on lines 35 to +38
class SlideLimitationValidator(ABC):
pass
age = IntegerRange(4, 60)
weight = IntegerRange(20, 120)
height = IntegerRange(80, 220)

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.

Comment on lines +68 to +73
try:
self.limitation_class(
age=visitor.age,
height=visitor.height,
weight=visitor.weight
)

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a 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! ✨

Comment on lines 4 to +24
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)

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.

Comment on lines 27 to +32
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

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.

Comment on lines 35 to +38
class SlideLimitationValidator(ABC):
pass
age = IntegerRange(4, 60)
weight = IntegerRange(20, 120)
height = IntegerRange(80, 220)

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.

Comment on lines 58 to +76
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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants