-
Notifications
You must be signed in to change notification settings - Fork 21
Ada AC2 - Supriya Chavan #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2824370
cea99d8
7536b12
98916db
88d8009
b4e7080
56c4469
587a9c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,11 @@ | ||
| class Clothing: | ||
| pass | ||
| # Wave 5 | ||
| import uuid | ||
| from swap_meet.item import Item | ||
| class Clothing(Item): | ||
| def __init__(self, id = None, category = "", condition = 0, fabric = "Unknown"): | ||
| super().__init__(id, category, condition) | ||
| self.fabric = fabric | ||
|
|
||
| def __str__(self): | ||
| item_message = super().__str__() | ||
| return f"{item_message}. It is made from {self.fabric} fabric." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,12 @@ | ||
| class Decor: | ||
| pass | ||
| # Wave 5 | ||
| import uuid | ||
| from swap_meet.item import Item | ||
| class Decor(Item): | ||
| def __init__(self, id = None, category = "", condition = 0, width = 0, length = 0): | ||
| super().__init__(id, category, condition) | ||
| self.width = width | ||
| self.length = length | ||
|
|
||
| def __str__(self): | ||
| item_message = super().__str__() | ||
| return f"{item_message}. It takes up a {self.width} by {self.length} sized space." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,12 @@ | ||
| class Electronics: | ||
| pass | ||
| # Wave 5 | ||
| import uuid | ||
| from swap_meet.item import Item | ||
| class Electronics(Item): | ||
| def __init__(self, id = None, category = "", condition = 0, type = "Unknown"): | ||
| super().__init__(id, category, condition) | ||
| self.type = type | ||
|
|
||
| def __str__(self): | ||
| item_message = super().__str__() | ||
| return f"{item_message}. This is a {self.type} device." | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,28 @@ | ||
| #Wave 2 , 5 | ||
| from math import floor | ||
| import uuid | ||
| #from swap_meet.vendor import Vendor | ||
| class Item: | ||
| pass | ||
| def __init__(self, id = None, category = "", condition = 0): | ||
|
|
||
| if id is not None and not isinstance(id,int): | ||
| raise ValueError("ID must be an integer") | ||
| self.id = id if id else uuid.uuid4().int | ||
| self.condition = condition | ||
| self.category = category | ||
|
|
||
| def get_category(self): | ||
| return self.__class__.__name__#type(self).__name__ I would like to know which is best practice in these two | ||
|
|
||
|
|
||
| # Wave 3 | ||
| def __str__(self): | ||
| return f"An object of type {self.get_category()} with id {self.id}" | ||
|
|
||
| # Wave 5 | ||
| def condition_description(self): | ||
| description_of_condition = ["Mint","Heavily Used","Like New","Gently Used","Best Used","New"] | ||
|
|
||
| return description_of_condition[round(self.condition)] | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,121 @@ | ||
| #Wave 1 | ||
|
|
||
| class Vendor: | ||
| pass | ||
| def __init__(self, inventory = None): | ||
| if inventory is not None: | ||
| self.inventory = inventory | ||
| else: | ||
| self.inventory = [] | ||
|
|
||
| def add(self, new_item): | ||
| self.inventory.append(new_item) | ||
| return new_item | ||
|
|
||
| def remove(self, item_to_remove): | ||
| if item_to_remove in self.inventory: | ||
| self.inventory.remove(item_to_remove) | ||
| return item_to_remove | ||
| return None | ||
|
|
||
| def get_by_id(self, id): | ||
| if id is None: | ||
| return None | ||
| for item in self.inventory: | ||
| if item.id == id: | ||
| return item | ||
| return None | ||
|
|
||
| # Wave 3 | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With indentation this line is a little long, I would consider breaking it up at the if (my_item not in self.inventory
or their_item not in other_vendor.inventory):
return FalseThe PEP8 style guide doesn't give a strict recommendation on how to break an if-statement across multiple lines, but they give a few suggestions in the second half of the section on indentation: https://peps.python.org/pep-0008/#indentation |
||
| return False | ||
| self.remove(my_item) | ||
| other_vendor.add(my_item) | ||
| other_vendor.remove(their_item) | ||
|
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of the Vendor methods 🙂 |
||
| self.add(their_item) | ||
| return True | ||
|
|
||
| # Wave 4 | ||
| def swap_first_item(self, other_vendor): | ||
| if not self.inventory or not other_vendor.inventory: | ||
| return False | ||
| self.swap_items(other_vendor,self.inventory[0],other_vendor.inventory[0]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something to consider about using self.inventory[0], other_vendor.inventory[0] = other_vendor.inventory[0], self.inventory[0] |
||
| return True | ||
|
|
||
| # Wave 6 | ||
| def get_by_category(self, category): | ||
| if not self.inventory: | ||
| return [] | ||
| list_items_by_category = [] | ||
|
|
||
| for item in self.inventory: | ||
| if item.get_category() == category: | ||
| list_items_by_category.append(item) | ||
| return list_items_by_category | ||
|
Comment on lines
+49
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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: list_items_by_category = [item for item in self.inventory if item.get_category() == category]At first, this may seem more difficult to read, but comprehensions are a very common python style, so I encourage y’all to practice working with them! |
||
|
|
||
| def get_best_by_category(self, category): | ||
| list_of_items_in_category = self.get_by_category(category) | ||
| if not list_of_items_in_category: | ||
| return None | ||
| best_item = max(list_of_items_in_category,key = lambda item: item.condition) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of |
||
| return best_item | ||
|
|
||
| def swap_best_by_category(self, other_vendor, my_priority, their_priority): | ||
| best_item_from_my_inventory = self.get_best_by_category(their_priority) | ||
| best_item_from_their_inventory = other_vendor.get_best_by_category(my_priority) | ||
|
Comment on lines
+64
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Loving the descriptive variable names 😊 |
||
| if not best_item_from_my_inventory or not best_item_from_their_inventory: | ||
| return False | ||
| self.swap_items(other_vendor,best_item_from_my_inventory,best_item_from_their_inventory) | ||
| return True | ||
|
|
||
| #Wave 7 | ||
| def display_inventory(self, category = ""): | ||
| if self.inventory == []: | ||
| print("No inventory to display.") | ||
| return | ||
| display_items = self.get_by_category(category) if category else self.inventory | ||
|
|
||
| if not display_items: | ||
| print("No inventory to display.") | ||
| return | ||
| else: | ||
| for index, item in enumerate(display_items, start=1): | ||
| print(f"{index}. {item}") | ||
| return | ||
|
|
||
| def swap_by_id(self, other_vendor, my_item_id = None, their_item_id = None): | ||
| my_item = self.get_by_id(my_item_id) | ||
| vendor_item = other_vendor.get_by_id(their_item_id) | ||
| if not my_item or not vendor_item: | ||
| return False | ||
| result = self.swap_items(other_vendor, my_item, vendor_item) | ||
| return result | ||
|
|
||
| def choose_and_swap_items(self, other_vendor, category = ""): | ||
| if not category: | ||
| self.display_inventory() | ||
| other_vendor.display_inventory() | ||
| else: | ||
| self.display_inventory(category) | ||
| other_vendor.display_inventory(category) | ||
| my_item_id = int(input("Enter Id of the item you want to swap")) | ||
| vendor_item_id = int(input("Enter Id of your friend vendor's item you want to swap with")) | ||
| if not self.get_by_id(my_item_id) or not other_vendor.get_by_id(vendor_item_id): | ||
| return False | ||
| self.swap_by_id(other_vendor,my_item_id,vendor_item_id) | ||
| return True | ||
| ''' | ||
| for index in range(len(display_items)): | ||
| print(f"{index +1}. {display_items[index]}") | ||
| return | ||
|
|
||
| if category: | ||
| inventory_items_by_category = self.get_by_category(category) | ||
| if inventory_items_by_category: | ||
| for index in range(len(inventory_items_by_category)): | ||
| print(f"{index +1}. {inventory_items_by_category[index]}") | ||
| else: | ||
| print("No inventory to display.") | ||
| else: | ||
| for index in range(len(self.inventory)): | ||
| print(f"{index +1}. {self.inventory[index]}")''' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,12 @@ | |
| import pytest | ||
| from swap_meet.vendor import Vendor | ||
|
|
||
| @pytest.mark.skip | ||
| #@pytest.mark.skip | ||
| def test_vendor_has_inventory(): | ||
| vendor = Vendor() | ||
| assert len(vendor.inventory) == 0 | ||
|
|
||
| @pytest.mark.skip | ||
| #@pytest.mark.skip | ||
| def test_vendor_takes_optional_inventory(): | ||
| inventory = ["a", "b", "c"] | ||
| vendor = Vendor(inventory=inventory) | ||
|
|
@@ -16,7 +16,7 @@ def test_vendor_takes_optional_inventory(): | |
| assert "b" in vendor.inventory | ||
| assert "c" in vendor.inventory | ||
|
|
||
| @pytest.mark.skip | ||
| #@pytest.mark.skip | ||
| def test_adding_to_inventory(): | ||
| vendor = Vendor() | ||
| item = "new item" | ||
|
|
@@ -27,7 +27,7 @@ def test_adding_to_inventory(): | |
| assert item in vendor.inventory | ||
| assert result == item | ||
|
|
||
| @pytest.mark.skip | ||
| #@pytest.mark.skip | ||
| def test_removing_from_inventory_returns_item(): | ||
| item = "item to remove" | ||
| vendor = Vendor( | ||
|
|
@@ -40,16 +40,23 @@ def test_removing_from_inventory_returns_item(): | |
| assert item not in vendor.inventory | ||
| assert result == item | ||
|
|
||
| @pytest.mark.skip | ||
| #@pytest.mark.skip | ||
| def test_removing_not_found_returns_none(): | ||
| item = "item to remove" | ||
| vendor = Vendor( | ||
| inventory=["a", "b", "c"] | ||
| ) | ||
|
|
||
| result = vendor.remove(item) | ||
|
|
||
| raise Exception("Complete this test according to comments below.") | ||
| #assert | ||
| assert result == None | ||
| assert item not in vendor.inventory | ||
| assert len(vendor.inventory) == 3 | ||
|
Comment on lines
+52
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great assertions, I would also suggest checking the inventory contents, since checks like the length being correct do not guarantee that the individual elements are still what we expect.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added these three assertions |
||
| assert "a" in vendor.inventory | ||
| assert "b" in vendor.inventory | ||
| assert "c" in vendor.inventory | ||
|
|
||
| #raise Exception("Complete this test according to comments below.") | ||
| # ********************************************************************* | ||
| # ****** Complete Assert Portion of this test ********** | ||
| # ********************************************************************* | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice use of inheritance across the child classes 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you.