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 #577

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 72 additions & 7 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,90 @@
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 __set_name__(self, cls: object, name: str) -> None:
self.private_name = "_" + name

def __get__(self, instance: object, cls: object) -> (int, str):
return getattr(instance, self.private_name)

def __set__(self, instance: object, value: (int, str)) -> None:
"""if not (self.min_amount <= value <= self.max_amount):
raise ValueError("ValueError")"""
Comment on lines +16 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""if not (self.min_amount <= value <= self.max_amount):
raise ValueError("ValueError")"""
if not isinstance(value, int):
raise TypeError("message")
if not (self.max_amount >= value >= self.min_amount):
raise ValueError("message")

setattr(instance, self.private_name, value)


class Visitor:
pass
age = IntegerRange(4, 60)
weight = IntegerRange(20, 120)
height = IntegerRange(80, 220)

def __init__(self,
name: str,
age: int,
height: float,
weight: float) -> None:
self.name = name
self.age = age
self.weight = weight
self.height = height


class SlideLimitationValidator(ABC):
pass
def __init__(self, age: int, weight: float, height: float) -> None:
self.age = age
self.weight = weight
self.height = height

@abstractmethod
def validate(self) -> None:
pass


class ChildrenSlideLimitationValidator(SlideLimitationValidator):
pass
def __init__(self, age: int, weight: float, height: float) -> None:
super().__init__(age, weight, height)

def validate(self) -> bool:
if ((not (4 <= self.age <= 14))
or (not (20 <= self.weight <= 50))
or (not (80 <= self.height <= 120))):
return False
return True
Comment on lines +53 to +57
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRY



class AdultSlideLimitationValidator(SlideLimitationValidator):
pass
def __init__(self, age: int, weight: float, height: float) -> None:
super().__init__(age, weight, height)

def validate(self) -> bool:
if (not (14 <= self.age <= 60)
or not (50 <= self.weight <= 120)
or not (120 <= self.height <= 220)):
return False
return True
Comment on lines +64 to +69
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRY
don't repeat yourself



class Slide:
pass
def __init__(self,
name: str,
limitation_class: [ChildrenSlideLimitationValidator,
AdultSlideLimitationValidator]) -> None:
Comment on lines +75 to +76
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
limitation_class: [ChildrenSlideLimitationValidator,
AdultSlideLimitationValidator]) -> None:
limitation_class: Type[SlideLimitationValidator]) -> 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)
except ValueError:
...
obj = self.limitation_class(visitor.age,
visitor.weight,
visitor.height)
return obj.validate()
Comment on lines +81 to +90
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
try:
self.limitation_class(visitor.age,
visitor.weight,
visitor.height)
except ValueError:
...
obj = self.limitation_class(visitor.age,
visitor.weight,
visitor.height)
return obj.validate()
try:
self.limitation_class(
visitor.age,
visitor.weight,
visitor.height
)
except (TypeError, ValueError):
return False
return True

Loading