-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
lockdown_cafe #1252
base: master
Are you sure you want to change the base?
lockdown_cafe #1252
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import datetime | ||
from app.errors import (NotVaccinatedError, NotWearingMaskError, | ||
OutdatedVaccineError) | ||
|
||
|
||
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() | ||
|
||
expiration_date = visitor["vaccine"]["expiration_date"] | ||
if expiration_date < datetime.date.today(): | ||
raise OutdatedVaccineError() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider providing a descriptive message when raising the |
||
|
||
if not visitor.get("wearing_a_mask", False): | ||
raise NotWearingMaskError() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider providing a descriptive message when raising the |
||
|
||
return f"Welcome to {self.name}" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class VaccineError(Exception): | ||
pass | ||
|
||
|
||
class NotVaccinatedError(VaccineError): | ||
def __init__(self) -> None: | ||
super().__init__("Visitor is not vaccinated.") | ||
|
||
|
||
class OutdatedVaccineError(VaccineError): | ||
def __init__(self) -> None: | ||
super().__init__("Visitor's vaccine is outdated.") | ||
|
||
|
||
class NotWearingMaskError(Exception): | ||
def __init__(self) -> None: | ||
super().__init__("Visitor is not wearing a mask.") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,86 @@ | ||
# write your code here | ||
from app.cafe import Cafe | ||
import datetime | ||
|
||
|
||
def go_to_cafe(friends: list[dict], cafe: Cafe) -> str: | ||
masks_needed = 0 | ||
all_vaccinated = True | ||
|
||
for friend in friends: | ||
if "vaccine" not in friend: | ||
all_vaccinated = False | ||
continue | ||
|
||
expiration_date = friend["vaccine"]["expiration_date"] | ||
if expiration_date < datetime.date.today(): | ||
all_vaccinated = False | ||
|
||
if not friend.get("wearing_a_mask", False): | ||
masks_needed += 1 | ||
|
||
if not all_vaccinated: | ||
return "All friends should be vaccinated" | ||
|
||
if masks_needed > 0: | ||
return f"Friends should buy {masks_needed} masks" | ||
|
||
return f"Friends can go to {cafe.name}" | ||
|
||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
kfc = Cafe("KFC") | ||
|
||
friends = [ | ||
{ | ||
"name": "Alisa", | ||
"vaccine": { | ||
"expiration_date": datetime.date.today() | ||
}, | ||
"wearing_a_mask": True | ||
}, | ||
{ | ||
"name": "Bob", | ||
"vaccine": { | ||
"expiration_date": datetime.date.today() | ||
}, | ||
"wearing_a_mask": True | ||
}, | ||
] | ||
|
||
print(go_to_cafe(friends, kfc)) | ||
|
||
friends = [ | ||
{ | ||
"name": "Alisa", | ||
"vaccine": { | ||
"expiration_date": datetime.date.today() | ||
}, | ||
"wearing_a_mask": False | ||
}, | ||
{ | ||
"name": "Bob", | ||
"vaccine": { | ||
"expiration_date": datetime.date.today() | ||
}, | ||
"wearing_a_mask": False | ||
}, | ||
] | ||
|
||
print(go_to_cafe(friends, kfc)) | ||
|
||
friends = [ | ||
{ | ||
"name": "Alisa", | ||
"wearing_a_mask": True | ||
}, | ||
{ | ||
"name": "Bob", | ||
"vaccine": { | ||
"expiration_date": datetime.date.today() | ||
}, | ||
"wearing_a_mask": True | ||
}, | ||
] | ||
|
||
print(go_to_cafe(friends, kfc)) |
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.
Consider providing a descriptive message when raising the
NotVaccinatedError
to comply with the checklist requirement for descriptive error messages. Example:raise NotVaccinatedError('Visitor is not vaccinated.')
.