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
from swap_meet.item import Item
class Clothing(Item):
def __init__(self, id=None, fabric="Unknown", condition=0):
super().__init__(id, condition)
self.fabric = fabric if fabric else "Unknown"

def get_category(self):
return f"Clothing"

def __str__(self):
return f"An object of type {self.get_category()} with id {self.id}. 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
from swap_meet.item import Item
class Decor(Item):
def __init__(self, id=None, width=0, length=0, condition=0):
super().__init__(id, condition)
self.width = width if width else 0

Choose a reason for hiding this comment

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

nice use of ternary operators

self.length = length if length else 0

def get_category(self):
return f"Decor"

def __str__(self):
return f"An object of type {self.get_category()} with id {self.id}. It takes up a {self.width} by {self.length} sized space."
13 changes: 11 additions & 2 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
class Electronics:
pass
from swap_meet.item import Item
class Electronics(Item):
def __init__(self, id=None, type="Unknown", condition=0):
super().__init__(id, condition)
self.type = type if type else "Unknown"

def get_category(self):
return f"Electronics"

def __str__(self):
return f"An object of type {self.get_category()} with id {self.id}. This is a {self.type} device."
31 changes: 30 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
import uuid
class Item:
pass
def __init__(self, id=None, condition=0):
unique_identifier = uuid.uuid4()
self.id = id if id else unique_identifier.int
if not isinstance(self.id, int):
raise TypeError("Item id should be an int type object")
self.condition = condition if condition else 0

def get_category(self):
return self.__class__.__name__

def __str__(self):
return f"An object of type {self.get_category()} with id {self.id}"

def condition_description(self):

Choose a reason for hiding this comment

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

Well organized code! At this point, you might see that a dict could be an alternative. And, has O(1) lookup time. In the worse case, it would take O(n) -- for n conditions -- to fall through to the last condition case with the if/else structure. :)

condition_rating = round(self.condition)
if condition_rating == 0:
return "user risk involved"
elif condition_rating == 1:
return "may not be working"
elif condition_rating == 2:
return "use at your own discretion"
elif condition_rating == 3:
return "used condition"
elif condition_rating == 4:
return "as good as new"
elif condition_rating == 5:
return "mint condition"
else:
return "Unknown condition"
95 changes: 94 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,95 @@
class Vendor:
pass
def __init__(self, inventory=None):
self.inventory = inventory if inventory else []

if not isinstance(self.inventory, list):

Choose a reason for hiding this comment

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

nice error checking

raise TypeError("Inventory should be a list type object")

def add(self, item):
if not item:
return None
self.inventory.append(item)
return item

def remove(self, item):
if item not in self.inventory:
return None
self.inventory.remove(item)
return item

def get_by_id(self, id):
if not id or not self.inventory:
return None
return next((item for item in self.inventory if item.id == id), None)

Choose a reason for hiding this comment

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

excellent list comprehension!


def swap_items(self, other_vendor, my_item, their_item):
if not my_item or not their_item:
return False
if not self.inventory or not other_vendor.inventory:
return False
if my_item not in self.inventory or their_item not in other_vendor.inventory:
return False
self.add(other_vendor.remove(their_item))
other_vendor.add(self.remove(my_item))
return True

def swap_first_item(self, other_vendor):
if not self or not other_vendor:

Choose a reason for hiding this comment

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

nice example of guard statements. No need to indent the rest of the function as an else block after the returns. Clean.

return False
if not self.inventory or not other_vendor.inventory:
return False
self.swap_items(other_vendor, self.inventory[0], other_vendor.inventory[0])
return True

def get_by_category(self, category):
if not category or not self.inventory:
return None
return [item for item in self.inventory if item.get_category() == category]

def get_best_by_category(self, category):
if not category:
return None
category_items = self.get_by_category(category)
if not category_items:
return None
return max(category_items, key=lambda item: item.condition)

Choose a reason for hiding this comment

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

nice use of max for this!


def swap_best_by_category(self, other_vendor, my_priority, their_priority):
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)
if not my_best_item or not their_best_item:
return False
return self.swap_items(other_vendor, my_best_item, their_best_item)

def display_inventory(self, category=None):
if not self.inventory:
print("No inventory to display.")
return
items = self.get_by_category(category) if category else self.inventory
if items:
for index, item in enumerate(items):
print(f"{index+1}. {str(item)}")
else:
print("No inventory to display.")

def swap_by_id(self, other_vendor, my_item_id, their_item_id):
if not self.inventory or not other_vendor.inventory:
return False
if (not self.get_by_id(my_item_id)) or (not other_vendor.get_by_id(their_item_id)):
return False
return self.swap_items(other_vendor, self.get_by_id(my_item_id), other_vendor.get_by_id(their_item_id))

def choose_and_swap_items(self, other_vendor, category=None):
if not self.inventory or not other_vendor.inventory:
print("No inventory to display.")
return False
print("Inventory 1:\n")
self.display_inventory(category)
print("Inventory 2:\n")
other_vendor.display_inventory(category)
my_item_id = int(input(f"Enter the item id from first inventory to swap: "))
their_item_id = int(input(f"Enter the item id from second inventory to swap: "))
return self.swap_by_id(other_vendor, my_item_id, their_item_id)

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
31 changes: 25 additions & 6 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,14 @@ def test_vendor_takes_optional_inventory():
assert "b" in vendor.inventory
assert "c" in vendor.inventory

@pytest.mark.skip
#@pytest.mark.skip
def test_vendor_inventory_should_be_list_object():
with pytest.raises(TypeError) as err:
vendor = Vendor(inventory="New Inventory")

assert str(err.value) == "Inventory should be a list type object"

#@pytest.mark.skip
def test_adding_to_inventory():
vendor = Vendor()
item = "new item"
Expand All @@ -27,7 +34,18 @@ def test_adding_to_inventory():
assert item in vendor.inventory
assert result == item

@pytest.mark.skip
#@pytest.mark.skip
def test_adding_to_inventory_edge_case_item_is_None():
vendor = Vendor()
item = None

result = vendor.add(item)

assert len(vendor.inventory) == 0
assert item not in vendor.inventory
assert result == None

#@pytest.mark.skip
def test_removing_from_inventory_returns_item():
item = "item to remove"
vendor = Vendor(
Expand All @@ -40,7 +58,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(
Expand All @@ -49,7 +67,8 @@ 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 result == None
18 changes: 12 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,36 @@
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_items_throw_error_if_id_is_not_int():
with pytest.raises(TypeError) as err:
item = Item(id="1234")
assert str(err.value) == "Item id should be an int type object"

#@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 @@ -36,7 +42,7 @@ def test_get_item_by_id():
result_item = vendor.get_by_id(test_id)
assert result_item is item_custom_id

@pytest.mark.skip
#@pytest.mark.skip
def test_get_item_by_id_no_matching():
test_id = 12345
item_a = Item()
Expand Down
17 changes: 10 additions & 7 deletions tests/unit_tests/test_wave_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
def test_item_overrides_to_string():
test_id = 12345
item = Item(id=test_id)
Expand All @@ -12,7 +12,7 @@ def test_item_overrides_to_string():
expected_result = f"An object of type Item with id {test_id}"
assert item_as_string == expected_result

@pytest.mark.skip
#@pytest.mark.skip
def test_swap_items_returns_true():
item_a = Item()
item_b = Item()
Expand Down Expand Up @@ -40,7 +40,7 @@ def test_swap_items_returns_true():
assert item_b in jolie.inventory
assert result

@pytest.mark.skip
#@pytest.mark.skip
def test_swap_items_when_my_item_is_missing_returns_false():
item_a = Item()
item_b = Item()
Expand All @@ -67,7 +67,7 @@ def test_swap_items_when_my_item_is_missing_returns_false():
assert item_e in jolie.inventory
assert not result

@pytest.mark.skip
#@pytest.mark.skip
def test_swap_items_when_their_item_is_missing_returns_false():
item_a = Item()
item_b = Item()
Expand All @@ -94,7 +94,7 @@ def test_swap_items_when_their_item_is_missing_returns_false():
assert item_e in jolie.inventory
assert not result

@pytest.mark.skip
#@pytest.mark.skip
def test_swap_items_from_my_empty_returns_false():
fatimah = Vendor(
inventory=[]
Expand All @@ -114,7 +114,7 @@ def test_swap_items_from_my_empty_returns_false():
assert len(jolie.inventory) == 2
assert not result

@pytest.mark.skip
#@pytest.mark.skip
def test_swap_items_from_their_empty_returns_false():
item_a = Item()
item_b = Item()
Expand All @@ -131,7 +131,10 @@ def test_swap_items_from_their_empty_returns_false():

result = fatimah.swap_items(jolie, item_b, nobodys_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(jolie.inventory) == 0
assert len(fatimah.inventory) == 3
assert not result
Loading