Ruby_CarolynQi#82
Conversation
… match Lecture Notes.
… in other functions
| # ********************************************************************* | ||
| assert len(vendor.inventory) == 3 | ||
| assert item not in vendor.inventory | ||
| assert not result No newline at end of file |
There was a problem hiding this comment.
You could do assert result is False to make sure that an actual False comes out of the function instead of just a falsey value
| if not id: | ||
| id = uuid.uuid4().int | ||
| self.id = id |
There was a problem hiding this comment.
A ternary is also fine here:
self.id = id if id else uuid.uuid4().int| return f"An object of type {self.get_category()} with id {self.id}." | ||
|
|
||
| def get_category(self): | ||
| return f"{self.__class__.__name__}" |
There was a problem hiding this comment.
[nit] __name__ already returns a string, so you don't need the f string.
| return f"{self.__class__.__name__}" | |
| return self.__class__.__name__ |
| return f"{self.__class__.__name__}" | ||
|
|
||
| def condition_description(self): | ||
| return str(self.condition) No newline at end of file |
There was a problem hiding this comment.
The requirements ask you to create a descriptive condition, rather than just returning the stringified number:
These can be basic descriptions (eg. 'mint', 'heavily used') but feel free to have fun with these (e.g. 'You probably want a glove for this one...").
|
|
||
| def get_first_item(self): | ||
| if not self.inventory: | ||
| return False |
There was a problem hiding this comment.
[opinion] I'd pick return None instead of return False when there is no first item to return.
| def swap_first_item(self, other_vendor): | ||
| my_first_item = self.get_first_item() | ||
| other_first_item = other_vendor.get_first_item() | ||
| return my_first_item and other_first_item and self.swap_items(other_vendor, my_first_item, other_first_item) |
There was a problem hiding this comment.
swap_items will do the right thing with None values, so you can get away with
return self.swap_items(other_vendor, my_first_item, other_first_item)| items_match_category = [] | ||
| for item in self.inventory: | ||
| if item.get_category() == category: | ||
| items_match_category.append(item) | ||
| return items_match_category |
There was a problem hiding this comment.
Notice this pattern of:
result_list = []
for element in source_list:
if some_condition(element):
result_list.append(element)can be rewritten using a list comprehension as:
result_list = [element for element in source_list if some_condition(element)]Which here would look like:
items_match_category = [item for item in self.inventory if item.get_category() == category]
return items_match_categoryAt first, this may seem more difficult to read, but comprehensions are a very common python style, so I encourage y’all to try working with them!
| best_condition = 0.0 | ||
| best_item = None | ||
| for item in items_match_category: | ||
| if float(item.condition_description()) > best_condition: | ||
| best_condition = float(item.condition_description()) | ||
| best_item = item | ||
| return best_item |
There was a problem hiding this comment.
You can simplify this by using item.condition directly instead of turning condition_description() into a float. You can also use max to your advantage:
if not items_match_category:
return None
return max(items_match_category, key=lambda item: item.condition)| newest_item_age = self.inventory[0].age | ||
| newest_item = self.inventory[0] | ||
| for item in self.inventory: | ||
| if item.age < newest_item_age: | ||
| newest_item_age = item.age | ||
| newest_item = item | ||
| return newest_item |
There was a problem hiding this comment.
I think you could use min here
return min(self.inventory, lambda item: item.age)But I wasn't able to check since you didn't write tests for the age optional enhancement. 😉
No description provided.