-
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 #586
base: master
Are you sure you want to change the base?
Solution #586
Conversation
app/main.py
Outdated
def __set__(self, instance: any, value: int) -> None: | ||
if self.min_amount <= value <= self.max_amount: | ||
setattr(instance, self.name, value) | ||
else: | ||
raise ValueError( | ||
f"Value must be between " | ||
f"{self.min_amount} and {self.max_amount}" | ||
) |
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.
def __set__(self, instance: any, value: int) -> None: | |
if self.min_amount <= value <= self.max_amount: | |
setattr(instance, self.name, value) | |
else: | |
raise ValueError( | |
f"Value must be between " | |
f"{self.min_amount} and {self.max_amount}" | |
) | |
def __set__(self, instance: any, value: int) -> None: | |
if not isinstance(value, int): | |
raise TypeError("Only integers are allowed") | |
if value not in range(self.min_amount, self.max_amount + 1): | |
raise ValueError( | |
f"Value must be between " | |
f"{self.min_amount} and {self.max_amount}" | |
) | |
setattr(instance, self.protected_name, value) |
app/main.py
Outdated
def __init__(self) -> None: | ||
super().__init__( | ||
IntegerRange(4, 14), | ||
IntegerRange(20, 51), | ||
IntegerRange(80, 121) | ||
) |
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.
def __init__(self) -> None: | |
super().__init__( | |
IntegerRange(4, 14), | |
IntegerRange(20, 51), | |
IntegerRange(80, 121) | |
) | |
age = IntegerRange(4, 14) | |
weight = IntegerRange(20, 50) | |
height = IntegerRange(80, 120) |
app/main.py
Outdated
def __init__(self) -> None: | ||
super().__init__(IntegerRange(14, 61), | ||
IntegerRange(50, 121), | ||
IntegerRange(120, 221) | ||
) |
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.
def __init__(self) -> None: | |
super().__init__(IntegerRange(14, 61), | |
IntegerRange(50, 121), | |
IntegerRange(120, 221) | |
) | |
age = IntegerRange(14, 60) | |
weight = IntegerRange(50, 120) | |
height = IntegerRange(120, 220) |
app/main.py
Outdated
return ( | ||
self.limitation_class.age_range.min_amount | ||
<= visitor.age <= self.limitation_class.age_range.max_amount | ||
and self.limitation_class.weight_range.min_amount | ||
<= visitor.weight <= self.limitation_class.weight_range.max_amount | ||
and self.limitation_class.height_range.min_amount | ||
<= visitor.height | ||
<= self.limitation_class.height_range.max_amount | ||
) |
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 try .. except
here
app/main.py
Outdated
def can_access(self, visitor: Visitor) -> bool: | ||
try: | ||
return ( | ||
self.limitation_class.age_range.min_amount | ||
<= visitor.age <= self.limitation_class.age_range.max_amount | ||
and self.limitation_class.weight_range.min_amount | ||
<= visitor.weight | ||
<= self.limitation_class.weight_range.max_amount | ||
and self.limitation_class.height_range.min_amount | ||
<= visitor.height | ||
<= self.limitation_class.height_range.max_amount | ||
) | ||
except Exception as e: | ||
print(e) |
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.
def can_access(self, visitor: Visitor) -> bool: | |
try: | |
return ( | |
self.limitation_class.age_range.min_amount | |
<= visitor.age <= self.limitation_class.age_range.max_amount | |
and self.limitation_class.weight_range.min_amount | |
<= visitor.weight | |
<= self.limitation_class.weight_range.max_amount | |
and self.limitation_class.height_range.min_amount | |
<= visitor.height | |
<= self.limitation_class.height_range.max_amount | |
) | |
except Exception as e: | |
print(e) | |
def can_access(self, visitor: Visitor) -> bool: | |
try: | |
self.limitation_class(visitor.age, visitor.weight, visitor.height) | |
return True | |
except ValueError: | |
return False |
app/main.py
Outdated
def __init__(self) -> None: | ||
super().__init__( | ||
age_range=IntegerRange(14, 60), | ||
weight_range=IntegerRange(50, 120), | ||
height_range=IntegerRange(120, 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.
def __init__(self) -> None: | |
super().__init__( | |
age_range=IntegerRange(14, 60), | |
weight_range=IntegerRange(50, 120), | |
height_range=IntegerRange(120, 220) | |
) | |
age = IntegerRange(14, 60) | |
height = IntegerRange(120, 220) | |
weight = IntegerRange(50, 120) |
app/main.py
Outdated
super().__init__( | ||
age_range=IntegerRange(4, 14), | ||
weight_range=IntegerRange(20, 50), | ||
height_range=IntegerRange(80, 120) | ||
) |
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.
super().__init__( | |
age_range=IntegerRange(4, 14), | |
weight_range=IntegerRange(20, 50), | |
height_range=IntegerRange(80, 120) | |
) | |
age = IntegerRange(4, 14) | |
height = IntegerRange(80, 120) | |
weight = IntegerRange(20, 50) |
app/main.py
Outdated
f"Value 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 comment
The reason will be displayed to describe this comment to others. Learn more.
please use setattr
here
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!
No description provided.