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' #538

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions app/cafe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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:
if "vaccine" not in visitor:
raise NotVaccinatedError
if datetime.date.today() > visitor["vaccine"]["expiration_date"]:

Choose a reason for hiding this comment

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

It would be preferable to place at the first position a value that you want to check from the function input and the value you check against in the second position.

Choose a reason for hiding this comment

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

In case you are unsure about the incoming dict structure, it would be preferable to use .get() in production settings to retrieve the required values.

raise OutdatedVaccineError
if not visitor["wearing_a_mask"]:
raise NotWearingMaskError
return f"Welcome to {self.name}"
17 changes: 17 additions & 0 deletions app/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class VaccineError(Exception):
pass


class NotVaccinatedError(VaccineError):
def __str__(self) -> str:
return "Visitor is not vaccinated"


class OutdatedVaccineError(VaccineError):
def __str__(self) -> str:
return "Vaccine is outdated"


class NotWearingMaskError(Exception):
def __str__(self) -> str:
return "Visitor is not wearing a mask"
17 changes: 16 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# write your code here
from app.cafe import Cafe
from app.errors import VaccineError, NotWearingMaskError


def go_to_cafe(friends: list[dict], cafe: Cafe) -> str:
not_wearing_masks = 0
for visitor in friends:
try:
cafe.visit_cafe(visitor)
except NotWearingMaskError:
not_wearing_masks += 1
except VaccineError:
return "All friends should be vaccinated"
if not not_wearing_masks:
return "Friends can go to KFC"
return f"Friends should buy {not_wearing_masks} masks"
Loading