C22: Leidy (Sprinx) and Tram (Phoenix)#18
Conversation
Co-authored-by: tramhn001 <tramhn001@users.noreply.github.com>
| def __str__(self): | ||
| return f"An object of type {self.__class__.__name__} with id {self.id}. "\ | ||
| f"It is made from {self.fabric} fabric." |
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."| self.width = 0 if width is None else width | ||
| self.length = 0 if length is None else length |
There was a problem hiding this comment.
Since integers are not mutable datatypes we don't need to take the same precautions as we do with mutable datatypes like lists. This is because a new 0 would be created in memory. So we could have the following:
def __init__(self, id=None, width=0, length=0, condition=None, age=None):
super().__init__(id, condition, age)
self.width = width
self.length = length| class Item: | ||
| pass No newline at end of file | ||
| def __init__(self, id=None, condition=None, age= None): | ||
| self.id = id if id is not None else uuid.uuid4().int |
| def condition_description(self): | ||
|
|
||
| if self.condition > 0 and self.condition < 1: | ||
| return "It's barely holding together." | ||
| elif self.condition >= 1 and self.condition < 2: | ||
| return "This item has seen better days." | ||
| elif self.condition >= 2 and self.condition < 3: | ||
| return "It is a bit worn, but still has life left." | ||
| elif self.condition >= 3 and self.condition < 4: | ||
| return "In good condition." | ||
| elif self.condition >= 4 and self.condition < 5: | ||
| return "Almost like new!" | ||
| return "Mint condition." |
There was a problem hiding this comment.
This could also be a time to use match case!
def condition_description(self):
match self.condition:
case 0:
return "Throw Away"
case 1:
return "Eh"
case 2:
return "Better..."
case 3:
return "..."
case 4:
return "Okay?!"
case 5:
return "Yes!"| class Vendor: | ||
| pass No newline at end of file | ||
| def __init__(self, inventory=None): | ||
| self.inventory = [] if inventory is None else inventory |
|
|
||
| def swap_items(self, other_vendor, my_item, their_item): | ||
|
|
||
| if not my_item 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?
| self.inventory[0] = their_first_item | ||
| other_vendor.inventory[0] = my_first_item |
There was a problem hiding this comment.
A perfect case to use the swap_items method
| category_list = [] | ||
| for item in self.inventory: | ||
| if item.get_category() == category: | ||
| category_list.append(item) |
There was a problem hiding this comment.
How would this look as list comprehension?
| return category_list | ||
|
|
||
| def get_best_by_category(self, category): | ||
| category_list = self.get_by_category(category) |
| best_item = None | ||
| best_item_condition = -1 | ||
|
|
||
| for item in category_list: | ||
| if item.condition > best_item_condition: | ||
| best_item_condition = item.condition | ||
| best_item = item |
There was a problem hiding this comment.
Great work here! Could we also use the dot (.) operator here to check the best_item's condition?
| vendor_best_item = self.get_best_by_category(their_priority) | ||
| other_vendor_best_item = other_vendor.get_best_by_category(my_priority) | ||
|
|
||
| if not vendor_best_item or not other_vendor_best_item: | ||
| return False | ||
|
|
||
| self.swap_items(other_vendor,vendor_best_item,other_vendor_best_item) |
| for item in self.inventory: | ||
| if item.age < my_newest_item_age: | ||
| my_newest_item_age = item.age | ||
| my_newest_item = item | ||
|
|
||
| # Find the newest item in the other vendor's inventory | ||
| for item in other_vendor.inventory: | ||
| if item.age < their_newest_item_age: | ||
| their_newest_item_age = item.age | ||
| their_newest_item = item |
There was a problem hiding this comment.
Since we are essentially doing the same thing here, we could make this into a method on that all instances will have access to. That way we DRY up our code some.
| def test_removing_not_found_is_false(): | ||
| item = "item to remove" | ||
| item = "c" | ||
| vendor = Vendor( | ||
| inventory=["a", "b", "c"] | ||
| ) | ||
|
|
||
| result = vendor.remove(item) | ||
| vendor_length = len(vendor.inventory) | ||
|
|
||
| assert result == "c" |
There was a problem hiding this comment.
This test should look to see if your function returns False if it looks to remove an item that is not in the inventory of a given vendor. How could we change it to implement that logic?
|
|
||
| raise Exception("Complete this test according to comments below.") | ||
| assert len(fatimah.inventory) == 3 | ||
| assert len(jolie.inventory) == 0 |
There was a problem hiding this comment.
assert jolie.inventory == [] would ensure that the inventory is an empty list.
| assert item_f in tai.inventory | ||
| assert item_c in jesse.inventory |
There was a problem hiding this comment.
We could use a for loop to check to see if all the items are in their respective inventories.
| assert item_a or item_b or item_c not in jesse.inventory | ||
| assert item_d or item_e or item_f not in tai.inventory |
There was a problem hiding this comment.
These check to make sure the items aren't in the wrong list, but we would still need to check to see if they are in the right list.
| assert len(jesse.inventory) == 0 | ||
| assert item_a in tai.inventory | ||
| assert item_b in tai.inventory | ||
| assert item_c in tai.inventory No newline at end of file |
No description provided.