Skip to content
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

swap-meet, C17, Sea Turtles, Shelby Faulconer #109

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Swap Meet

## Skills Assessed
Expand Down
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, condition = 0):
super().__init__(category = "Clothing", condition = condition)
self.condition = condition

Choose a reason for hiding this comment

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

We can remove this line like in the Decor & Electronics classes since the condition attribute is set by the super() dunder init function.



def __str__(self):
return "The finest clothing you could wear."
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, condition = 0):
super().__init__(category= "Decor", condition = condition)


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

class Electronics(Item):

def __init__(self, condition = 0):

Choose a reason for hiding this comment

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

The subclasses look great!

super().__init__(category= "Electronics", condition = condition)


def __str__(self):
return "A gadget full of buttons and secrets."
23 changes: 22 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
CONDITION_DESCRIPTION = {0:"One person's trash is another's treasure.",
1:"Probably not it's first swap meet.",
2:"Stained but functional",
3:"Used enough to know it works juuust right.",
4:"Stored for ages and finally seeing the light.",
5:"The tag is still on it."}

class Item:
pass

def __init__(self, category = "", condition = 0):
self.category = category
self.condition = condition


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


def condition_description(self):
for key, value in CONDITION_DESCRIPTION.items():
if self.condition is key:
return value
Comment on lines +20 to +22

Choose a reason for hiding this comment

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

Since CONDITION_DESCRIPTION is a dictionary, we could simplify this to avoid looping over the values:

Suggested change
for key, value in CONDITION_DESCRIPTION.items():
if self.condition is key:
return value
return CONDITION_DESCRIPTION[self.condition]

Since we don't currently check if the condition passed to an Item is strictly between 0-5, we might want to wrap it in a try/except in case we get a KeyError.

Something else to consider: what would happen if we tried to get the description for an Item with a condition of 3.5? There are tests that set the condition to 3.5, but don't try to read the description. The description test checks for a condition of 1 and a description of 5, but we should still make sure our code covers any other reasonable conditions, including floating point values (since we've seen them used in other tests).

How could we modify this (slightly) to cover the values between the whole numbers?


88 changes: 87 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,88 @@
from unicodedata import category

Choose a reason for hiding this comment

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

It looks like VS Code might have been a little overzealous with auto-imports. We should check for and remove imports that are unused as part of our code clean up when we think we're about done.

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.

Because of the way python handles typing and looking up attributes & methods, if we do not directly reference a class or its constructor, we do not need to import the class into another file, even if it uses instances of that type. Ansel shared out a slide deck on Slack that talks about how this works that could be helpful if you haven't seen it yet: https://docs.google.com/presentation/d/1PMSNIHgfBViYQ_MlP59qAm5ylT9njZfS9YQ6-xsvKhU/edit?usp=sharing

Here in Vendor, we never create an Item instance or use the Item class itself to do something like call a class method, so we do not need to import item, and should remove this line.


class Vendor:
pass

def __init__(self, inventory = None):
if not inventory:
self.inventory = []
else:
self.inventory = inventory
Comment on lines +7 to +10

Choose a reason for hiding this comment

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

Nice handling of the None default value! Another way we could write this is with a ternary operator:

self.inventory = inventory if inventory else []

This can be read as "If the parameter inventory is truthy, assign self.inventory to inventory, else assign self.inventory to an empty list". The general blueprint for a ternary operator looks like: variable_name = <value_if_condition_true> if <condition_statement> else <value_if_condition_false>.

The solution above will ignore if the user passes in an empty list (since an empty list is falsy) and create a new empty list. If we're worried about space and want to avoid that, we could be more explicit with our check for None

self.inventory = inventory if inventory is not None else []

More info: https://book.pythontips.com/en/latest/ternary_operators.html



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

Choose a reason for hiding this comment

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

I like inverting the logic of the condition to be able to treat it like a guard clause, which allows us to outdent the main logic of the function something like the following:

        if item not in self.inventory:
            return False

        self.inventory.remove(item)
        return item

Another approach we could take is to try to remove the item directly, and handle the ValueError that occurs if it's not there, and return False to handle it.



def get_by_category(self, category):
if not category:
return False

Choose a reason for hiding this comment

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

I like this check, since it prevents us from looping over the list if the category doesn't exist!

I would suggest returning an empty list here so that the function returns the same type of data in success or failure scenarios, making it easier for users to reason about what they can expect from the function. We could also return None instead since it's a common enough pattern to return None in place of an object if that object does not exist or cannot be created.

category_list = []
for item in self.inventory:
if item.category == category:
category_list.append(item)
Comment on lines +29 to +32

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:

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

At first, this may seem more difficult to read, but comprehensions are a very common python style, so try getting used to working with them!

return category_list


def swap_items(self, friend, my_item, their_item):
if my_item not in self.inventory:
return False
if their_item not in friend.inventory:
return False
Comment on lines +37 to +40

Choose a reason for hiding this comment

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

We could use or to combine these if-statements:

Suggested change
if my_item not in self.inventory:
return False
if their_item not in friend.inventory:
return False
if my_item not in self.inventory or their_item not in friend.inventory:
return False


self.inventory.remove(my_item)
friend.inventory.append(my_item)
Comment on lines +42 to +43

Choose a reason for hiding this comment

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

remove and append are being called directly on self.inventory and friend.inventory, but we could also use the add and remove functions we wrote here:

Suggested change
self.inventory.remove(my_item)
friend.inventory.append(my_item)
self.remove(my_item)
friend.add(my_item)

friend.inventory.remove(their_item)
self.inventory.append(their_item)

if their_item not in self.inventory:
return False
else:
return True
Comment on lines +42 to +50

Choose a reason for hiding this comment

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

Since we can assume that the python built-in functions append and remove will do as we expect or raise an error, I'd leave off checking if their_item is in our list and directly return True here.

Suggested change
self.inventory.remove(my_item)
friend.inventory.append(my_item)
friend.inventory.remove(their_item)
self.inventory.append(their_item)
if their_item not in self.inventory:
return False
else:
return True
self.inventory.remove(my_item)
friend.inventory.append(my_item)
friend.inventory.remove(their_item)
self.inventory.append(their_item)
return True



def swap_first_item(self, friend):
if self.inventory == []:
return False
else:
my_item = self.inventory[0]
if friend.inventory == []:
return False
else:
their_item = friend.inventory[0]
Comment on lines +54 to +61

Choose a reason for hiding this comment

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

If friend.inventory is empty, we'll create the my_item variable even though it will never be used. I recommend using an if to validate the input first before creating variables:

Suggested change
if self.inventory == []:
return False
else:
my_item = self.inventory[0]
if friend.inventory == []:
return False
else:
their_item = friend.inventory[0]
if not self.inventory or not friend.inventory:
return False
my_item = self.inventory[0]
their_item = friend.inventory[0]

# use swap_items method
self.swap_items(friend, my_item, their_item)

Choose a reason for hiding this comment

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

Nice code reuse!

Something to consider: by using swap_items, this winds up being O(n) (mostly due to removing the items from the inventory). If it was important to lower the Big O complexity, we could swap the items in place here to have O(1) runtime with something like:

self.inventory[0], friend.inventory[0] = friend.inventory[0], self.inventory[0]

return True


def get_best_by_category(self, category):
desired_category = []

for item in self.inventory:
if category in item.category:

Choose a reason for hiding this comment

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

I recommend using the == operator here to compare category and item.category since they are string objects. The if-statement here will evaluate to true if the word held by category is the same as or is a substring of the word held by item.category. For example, if category was "Dress" and item.category held "Dresser", the if-statement would evaluate to true and the item would be added to the list even though that's likely not what we want.

Suggested change
if category in item.category:
if category == item.category:

desired_category.append(item)
# lambda will sort objects by instance's "condtion" attribute
sort_list = sorted(desired_category, key=lambda item: item.condition)
Comment on lines +73 to +74

Choose a reason for hiding this comment

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

We should line up comments with the code they refer to. This makes it easier to see at a glance the indentation (and thus various scopes) across a file.

Suggested change
# lambda will sort objects by instance's "condtion" attribute
sort_list = sorted(desired_category, key=lambda item: item.condition)
# lambda will sort objects by instance's "condtion" attribute
sort_list = sorted(desired_category, key=lambda item: item.condition)

Comment on lines +70 to +74

Choose a reason for hiding this comment

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

Because line 74 is inside the for loop, every time an item is added to desired_category we sort all the values in it. If we had a list of thousands of items, several hundred of which match our category, this could be a very slow operation. If we outdented line 74 to place it outside the for loop, we could do one sort at the end once we have all of our items.

Suggested change
for item in self.inventory:
if category in item.category:
desired_category.append(item)
# lambda will sort objects by instance's "condtion" attribute
sort_list = sorted(desired_category, key=lambda item: item.condition)
for item in self.inventory:
if category in item.category:
desired_category.append(item)
# lambda will sort objects by instance's "condtion" attribute
sort_list = sorted(desired_category, 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.

Great use of a lambda function!

Comment on lines +68 to +74

Choose a reason for hiding this comment

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

Since we're building up a list of items with a particular category we could reuse the function get_by_category here!

Suggested change
desired_category = []
for item in self.inventory:
if category in item.category:
desired_category.append(item)
# lambda will sort objects by instance's "condtion" attribute
sort_list = sorted(desired_category, key=lambda item: item.condition)
desired_category = self.get_by_category(category)
# lambda will sort objects by instance's "condtion" attribute
sort_list = sorted(desired_category, key=lambda item: item.condition)

if desired_category == []:
return None
return sort_list[-1]
Comment on lines +74 to +77

Choose a reason for hiding this comment

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

We can keep sort_list if we want to be more explicit about our variables and what they hold, but we could also factor that variable out:

Suggested change
sort_list = sorted(desired_category, key=lambda item: item.condition)
if desired_category == []:
return None
return sort_list[-1]
desired_category = sorted(desired_category, key=lambda item: item.condition)
if desired_category == []:
return None
return desired_category[-1]

If we wanted to keep sort_list I would recommend looking at the same list for both the end checks, so it cannot be confused which list should be the source of truth for the result:

Suggested change
sort_list = sorted(desired_category, key=lambda item: item.condition)
if desired_category == []:
return None
return sort_list[-1]
sort_list = sorted(desired_category, key=lambda item: item.condition)
if sort_list == []:
return None
return sort_list[-1]



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

Choose a reason for hiding this comment

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

Great approach for this function!

# created variables for self and other to use the get_best_by_category method
their_desired_item = self.get_best_by_category(their_priority)
my_desired_item = other.get_best_by_category(my_priority)

if their_desired_item is None or my_desired_item is None:
return False
self.swap_items(other, their_desired_item, my_desired_item)
return True
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
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 **********
# *********************************************************************
# raise Exception("Complete this test according to comments below.")
assert result == False
Copy link

@kelsey-steven-ada kelsey-steven-ada Apr 14, 2022

Choose a reason for hiding this comment

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

This assert is great for confirming the return value is correct, but what else could we assert to make sure there were no side affects like changes to the inventory?

14 changes: 6 additions & 8 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 @@ -33,8 +33,6 @@ def test_get_no_matching_items_by_category():
)

items = vendor.get_by_category("electronics")

raise Exception("Complete this test according to comments below.")
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
assert len(items) == 0
# raise Exception("Complete this test according to comments below.")
# ********** complete
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