Skip to content

Sprucie - Asya S #84

Open
asya0107 wants to merge 1 commit into
Ada-C16:masterfrom
asya0107:master
Open

Sprucie - Asya S #84
asya0107 wants to merge 1 commit into
Ada-C16:masterfrom
asya0107:master

Conversation

@asya0107

@asya0107 asya0107 commented Oct 1, 2021

Copy link
Copy Markdown

No description provided.

@audreyandoy audreyandoy left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Asya!

Your submission has completed all the waves and passed all required tests. Your code was well-organized and utilized a lot of helper functions which made the overall code very neat!

My main feedback primarily focuses on utilizing guard clauses, avoiding mutable types as default parameters, using the parent constructor and being wary of creating unneeded instance attributes. Guard clauses can help refine our code and communicate our intentions with our functions more clearly.

Overall, this project is well-deserving of a 🟢 green 🟢 ✨

Comment thread README.md
Comment on lines 166 to +169

### Wave 1

The first two tests in wave 1 imply:
<!-- The first two tests in wave 1 imply:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This README file didn't need to be pushed. We can use commands like git add to ensure that you're only adding specific files to be tracked, committed, and pushed to Github.

Comment thread swap_meet/clothing.py
Comment on lines +9 to +12
if condition :
self.condition = condition
else:
self.inventory = 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The requirements don't discuss inventory for Clothing instances, so inventory can be omitted here.

Comment thread swap_meet/clothing.py
Comment on lines +6 to +7
super().__init__(condition = 0, category = "")
self.category = 'Clothing'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Creating the category attribute here isn't necessary as we can have the parent constructor to create a category attribute for the Clothing instance by passing that value in the super() constructor.

Suggested change
super().__init__(condition = 0, category = "")
self.category = 'Clothing'
super().__init__(condition = 0, category = "Clothing")

Comment thread swap_meet/decor.py
Comment on lines +5 to +11
def __init__ (self, condition = 0 ):
super().__init__(condition = 0, category = "")
self.category = 'Decor'
if condition :
self.condition = condition
else:
self.inventory = 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same comments about Clothing can also be applied for Decor.

Comment thread swap_meet/electronics.py
Comment on lines +3 to +11
class Electronics(Item):
def __init__ (self, condition = 0):
super().__init__(condition = 0, category = "")
self.category = 'Electronics'
if condition :
self.condition = condition
else:
self.inventory = 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same comments about Clothing can also be applied for Electronics.

Comment thread swap_meet/vendor.py
Comment on lines +39 to +47
def swap_items(self, another_vendor, my_item, their_item):
if my_item not in self.inventory or their_item not in another_vendor.inventory:
return False
else:
self.inventory.remove(my_item)
another_vendor.inventory.append(my_item)
another_vendor.inventory.remove(their_item)
self.inventory.append(their_item)
return True

@audreyandoy audreyandoy Oct 21, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Love the guard clause!!

However, else conditions are typically used for last case scenarios. Every if conditional doesn't need to be accompanied by an else. When we get into web development, else clauses are useful in scenarios where we need a 'catch all' such as handling gibberish a user enters into a form.

Let's say a user was trying to enter their email in a form.. if the user enters !@#AFS12334 into the form, we can have conditionals describe the input as needing to be a string type, containing alphanumeric values, etc. and in case the input doesn't cover all the required checks we can use else: return "Please enter a valid email" as a catch all for invalid inputs.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We can refactor this function like so:

Suggested change
def swap_items(self, another_vendor, my_item, their_item):
if my_item not in self.inventory or their_item not in another_vendor.inventory:
return False
else:
self.inventory.remove(my_item)
another_vendor.inventory.append(my_item)
another_vendor.inventory.remove(their_item)
self.inventory.append(their_item)
return True
def swap_items(self, another_vendor, my_item, their_item):
if my_item not in self.inventory or their_item not in another_vendor.inventory:
return False
self.inventory.remove(my_item)
another_vendor.inventory.append(my_item)
another_vendor.inventory.remove(their_item)
self.inventory.append(their_item)
return True

Comment thread swap_meet/vendor.py
Comment on lines +50 to +59
def swap_first_item(self, another_vendor):
if self.inventory == [] or another_vendor.inventory == []:
return False
else:
another_vendor.inventory.append(self.inventory[0])
self.inventory.remove(self.inventory[0])
self.inventory.append(another_vendor.inventory[0])
another_vendor.inventory.remove(another_vendor.inventory[0])

return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

See my comment above about else clauses. We could have reused the swap_items method to do the actual swapping.

Suggested change
def swap_first_item(self, another_vendor):
if self.inventory == [] or another_vendor.inventory == []:
return False
else:
another_vendor.inventory.append(self.inventory[0])
self.inventory.remove(self.inventory[0])
self.inventory.append(another_vendor.inventory[0])
another_vendor.inventory.remove(another_vendor.inventory[0])
return True
def swap_first_item(self, another_vendor):
if self.inventory == [] or another_vendor.inventory == []:
return False
self.swap_items(self, another_vendor, self.inventory[0], another_vendor.inventory[0])
return True

This method would have a worst-case time complexity of O(n) due to remove.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We can improve the time complexity to O(1) by using multiple assignment.

        self.inventory[0], friend_vendor.inventory[0] = friend_vendor.inventory[0], self.inventory[0]

Here's more info on Python multiple assignment: https://www.w3schools.com/python/gloss_python_assign_value_to_multiple_variables.asp

Comment thread swap_meet/vendor.py
Comment on lines +62 to +75
def get_best_by_category(self, category):
best_condition = 0.0
best_item = ''

for item in self.inventory:
if item.category == category:
if item.condition > best_condition:
best_condition = item.condition
best_item = item

if best_item:
return best_item
else:
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

To refactor, we can assign None to best_item in the beginning. That way if the category is not found in inventory best_item will remain None.

Suggested change
def get_best_by_category(self, category):
best_condition = 0.0
best_item = ''
for item in self.inventory:
if item.category == category:
if item.condition > best_condition:
best_condition = item.condition
best_item = item
if best_item:
return best_item
else:
return None
def get_best_by_category(self, category):
best_condition = 0.0
best_item = None
for item in self.inventory:
if item.category == category:
if item.condition > best_condition:
best_condition = item.condition
best_item = item
return best_item

Comment thread swap_meet/vendor.py
Comment on lines +77 to +88
def swap_best_by_category(self, other, their_priority, my_priority):

# my best item to give to them
my_best_item = self.get_best_by_category(their_priority)
# their best item to give to me
their_best_item = other.get_best_by_category(my_priority)

if my_best_item and their_best_item:
self.swap_items(other, my_best_item, their_best_item)
return True
else:
return False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great use of helper methods!! We can also use a guard clause here!

Comment thread swap_meet/vendor.py
Comment on lines +93 to +105













Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No need to submit a project with this much empty space. You can use the autopep8 package to format your projects (instructions can be found in your viewing party feedback)

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.

2 participants