-
Notifications
You must be signed in to change notification settings - Fork 0
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
PR add solution #1
base: master
Are you sure you want to change the base?
Conversation
self.name = name | ||
|
||
def visit_cafe(self, vstr: dict) -> str: | ||
if "vaccine" not in vstr.keys() or vstr["vaccine"] == None: |
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.
To check if a key exists in a dictionary - it is better to use in
with the dictionary instead: <key> in vstr
If you compare a value with None
- it's necessary to use the is
key-word
[0 is None
- False; 0 == None
- True]
You can use the method get
from a dictionary to check the value
vstr.get(<key>, None)
- where None
is a default value
def visit_cafe(self, vstr: dict) -> str: | ||
if "vaccine" not in vstr.keys() or vstr["vaccine"] == None: | ||
raise NotVaccinatedError("not vaccinated") | ||
elif vstr["vaccine"]["expiration_date"] < datetime.date.today(): |
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.
We can rewrite elif
to if
because we can't reach the 14th row if the previous if-branch raises an error
raise OutdatedVaccineError("outdated") | ||
elif not vstr["wearing_a_mask"]: | ||
raise NotWearingMaskError("not wearing mask") | ||
else: |
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.
We can skip the else
key-word because if an error is raised, the function will be finished
Just do:
if ...
if ...
...
return <something>
elif not vstr["wearing_a_mask"]: | ||
raise NotWearingMaskError("not wearing mask") | ||
else: | ||
return "Welcome to " + self.name |
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.
Much better use f-string - more readeble
Example: f"Welcome to {self.name}"
Or format
method from str
"Welcome to {}".format(self.name)"
def go_to_cafe(friends, cafe) -> str: | ||
total = 0 | ||
|
||
for i in range(len(friends)): |
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.
We can iterate in list
in Python-way [NO C-way]
for friend in friends
- in friend
will be written a one element of the list
OutdatedVaccineError) | ||
|
||
|
||
def go_to_cafe(friends, cafe) -> str: |
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.
Recommendation: use type-hints for lists
@@ -0,0 +1,14 @@ | |||
class NotWearingMaskError(BaseException): |
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 Exception
for user-defined errors
https://docs.python.org/3/library/exceptions.html#BaseException
for i in range(len(friends)): | ||
try: | ||
cafe.visit_cafe(friends[i]) | ||
except (NotVaccinatedError, OutdatedVaccineError): |
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.
From task: Use it to catch both types of errors in the same except clause.
We can put only VaccineError
here
|
||
|
||
class OutdatedVaccineError(VaccineError): | ||
pass |
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.
New line is not found [Have you used the flake8
command for checking?
@@ -1 +1,21 @@ | |||
# write your code here | |||
from app.cafe import Cafe |
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.
Unused import
@@ -0,0 +1,19 @@ | |||
from app.errors import NotVaccinatedError, \ |
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.
Rewrite import with brackets and in alphabetical order
from <library> import (
a-package
b-package
...
k-package
)
No description provided.