Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions swap_meet/clothing.py
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."
14 changes: 12 additions & 2 deletions swap_meet/decor.py
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."
14 changes: 12 additions & 2 deletions swap_meet/electronics.py
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__()
Copy link
Copy Markdown

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 👍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

return f"{item_message}. This is a {self.type} device."

28 changes: 27 additions & 1 deletion swap_meet/item.py
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)]


121 changes: 120 additions & 1 deletion swap_meet/vendor.py
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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 or statement.

if (my_item not in self.inventory 
        or their_item not in other_vendor.inventory):
    return False

The 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to consider about using swap_items is that the time complexity winds up being O(n) due to removing the items from the inventory. If we were in a different scenario where it was important to lower the time complexity and the order that we add items to the inventory didn't matter, we could swap the items in place to have O(1) runtime with something like:

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of max with a lambda!

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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]}")'''
2 changes: 1 addition & 1 deletion tests/integration_tests/test_wave_01_02_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from swap_meet.vendor import Vendor
from swap_meet.item import Item

@pytest.mark.skip
#@pytest.mark.skip
@pytest.mark.integration_test
def test_integration_wave_01_02_03():
# make a vendor
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/test_wave_04_05_06_07.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from swap_meet.decor import Decor
from swap_meet.electronics import Electronics

@pytest.mark.skip
#@pytest.mark.skip
@pytest.mark.integration_test
def test_integration_wave_04_05_06(capfd):
camila = Vendor()
Expand Down
21 changes: 14 additions & 7 deletions tests/unit_tests/test_wave_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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(
Expand All @@ -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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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
Thank you.

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 **********
# *********************************************************************
27 changes: 21 additions & 6 deletions tests/unit_tests/test_wave_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@
from swap_meet.vendor import Vendor
from swap_meet.item import Item

@pytest.mark.skip
#@pytest.mark.skip
def test_items_have_default_uuid_length_id():
item = Item()
assert isinstance(item.id, int)
assert len(str(item.id)) >= 32

@pytest.mark.skip

#@pytest.mark.skip
def test_item_instances_have_different_default_ids():
item_a = Item()
item_b = Item()
assert item_a.id != item_b.id

@pytest.mark.skip
#@pytest.mark.skip
def test_items_use_custom_id_if_passed():
item = Item(id=12345)
assert isinstance(item.id, int)
assert item.id == 12345

@pytest.mark.skip
#@pytest.mark.skip
def test_item_obj_returns_text_item_for_category():
item = Item()
assert item.get_category() == "Item"

@pytest.mark.skip
#@pytest.mark.skip
def test_get_item_by_id():
test_id = 12345
item_custom_id = Item(id=test_id)
Expand All @@ -35,8 +36,21 @@ def test_get_item_by_id():

result_item = vendor.get_by_id(test_id)
assert result_item is item_custom_id
######### New Test for Code coverage
#@pytest.mark.skip
def test_get_by_id_for_none_id():
#Arrange

item_with_no_id = Item()
vendor = Vendor(
inventory=[Item(),Item(),item_with_no_id]
)
result_item = vendor.get_by_id(None)
#Assert
assert result_item == None


@pytest.mark.skip
#@pytest.mark.skip
def test_get_item_by_id_no_matching():
test_id = 12345
item_a = Item()
Expand All @@ -47,6 +61,7 @@ def test_get_item_by_id_no_matching():
)

result_item = vendor.get_by_id(test_id)

assert result_item is None

items = vendor.inventory
Expand Down
Loading