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

def __str__(self):
parent_str = super().__str__()
return f"{parent_str} It is made from {self.fabric} fabric."

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I really like the way you've parsed out the parent_str here. Feel free to skip that step and just include the super().__str__() in the f String!



12 changes: 10 additions & 2 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
class Decor:
pass
from .item import Item
class Decor(Item):
def __init__(self, id=None, width=0, length=0, condition=0, age=0):
super().__init__(id, condition, age)
self.width = width
self.length = length

def __str__(self):
parent_str = super().__str__()
return f"{parent_str} It takes up a {self.width} by {self.length} sized space."

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This function looks great! Good use of the default parameters!

11 changes: 9 additions & 2 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
class Electronics:
pass
from .item import Item
class Electronics(Item):
def __init__(self, id=None, type="Unknown", condition=0, age=0):
super().__init__(id, condition, age)
self.type = type

def __str__(self):
parent_str = super().__str__()
return f"{parent_str} This is a {self.type} device."

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good!

30 changes: 29 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
import uuid
class Item:
pass
def __init__(self, id=None, condition=0, age=0):
self.id = uuid.uuid4().int if id is None else id

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 ternary here!

self.condition = condition
self.age = age

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

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

def condition_description(self):
x = self.condition
if x < 0 or x > 5:
return "Condition unknown. Is this from another dimension?"
elif x < 1:
return "Yikes! This thing's seen better centuries."
elif x < 2:
return "Handle with care... and maybe a hazmat suit."
elif x < 3:
return "It's got 'character' (that's code for 'issues')."
elif x < 4:
return "Not too shabby, just don't look too closely."
elif x < 5:
return "Almost new, if you squint hard enough."
else:
return "Mint condition! Did you steal this from a time machine?"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your Item class looks really good too! I love the descriptions you have for your conditions! I also appreciate that you have given ranges for each condition description! this allows for a wider range of possibilities!


85 changes: 84 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,85 @@
class Vendor:
pass
def __init__(self, inventory=None):
self.inventory = inventory or []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is not syntax I had seen before and I intuitively didn't think it would work (I knew it did because all of your tests pass), but great job researching and finding this particular syntax!


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

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

self.inventory.remove(item)
return item

def get_by_id(self, num=None):
for item in self.inventory:
if item.id == num:
return item
return None
Comment on lines +5 to +20

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your add, remove and get_by_id functions look great!


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:
return False
Comment on lines +23 to +24

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 job on this guard clause!


other_vendor.add(my_item)
self.add(their_item)
other_vendor.remove(their_item)
self.remove(my_item)
Comment on lines +26 to +29

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

My one challenge here would to be to see if you can combine these four lines into just two! Otherwise it looks great!


return True

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.

Good use of the previous function!


return True

def get_by_category(self, category):

items = [item for item in self.inventory if item.get_category() == category]

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 list comprehension!

return items

def get_best_by_category(self, category):

items = self.get_by_category(category)

if not items:
return None

best_item = None
for item in items:
if best_item is None or item.condition > best_item.condition:
best_item = item
Comment on lines +53 to +56

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A super small tweak, but if we can avoid having to check and see if best_item is None along with the more important condition of comparing the conditions, we should try that!


return best_item

def swap_best_by_category(self, other_vendor, my_priority, their_priority):

best_from_other = other_vendor.get_best_by_category(my_priority)
best_from_self = self.get_best_by_category(their_priority)

return self.swap_items(other_vendor, best_from_self, best_from_other)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As always, this looks good too!


def find_newest_item(self, items):
if not items:
return None

newest_item = None
for item in items:
if newest_item is None or item.age < newest_item.age:
newest_item = item

return newest_item

def swap_by_newest(self, other_vendor):
my_new_item = self.find_newest_item(self.inventory)
their_new_item = self.find_newest_item(other_vendor.inventory)

if not my_new_item or not their_new_item:
return False

return self.swap_items(other_vendor, my_new_item, their_new_item)
Comment on lines +67 to +85

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for adding in these optional enhancements! They look really good!

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.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():
camila = Vendor()
Expand Down
65 changes: 65 additions & 0 deletions tests/unit_tests/test_optional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import pytest
from swap_meet.item import Item
from swap_meet.vendor import Vendor
from swap_meet.clothing import Clothing
from swap_meet.decor import Decor
from swap_meet.electronics import Electronics

def test_age_valid_is_true():
num = 5

item = Item(age=num)
assert isinstance(item.age, int)
assert item.age == num

def test_swap_by_newest_no_inventory_is_false():
tai = Vendor(
inventory=[]
)

item_a = Clothing(age=1)
item_b = Decor(age=3)
item_c = Clothing(age=5)
jesse = Vendor(
inventory=[item_a, item_b, item_c]
)

result = tai.swap_by_newest(
other_vendor=jesse
)

assert not result
assert len(tai.inventory) == 0
assert len(jesse.inventory) == 3
assert item_a in jesse.inventory
assert item_b in jesse.inventory
assert item_c in jesse.inventory

def test_swap_by_newest_valid_input_true():
# Arrange
# me
item_a = Decor(age=2)
item_b = Electronics(age=4)
item_c = Decor(age=4)
tai = Vendor(
inventory=[item_a, item_b, item_c]
)

# them
item_d = Clothing(age=2)
item_e = Decor(age=4)
item_f = Clothing(age=4)
jesse = Vendor(
inventory=[item_d, item_e, item_f]
)

# Act
result = tai.swap_by_newest(
other_vendor=jesse
)
assert result
assert len(tai.inventory) == 3
assert len(jesse.inventory) == 3
assert item_a in jesse.inventory
assert item_d in tai.inventory
assert item_b in tai.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.

Your optional test suite looks great!

16 changes: 7 additions & 9 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,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_is_false():
item = "item to remove"
vendor = Vendor(
Expand All @@ -49,7 +49,5 @@ def test_removing_not_found_is_false():

result = vendor.remove(item)

raise Exception("Complete this test according to comments below.")
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
assert item not in vendor.inventory
assert not result
12 changes: 6 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,30 @@
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 @@ -36,7 +36,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
Loading