C22 - Lorraine Shi and Charday Neal#19
Conversation
| self.fabric = fabric | ||
| self.age = age | ||
|
|
||
| def __str__(self): |
There was a problem hiding this comment.
Notice that each __str__ method for Clothing, Decor, and Electronic all start with "An object of type [CLASSNAME] with id [ID].". Inside of Item we can have a __str__ method that print could return the part mentioned above and then pair that with the __str__ inside the child classes (Clothing, Decor, and Electronic). It could something like this:
class Item:
...
def __str__(self):
return f"An object of type {self.get_category()}" \
f" with id {self.id}."
class Clothing(Item):
...
def __str__(self):
return f"{super().__str__()} It is made " \
f"from {self.fabric} fabric."| def __init__(self, id=None, fabric="Unknown",condition=0,age=0): | ||
| super().__init__(id,condition) | ||
| self.fabric = fabric | ||
| self.age = age |
There was a problem hiding this comment.
If all the children classes will have an attribute called age could we move this attribute into the parent class Item?
| class Item: | ||
| pass No newline at end of file | ||
| def __init__(self, id=None,condition=0,age=0): | ||
| self.id = uuid4().int if id is None else id |
| self.age = age | ||
|
|
||
| def get_category(self): | ||
| return type(self).__name__ |
There was a problem hiding this comment.
This could be written as self.__class__.__name__
| if self.condition <= 2.0: | ||
| return "This is poor condition." | ||
| elif self.condition <= 4.0: | ||
| return "It is gently used." | ||
| elif self.condition > 4.0: | ||
| return "Fantastic condition!" No newline at end of file |
There was a problem hiding this comment.
A perfect time to use match casing as well!
| class Vendor: | ||
| pass No newline at end of file | ||
| def __init__(self, inventory=None): | ||
| self.inventory = [] if inventory is None else inventory |
| return None | ||
|
|
||
| def swap_items(self, other_vendor, my_item, their_item): | ||
| if my_item not in self.inventory or their_item not in other_vendor.inventory: |
There was a problem hiding this comment.
We could also use get_by_id to check if an item is in a given inventory and then check to see if that function returns None or the item.
| return False | ||
|
|
||
| self.inventory.remove(my_item) | ||
| self.inventory.append(their_item) |
There was a problem hiding this comment.
Don't we have a method that adds items to an inventory? 👀 Might look simple here, but what happens if the structure of inventory changes?
| def swap_first_item(self, other_vendor): | ||
| if not self.inventory or not other_vendor.inventory: | ||
| return False | ||
|
|
||
| my_first_item = self.inventory[0] | ||
| other_first_item = other_vendor.inventory[0] | ||
| self.swap_items(other_vendor, my_first_item, other_first_item) | ||
| return True |
| return True | ||
|
|
||
| def get_by_category(self, category): | ||
| return [item for item in self.inventory if item.get_category() == category] |
| best_condition_item = item | ||
|
|
||
| return best_condition_item | ||
| # return max(category_items, key=lambda item : item.condition) |
| def swap_best_by_category(self, other_vendor, my_priority, their_priority): | ||
| my_swap_item = self.get_best_by_category(their_priority) | ||
| their_swap_item = other_vendor.get_best_by_category(my_priority) | ||
|
|
||
| if not my_swap_item or not their_swap_item: | ||
| return False | ||
| return self.swap_items(other_vendor, my_swap_item, their_swap_item) |
| my_swap_item = self.get_newest() | ||
| their_swap_item = other_vendor.get_newest() |
| assert len(fatimah.inventory) == 3 | ||
| assert len(jolie.inventory) == 0 |
There was a problem hiding this comment.
How could we alter these to make sure they check for the correct data structure as well.
| assert tai.inventory == [item_a, item_b, item_c] | ||
| assert jesse.inventory == [item_d, item_e, item_f] |
| assert len(tai.inventory) == 3 | ||
| assert len(jesse.inventory) == 3 | ||
| assert tai.inventory == [item_c, item_b, item_f] | ||
| assert jesse.inventory == [item_e, item_d, item_a] |
No description provided.