From ded0ea6889b52ffb844f8b06f34ea47479abe848 Mon Sep 17 00:00:00 2001 From: Illia Date: Sun, 20 Aug 2023 18:48:43 +0300 Subject: [PATCH 1/2] Implemented py-lockdown-cafe --- app/cafe.py | 29 +++++++++++++++++++++++++++++ app/errors.py | 20 ++++++++++++++++++++ app/main.py | 26 +++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 app/cafe.py create mode 100644 app/errors.py diff --git a/app/cafe.py b/app/cafe.py new file mode 100644 index 00000000..d7aebee4 --- /dev/null +++ b/app/cafe.py @@ -0,0 +1,29 @@ +import datetime + +from app.errors import (NotVaccinatedError, + OutdatedVaccineError, + NotWearingMaskError) + + +class Cafe: + def __init__(self, name: str) -> None: + self.name = name + + def visit_cafe(self, + visitor: dict + ) -> (str + | NotVaccinatedError + | OutdatedVaccineError + | NotWearingMaskError): + + if "vaccine" not in visitor: + raise NotVaccinatedError + + current_date = datetime.datetime.now().date() + if visitor["vaccine"]["expiration_date"] < current_date: + raise OutdatedVaccineError + + if not visitor.get("wearing_a_mask", False): + raise NotWearingMaskError(masks_to_buy=0) + + return f"Welcome to {self.name}" diff --git a/app/errors.py b/app/errors.py new file mode 100644 index 00000000..cb4be7db --- /dev/null +++ b/app/errors.py @@ -0,0 +1,20 @@ +class VaccineError(Exception): + def __str__(self) -> str: + return "All friends should be vaccinated" + + +class NotVaccinatedError(VaccineError): + pass + + +class OutdatedVaccineError(VaccineError): + pass + + +class NotWearingMaskError(Exception): + def __init__(self, masks_to_buy: int) -> None: + self.masks_to_buy = masks_to_buy + super().__init__() + + def __str__(self) -> str: + return f"Friends should buy {self.masks_to_buy} masks" diff --git a/app/main.py b/app/main.py index fa56336e..6bc43148 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,25 @@ -# write your code here +from app.cafe import Cafe +from app.errors import NotWearingMaskError, VaccineError + + +def go_to_cafe(friends: list, + cafe: Cafe + ) -> str | VaccineError: + + not_wearing_mask_error = None + + for friend in friends: + try: + cafe.visit_cafe(friend) + except VaccineError as e: + return str(e) + except NotWearingMaskError as e: + if not not_wearing_mask_error: + not_wearing_mask_error = e + if not_wearing_mask_error: + not_wearing_mask_error.masks_to_buy += 1 + + if not_wearing_mask_error is not None: + return str(not_wearing_mask_error) + + return f"Friends can go to {cafe.name}" From e77ae0e7968d8e28e19b0c316d5cfe2305707ed1 Mon Sep 17 00:00:00 2001 From: Illia Date: Tue, 22 Aug 2023 19:32:56 +0300 Subject: [PATCH 2/2] fixed code --- app/cafe.py | 23 +++++++++-------------- app/errors.py | 2 +- app/main.py | 11 +++++------ 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/app/cafe.py b/app/cafe.py index d7aebee4..77b088c1 100644 --- a/app/cafe.py +++ b/app/cafe.py @@ -1,29 +1,24 @@ import datetime -from app.errors import (NotVaccinatedError, - OutdatedVaccineError, - NotWearingMaskError) +from app.errors import ( + NotVaccinatedError, + OutdatedVaccineError, + NotWearingMaskError +) class Cafe: def __init__(self, name: str) -> None: self.name = name - def visit_cafe(self, - visitor: dict - ) -> (str - | NotVaccinatedError - | OutdatedVaccineError - | NotWearingMaskError): - + def visit_cafe( + self, visitor: dict + ) -> str: if "vaccine" not in visitor: raise NotVaccinatedError - current_date = datetime.datetime.now().date() if visitor["vaccine"]["expiration_date"] < current_date: raise OutdatedVaccineError - if not visitor.get("wearing_a_mask", False): - raise NotWearingMaskError(masks_to_buy=0) - + raise NotWearingMaskError return f"Welcome to {self.name}" diff --git a/app/errors.py b/app/errors.py index cb4be7db..f243d96f 100644 --- a/app/errors.py +++ b/app/errors.py @@ -12,7 +12,7 @@ class OutdatedVaccineError(VaccineError): class NotWearingMaskError(Exception): - def __init__(self, masks_to_buy: int) -> None: + def __init__(self, masks_to_buy: int = 0) -> None: self.masks_to_buy = masks_to_buy super().__init__() diff --git a/app/main.py b/app/main.py index 6bc43148..bdaa72ad 100644 --- a/app/main.py +++ b/app/main.py @@ -1,11 +1,11 @@ from app.cafe import Cafe -from app.errors import NotWearingMaskError, VaccineError +from app.errors import ( + NotWearingMaskError, + VaccineError, +) -def go_to_cafe(friends: list, - cafe: Cafe - ) -> str | VaccineError: - +def go_to_cafe(friends: list, cafe: Cafe) -> str | VaccineError: not_wearing_mask_error = None for friend in friends: @@ -21,5 +21,4 @@ def go_to_cafe(friends: list, if not_wearing_mask_error is not None: return str(not_wearing_mask_error) - return f"Friends can go to {cafe.name}"