Sphinx C22 - Aleida Vieyra and Tami Gaertner#24
Conversation
…onditional_dict file for the condition_description in item, cleaned up code.
… due to redundancy
| def get_category(self): | ||
| return "Clothing" |
There was a problem hiding this comment.
For our children classes of Item, we have a get_category method that essentially returns the name of the class. Though we are returning the different names (Clothing, Decor, Electronic) we could actually move this functionality into the parent class, Item. This is how it would look:
class Item:
...
def get_category(self):
return self.__class__.__name__self refers to the class instance that called it. Meaning if we have the following:
shirt = Clothing()
shirt.get_category() # returns "Clothing"self in this case refers to shirt. The class of clothing is Clothing and since Clothing is inheriting from the Item class it has access to the get_category method. This method is inherently being passed shirt and then accesses the __class__ attribute and then the __name__ attribute which gives us Clothing. Doing this will allow to make our code more DRY since we do this for Clothing, Decor, and Electronic.
| def __str__(self): | ||
| return f"An object of type Clothing with id {self.id}. 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.__class__.__name__)}" \
f" with id {self.id}."
class Clothing(Item):
...
def __str__(self):
return f"{super().__str__()} It is made " \
f"from {self.fabric} fabric."| condition_table = { | ||
| 5: "Brand New", | ||
| 4: "Used - Like New/ Open Box/ Without Tags", | ||
| 3: "Used - Good", | ||
| 2: "Used - Fair", | ||
| 1: "Used - Poor", | ||
| 0: "For Parts - Not Working" | ||
| } No newline at end of file |
There was a problem hiding this comment.
How love you used a dictionary here! 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!"| ''' | ||
|
|
||
| def __init__(self, id=None, condition=0): | ||
| self.id = uuid.uuid4().int if id == None else id |
| Returns a description of the items condition based on its condition value. | ||
| ''' | ||
| if self.condition in condition_table.keys(): | ||
| return condition_table[int(self.condition)] |
There was a problem hiding this comment.
Nice typecasting here since floats are passed in inside of the tests!
| self.inventory.remove(item) | ||
| return item | ||
|
|
||
| def get_by_id(self, vendor_id): |
There was a problem hiding this comment.
I would maybe change this to item_id since we are looking for a specific item by id not necessarily a vendor.
| Returns None if no matching item is found. | ||
| ''' | ||
|
|
||
| for object in self.inventory: |
There was a problem hiding this comment.
Maybe the name item would be more declarative here?
| Returns: bool: True if the swap was successful, False otherwise. | ||
| ''' | ||
|
|
||
| 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.
| self.remove(my_item) | ||
| other_vendor.remove(their_item) | ||
| self.add(their_item) | ||
| other_vendor.add(my_item) |
| Returns a list of items in the inventory matching the given category. | ||
| ''' | ||
|
|
||
| category_objects = [object for object in self.inventory if category == object.get_category()] |
| def get_best_by_category(self, category_best): | ||
| ''' | ||
| Returns the item with the highest condition in the given category. | ||
| ''' | ||
|
|
||
| category_inventory = self.get_by_category(category_best) | ||
|
|
||
| if not category_inventory: | ||
| return None | ||
|
|
||
| best_condition_object = category_inventory[0] | ||
|
|
||
| for object in category_inventory: | ||
| if object.condition > best_condition_object.condition: | ||
| best_condition_object = object | ||
| return best_condition_object |
| if not self.inventory or not other_vendor.inventory: | ||
| return False | ||
|
|
||
| my_best_item = self.get_best_by_category(their_priority) | ||
| their_best_item = other_vendor.get_best_by_category(my_priority) |
There was a problem hiding this comment.
We could also write this in the following manner:
my_best = self.get_best_by_category(their_priority)
their_best = other_vendor.get_best_by_category(my_priority)
if not my_best or not their_best:
return False
self.swap_items(other_vendor, my_best, their_best)
return True| assert result == False | ||
| assert len(vendor.inventory) == 3 | ||
| assert item not in vendor.inventory | ||
| assert "a" in vendor.inventory | ||
| assert "b" in vendor.inventory | ||
| assert "c" in vendor.inventory |
| # ********************************************************************* | ||
| # ****** Complete Assert Portion of this test ********** | ||
| assert result == False | ||
| 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_a in fatimah.inventory | ||
| assert item_b in fatimah.inventory | ||
| assert item_c in fatimah.inventory |
| assert len(tai.inventory)==3 | ||
| assert len(jesse.inventory)==3 |
There was a problem hiding this comment.
Don't forget your spacing between operators
No description provided.