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

PR add solution #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

PR add solution #1

wants to merge 1 commit into from

Conversation

andrsj
Copy link
Owner

@andrsj andrsj commented Jan 12, 2023

No description provided.

self.name = name

def visit_cafe(self, vstr: dict) -> str:
if "vaccine" not in vstr.keys() or vstr["vaccine"] == None:
Copy link
Owner Author

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():
Copy link
Owner Author

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:
Copy link
Owner Author

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
Copy link
Owner Author

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)):
Copy link
Owner Author

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:
Copy link
Owner Author

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):
Copy link
Owner Author

Choose a reason for hiding this comment

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

for i in range(len(friends)):
try:
cafe.visit_cafe(friends[i])
except (NotVaccinatedError, OutdatedVaccineError):
Copy link
Owner Author

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
Copy link
Owner Author

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
Copy link
Owner Author

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, \
Copy link
Owner Author

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
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant