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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Now, we may notice that these three classes hold the same types of state and hav
You'll need to refer to `Item` in order to declare it as a parent. To reference the `Item` class from these modules, try this import line:

```python
from swap_meet.item import Item
```

### Wave 6
Expand Down
9 changes: 7 additions & 2 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
class Clothing:
pass
from swap_meet.item import Item

Choose a reason for hiding this comment

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

Since Item and Clothing are in the same swap_meet package, we can import the class with shorter syntax from .item import Item. If they were not in the same package, then we would need to specify the package name swap_meet

class Clothing(Item):
def __init__(self, condition = 0, age = 0):

Choose a reason for hiding this comment

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

Nice work removing category from the available initializer parameters. Doing so indicates that we don't want the caller to be able to set a different category.

The caller could always change the category by assigning directly to it after:

clothing = Clothing(condition=3)
clothing.category = "Electronics"

But at least we're indicating that they shouldn't do that when we organize the code this way.

Also, with default params we tend to leave spaces off from each side of the equal sign:

def __init__(self, condition=0, age=0):

super().__init__('Clothing', condition, age)

def __str__(self):
return f"The finest clothing you could wear."
9 changes: 7 additions & 2 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
class Decor:
pass
from swap_meet.item import Item
class Decor(Item):
def __init__(self, condition = 0, age = 0):
super().__init__('Decor', condition, age)

def __str__(self):
return f"Something to decorate your space."
10 changes: 8 additions & 2 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
class Electronics:
pass
from swap_meet.item import Item
class Electronics(Item):
def __init__(self, condition = 0, age = 0):
super().__init__('Electronics', condition, age)

def __str__(self):
return "A gadget full of buttons and secrets."

24 changes: 23 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
class Item:
pass
def __init__(self, category = '', condition = 0, age = 0):
self.category = category
self.condition = condition
self.age = age
Comment on lines +2 to +5

Choose a reason for hiding this comment

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

🥳



def __str__(self):
return f"Hello World!"

def condition_description(self):
description = {
0: 'bad',
1: 'poor',
2: 'decent',
3: 'average',
4: 'good',
5: 'pristine'
}

for rating, quality in description.items():
if self.condition == rating:
return quality
Comment on lines +12 to +23

Choose a reason for hiding this comment

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

Excellent use of a data structure here to get the description based on condition - using a list enables you to avoid a long list of chained checks (if/elif/elif...) which can introduce bugs.

There are still edge case behaviors we'd need to consider and handle: what if the condition is outside the expected range? what if the condition is a non-integer value? Currently the function would return None if the condition on line 22 doesn't evaluate to True.

Also, when you have a dict, you can iterate through the keys without using .items like:

for rating in description:
        if condition == rating:
            return description[rating] 


57 changes: 56 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,57 @@
class Vendor:
pass

def __init__(self,inventory=None):
self.inventory = inventory if inventory is not None else []

Choose a reason for hiding this comment

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

👍


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

def remove(self,item):
if item in self.inventory:
self.inventory.remove(item)
return item
return False
Comment on lines +11 to +14

Choose a reason for hiding this comment

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

Prefer writing the error handling case as a guard clause first, which lets us then emphasize the main part of the code by not indenting it.

if item not in self.inventory: 
    return False 

self.inventory.remove(item) 
return item


def get_by_category(self, category):

return [item for item in self.inventory if category == item.category]

Choose a reason for hiding this comment

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

Niiiice!


def swap_items(self,friend, my_item, their_item):

if my_item in self.inventory and their_item in friend.inventory:
self.inventory.remove(my_item)
friend.inventory.remove(their_item)
self.inventory.append(their_item)
friend.inventory.append(my_item)
return True
return False
Comment on lines +22 to +28

Choose a reason for hiding this comment

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

We can use a guard clause here too to flip the logic so that we have a check to make sure we're working with valid data. If the data isn't valid we'll return false and then the meat of your logic can be un-indented in the lines that follow:

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

# rest of your swapping logic here

Also, recall that you already wrote methods for the Vendor class that allow you to add and remove from the inventory (without needing to access the inventory attribute on each Vendor object and using the Python List methods remove and append). You can reuse those methods on lines 23-26:

self.remove(my_item)
friend.remove(their_item)
self.add(their_item)
friend.add(my_item)


def swap_first_item(self, friend):

if self.inventory and friend.inventory:
return self.swap_items(friend, self.inventory[0], friend.inventory[0])
Comment on lines +32 to +33

Choose a reason for hiding this comment

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

What would this function return if self.inventory and friend.inventory were empty?

How could you use a guard clause here to handle that scenario?

Also, it's great to reuse the swap_items method you already wrote! However, swap_items has linear time complexity because it invokes the remove() method.

Can you think of how you could write a custom swapping implementation here to bring the time complexity to O(1) ?


def get_best_by_category(self, category):

categories = self.get_by_category(category)

if not categories:
return None
return max(categories, key = lambda x : x.condition)
Comment on lines +37 to +41

Choose a reason for hiding this comment

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

Nicely done. Consider renaming x to item for clarity


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

other_vendor = other.get_best_by_category(my_priority)
my_vendor = self.get_best_by_category(their_priority)

return self.swap_items(other, my_vendor, other_vendor)

def swap_by_newest(self, other, their_newest, my_newest):

return self.swap_items(other, min(self.inventory, key = lambda x : x.age) , min(other.inventory, key = lambda x : x.age))

Choose a reason for hiding this comment

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

Nice and concise solution here! To make the method a little more readable, consider pulling out some of these values into variables and passing the variables into swap_items.

It's definitely up to the coder to decide when to pull values into variables and when to write things in-line. The reason I suggested using variables is because as I was reading line 52, I had to slow down and figure out what each argument being passed into swap_items was -- that's usually a signal that variables should be introduced.






4 changes: 1 addition & 3 deletions 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 Expand Up @@ -50,5 +50,3 @@ def test_integration_wave_01_02_03():
assert len(other_vendor.inventory) == 1
assert item2 in other_vendor.inventory
assert item3 in vendor.inventory


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
18 changes: 12 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,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,13 @@ def test_removing_not_found_is_false():

result = vendor.remove(item)

raise Exception("Complete this test according to comments below.")
assert item not in vendor.inventory
assert len(vendor.inventory) == 3
assert result is False

# raise Exception("Complete this test according to comments below.")
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************


11 changes: 7 additions & 4 deletions tests/unit_tests/test_wave_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from swap_meet.vendor import Vendor
from swap_meet.item import Item

@pytest.mark.skip
# @pytest.mark.skip
def test_items_have_blank_default_category():
item = Item()
assert item.category == ""

@pytest.mark.skip
# @pytest.mark.skip
def test_get_items_by_category():
item_a = Item(category="clothing")
item_b = Item(category="electronics")
Expand All @@ -23,7 +23,7 @@ def test_get_items_by_category():
assert item_c in items
assert item_b not in items

@pytest.mark.skip
# @pytest.mark.skip
def test_get_no_matching_items_by_category():
item_a = Item(category="clothing")
item_b = Item(category="clothing")
Expand All @@ -34,7 +34,10 @@ def test_get_no_matching_items_by_category():

items = vendor.get_by_category("electronics")

raise Exception("Complete this test according to comments below.")
assert len(vendor.inventory) == 3
assert item_a is not items
assert item_b is not items
assert item_c is not items
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
12 changes: 6 additions & 6 deletions tests/unit_tests/test_wave_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
from swap_meet.vendor import Vendor
from swap_meet.item import Item

@pytest.mark.skip
# @pytest.mark.skip
def test_item_overrides_to_string():
item = Item()

stringified_item = str(item)

assert stringified_item == "Hello World!"

@pytest.mark.skip
# @pytest.mark.skip
def test_swap_items_returns_true():
item_a = Item(category="clothing")
item_b = Item(category="clothing")
Expand Down Expand Up @@ -38,7 +38,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(category="clothing")
item_b = Item(category="clothing")
Expand All @@ -65,7 +65,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(category="clothing")
item_b = Item(category="clothing")
Expand All @@ -92,7 +92,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 @@ -112,7 +112,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(category="clothing")
item_b = Item(category="clothing")
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/test_wave_04.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_swap_first_item_returns_true():
item_a = Item(category="clothing")
item_b = Item(category="clothing")
Expand Down Expand Up @@ -30,7 +30,7 @@ def test_swap_first_item_returns_true():
assert item_a in jolie.inventory
assert result

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

@pytest.mark.skip
# @pytest.mark.skip
def test_swap_first_item_from_their_empty_returns_false():
item_a = Item(category="clothing")
item_b = Item(category="clothing")
Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests/test_wave_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
from swap_meet.decor import Decor
from swap_meet.electronics import Electronics

@pytest.mark.skip
# @pytest.mark.skip
def test_clothing_has_default_category_and_to_str():
cloth = Clothing()
assert cloth.category == "Clothing"
assert str(cloth) == "The finest clothing you could wear."

@pytest.mark.skip
# @pytest.mark.skip
def test_decor_has_default_category_and_to_str():
decor = Decor()
assert decor.category == "Decor"
assert str(decor) == "Something to decorate your space."

@pytest.mark.skip
# @pytest.mark.skip
def test_electronics_has_default_category_and_to_str():
electronics = Electronics()
assert electronics.category == "Electronics"
assert str(electronics) == "A gadget full of buttons and secrets."

@pytest.mark.skip
# @pytest.mark.skip
def test_items_have_condition_as_float():
items = [
Clothing(condition=3.5),
Expand All @@ -31,7 +31,7 @@ def test_items_have_condition_as_float():
for item in items:
assert item.condition == pytest.approx(3.5)

@pytest.mark.skip
# @pytest.mark.skip
def test_items_have_condition_descriptions_that_are_the_same_regardless_of_type():
items = [
Clothing(condition=5),
Expand Down
Loading