-
Notifications
You must be signed in to change notification settings - Fork 21
Ocelots - Kelly T #21
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
41d3eb7
c5e096b
01aaf3a
30843af
fe5291b
cfc62db
05f3a1d
b862313
2c0889f
222a77d
528e028
a7f6cff
06a7593
62405d6
0ba12e1
5aa6d0b
f9f3cc9
09c1229
8149e52
f86ca51
3a0f2c0
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,18 @@ | ||
| class Clothing: | ||
| pass | ||
| from swap_meet.item import Item | ||
|
|
||
| class Clothing(Item): | ||
| """Clothing class inherits id and condition from Item class and adds the | ||
| fabric attribute. | ||
| """ | ||
|
|
||
| def __init__(self, fabric = "Unknown", id = None, condition = 0): | ||
| super().__init__(id, condition) | ||
| self.fabric = fabric | ||
|
|
||
| def get_attribute(self): | ||
|
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. I fully see what you were doing with |
||
| return self.fabric | ||
|
|
||
| def __str__(self): | ||
| summary = super().__str__() | ||
| class_summary = f"It is made from {self.fabric} fabric." | ||
| return ". ".join((summary, class_summary)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,20 @@ | ||
| class Decor: | ||
| pass | ||
| from swap_meet.item import Item | ||
|
|
||
| class Decor(Item): | ||
| """Decor class inherits id and condition from Item class and adds the | ||
| width and length attributes. | ||
| """ | ||
|
|
||
| def __init__(self, id = None, width = 0, length = 0, condition = 0): | ||
| super().__init__(id, condition) | ||
| self.width = width | ||
| self.length = length | ||
|
|
||
| def get_attribute(self): | ||
| item_size = self.width * self.length | ||
| return item_size | ||
|
|
||
| def __str__(self): | ||
| summary = super().__str__() | ||
| class_summary = f"It takes up a {self.width} by {self.length} sized space." | ||
| return ". ".join((summary, class_summary)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,18 @@ | ||
| class Electronics: | ||
| pass | ||
| from swap_meet.item import Item | ||
|
|
||
| class Electronics(Item): | ||
| """Electronics class inherits id and condition from Item class and adds the | ||
| type attribute. | ||
| """ | ||
|
|
||
| def __init__(self, id = None, type = "Unknown", condition = 0): | ||
| super().__init__(id, condition) | ||
| self.type = type | ||
|
|
||
| def get_attribute(self): | ||
| return self.type | ||
|
|
||
| def __str__(self): | ||
| summary = super().__str__() | ||
| class_summary = f"This is a {self.type} device." | ||
| return ". ".join((summary, class_summary)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,31 @@ | ||
| import uuid | ||
|
|
||
| class Item: | ||
| pass | ||
| """Item class has a unique id and a condition.""" | ||
|
|
||
| def __init__(self, id = None, condition = 0): | ||
| if id is not None: | ||
| if type(id) is not int: | ||
| raise ValueError("invalid id input") | ||
| else: | ||
| self.id = id | ||
| else: | ||
| self.id = uuid.uuid1().int | ||
|
Comment on lines
+7
to
+13
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. I think we could simplify the if/else tree a little by combining our validation checks: if id is not None and type(id) is not int:
raise ValueError("invalid id input")
self.id = id if id else uuid.uuid1().int |
||
| self.condition = condition | ||
|
|
||
| def get_category(self): | ||
| return type(self).__name__ | ||
|
|
||
| def condition_description(self): | ||
| switch = { | ||
| 0 : "gross", | ||
| 1 : "not good", | ||
| 2 : "okay", | ||
| 3 : "good", | ||
| 4 : "not bad", | ||
| 5 : "excellent" | ||
| } | ||
| return switch.get(self.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. Nice handling for condition description! It looks like the code assumes that |
||
|
|
||
| def __str__(self): | ||
| return f"An object of type {self.get_category()} with id {self.id}" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,160 @@ | ||
| class Vendor: | ||
| pass | ||
| """Vendor class contains an inventory list of Item objects and methods to | ||
| add, remove, retrieve, and swap items with other vendor instances. | ||
| """ | ||
|
|
||
| def __init__(self, inventory = None): | ||
| """Creates Vendor instance""" | ||
|
|
||
| self.inventory = inventory if inventory is not None else [] | ||
|
|
||
| def add(self, item): | ||
| """Adds items to inventory. Returns item.""" | ||
|
|
||
| self.inventory.append(item) | ||
| return item | ||
|
|
||
| def remove(self, item): | ||
| """Removes item from inventory. Returns removed item or None if item | ||
| not found. | ||
| """ | ||
|
|
||
| if item in self.inventory: | ||
| self.inventory.remove(item) | ||
| return item | ||
| return None | ||
|
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. I like the explicit return for |
||
|
|
||
| def get_by_id(self, id): | ||
| """Gets item by item id from list of inventory. Returns Item object.""" | ||
|
|
||
| item = next((item for item in self.inventory if item.id == id), None) | ||
|
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 |
||
| return item | ||
|
|
||
| def get_by_category(self, category): | ||
| """Gets items in inventory that match category. Returns a list.""" | ||
|
|
||
| inventory = self.inventory | ||
| items = [item for item in inventory if item.get_category() == category] | ||
|
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 a list comprehension! |
||
| return items | ||
|
|
||
| def get_best_by_category(self, category): | ||
| """Gets item with the best condition in category from inventory. Returns | ||
| Item object. | ||
| """ | ||
|
|
||
| items = self.get_by_category(category) | ||
|
|
||
| if not items: | ||
| return None | ||
|
|
||
| max_value = max([item.condition for item in items]) | ||
|
|
||
| item = next( | ||
| (item for item in items if item.condition == max_value), None) | ||
|
Comment on lines
+50
to
+53
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. I love the idea to use return max(items, key=lambda item: item.condition) |
||
|
|
||
| return item | ||
|
|
||
| def swap_best_by_category(self, other_vendor, my_priority, their_priority): | ||
| """Swaps best items in category from two vendors. Returns boolean.""" | ||
|
|
||
| my_item = self.get_best_by_category(their_priority) | ||
| their_item = other_vendor.get_best_by_category(my_priority) | ||
|
Comment on lines
+60
to
+61
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. These variable names are solid, and we could be even more descriptive. Using line 60 as an example, some other options could be |
||
|
|
||
| if not their_item or not my_item: | ||
| return False | ||
|
|
||
| swapped = self.swap_items(other_vendor, my_item, their_item) | ||
| return swapped | ||
|
|
||
| def get_by_category_attribute(self, category, attribute): | ||
| """Gets items from inventory that match category and item attribute. | ||
| Returns a list. | ||
| """ | ||
|
|
||
| category_items = self.get_by_category(category) | ||
| items = [item for item in category_items | ||
| if attribute == item.get_attribute()] | ||
|
|
||
| return items | ||
|
|
||
| def swap_by_attribute(self, other_vendor, category, attribute): | ||
| """Swaps items that match category and item attribute from 2 vendors. | ||
| Returns boolean. | ||
| """ | ||
|
|
||
| my_items = self.get_by_category_attribute(category, attribute) | ||
| their_items = other_vendor.get_by_category_attribute( | ||
| category, attribute) | ||
|
|
||
| if not their_items or not my_items: | ||
| return False | ||
|
|
||
| my_item = my_items[0] | ||
| their_item = their_items[0] | ||
| swapped = self.swap_items(other_vendor, my_item, their_item) | ||
| return swapped | ||
|
|
||
| def swap_items(self, other_vendor, my_item, their_item): | ||
| """Swaps items from 2 vendors. Returns boolean.""" | ||
|
|
||
| inventory = self.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. I think this variable could be removed, I see it referenced on the line below, but not after that. |
||
| if my_item not in 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.inventory.remove(my_item) | ||
| other_vendor.inventory.append(my_item) | ||
|
|
||
| other_vendor.inventory.remove(their_item) | ||
| self.inventory.append(their_item) | ||
|
|
||
| return True | ||
|
|
||
| def swap_first_item(self, other_vendor): | ||
| """Swaps first item in inventory from 2 vendors. Returns boolean.""" | ||
|
|
||
| if not self.inventory or not other_vendor.inventory: | ||
| return False | ||
|
|
||
| my_item = self.inventory[0] | ||
| their_item = other_vendor.inventory[0] | ||
| swapped = self.swap_items(other_vendor, my_item, their_item) | ||
|
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 swapped | ||
|
|
||
| def swap_by_id(self, other_vendor, my_item_id, their_item_id): | ||
| """Swaps items by id from 2 vendors. Returns boolean.""" | ||
|
|
||
| my_item = self.get_by_id(my_item_id) | ||
| their_item = other_vendor.get_by_id(their_item_id) | ||
|
|
||
| if not their_item or not my_item: | ||
| return False | ||
|
|
||
| swapped = self.swap_items(other_vendor, my_item, their_item) | ||
| return swapped | ||
|
|
||
| def choose_and_swap_items(self, other_vendor, category = ""): | ||
| """Displays inventory from 2 vendors and allows user to select items to | ||
| swap by id. Returns boolean. | ||
| """ | ||
|
|
||
| self.display_inventory(category) | ||
| other_vendor.display_inventory(category) | ||
|
|
||
| my_item_id = int(input("Enter item from your inventory by id: ")) | ||
| their_item_id = int(input("Enter item from their inventory by id: ")) | ||
| swapped = self.swap_by_id(other_vendor, my_item_id, their_item_id) | ||
| return swapped | ||
|
|
||
| def display_inventory(self, category = ""): | ||
| """Displays Vendor inventory with item descriptions.""" | ||
|
|
||
| if not self.inventory: | ||
| print("No inventory to display.") | ||
| return | ||
|
|
||
| items = self.get_by_category(category) if category else self.inventory | ||
| if items: | ||
| for i, item in enumerate(items, 1): | ||
|
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 for enumerate! |
||
| print(f"{i}. {str(item)}") | ||
|
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. Because we have implemented the print(f"{i}. {item}") |
||
| else: | ||
| print("No inventory to display.") | ||
| 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,7 +40,7 @@ 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( | ||
|
|
@@ -49,7 +49,10 @@ def test_removing_not_found_returns_none(): | |
|
|
||
| result = vendor.remove(item) | ||
|
|
||
| raise Exception("Complete this test according to comments below.") | ||
| # raise Exception("Complete this test according to comments below.") | ||
| # ********************************************************************* | ||
| # ****** Complete Assert Portion of this test ********** | ||
| # ********************************************************************* | ||
| assert len(vendor.inventory) == 3 | ||
| assert item not in vendor.inventory | ||
| assert result is None | ||
|
Comment on lines
+56
to
+58
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. |
||
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.
Nice use of default values.