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

Otters - Nikki Torab #104

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
17 changes: 15 additions & 2 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
class Clothing:
pass
from swap_meet.item import Item


class Clothing(Item):


def __init__(self, condition = 0, age = None):
self.category = "Clothing"
self.condition = condition
self.age = age
Comment on lines +8 to +10

Choose a reason for hiding this comment

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

This will work, but it's duplicating code that is already in Item. Using the super constructor allows this function to take advantage of all of the code in Item:

Suggested change
self.category = "Clothing"
self.condition = condition
self.age = age
super().__init__("Clothing", condition, age)



#override str() for Clothing, returns "The finest clothing you could wear."
def __str__(self):
return "The finest clothing you could wear."
16 changes: 14 additions & 2 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
class Decor:
pass
from swap_meet.item import Item


class Decor(Item):


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

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


class Electronics(Item):

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

#override str() for Electronics, returns "A gadget full of buttons and secrets."
def __str__(self):
return "A gadget full of buttons and secrets."
36 changes: 35 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@


class Item:
pass

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

#override str() for Item, returns "Hello World!" instead
def __str__(self):
return "Hello World!"


#describes condition based on the value, range from 0 to 5
def condition_description(self):

if self.condition == 0:
return "Terrible condition"

if self.condition == 1:
return "Bad condition"

if self.condition == 2:
return "Slightly bad condition"

if self.condition == 3:
return "Acceptable condition"

if self.condition == 4:
return "Good condition"

if self.condition == 5:
return "Mint conditon"

Choose a reason for hiding this comment

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

The condition can be a float, so it can contain a decimal. In the cases where the condition has a decimal, this function will return None. There are a few solutions, one is to cast the condition to an int, which will drop the decimal portion of the number and round down. Another possible solution is to test for a range of values (ex: if self.condition <= 5 and self.condition > 4.



140 changes: 139 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,140 @@
from swap_meet.item import Item

class Vendor:
pass
def __init__(self, inventory = None):
#code on lines 6-7 from https://docs.python-guide.org/writing/gotchas/
if inventory == None:
inventory = []
self.inventory = inventory

# Adds the item to the inventory
def add(self, item):
self.inventory.append(item)
return item


# Removes the matching item from the inventory
def remove(self, item):
if item not in self.inventory:
return False
else:
self.inventory.remove(item)
return item


# Takes one argument: a string, representing a category
# Returns a list of Items in the inventory with that category
def get_by_category(self, category):
#self.item = Item(category)
item_list = []
for item in self.inventory:
if item.category == category:
item_list.append(item)


return item_list


# removes my_item from this Vendor's inventory
# adds my_item to the other Vendor's inventory
# removes their_item from other Vendor's inventory
# adds their_item to the this Vendor's inventory
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

self.remove(my_item)
other_vendor.add(my_item)

other_vendor.remove(their_item)
self.add(their_item)
Comment on lines +47 to +51

Choose a reason for hiding this comment

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

Great use of helper methods!



return True


#removes first item from this Vendor's inventory
# adds the other Vendor's first item
# removes first item from other Vendor's inventory
# adds this Vendor's first item
def swap_first_item(self, other_vendor):
if len(self.inventory) == 0 or len(other_vendor.inventory) == 0:
return False

self.swap_items(other_vendor, self.inventory[0], other_vendor.inventory[0])

Choose a reason for hiding this comment

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

💯


return True


#gets the item with the best condition in a certain category
def get_best_by_category(self, category):

category_inventory = self.get_by_category(category)

if len(category_inventory) == 0:
return None

highest_rated = category_inventory[0]

for item in category_inventory:

if (item.condition) > (highest_rated.condition):
highest_rated = item

return highest_rated

Choose a reason for hiding this comment

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

👍



#swap the best item of certain categories with another Vendor
def swap_best_by_category(self, other, my_priority, their_priority):

#find best item in my inventory that matches their_priority
my_best = self.get_best_by_category(their_priority)

their_best = other.get_best_by_category(my_priority)

if my_best == None or their_best == None:
return False

self.swap_items(other, my_best, their_best)
Comment on lines +92 to +99

Choose a reason for hiding this comment

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

Fantastic code re-use!


return True



def swap_by_newest(self, other):
my_newest = self.inventory[0]

Choose a reason for hiding this comment

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

If the inventory is empty, this will throw an error. Consider wrapping this line in a try/except block that returns None if this line fails.

for item in self.inventory:
if not item.age is None:
my_newest = item
break
if my_newest.age is None:
return False

for item in self.inventory:
if not item.age is None:
if item.age < my_newest.age:
my_newest = item

their_newest = other.inventory[0]
for item in other.inventory:
if not item.age is None:
their_newest = item
break
if their_newest.age is None:
return False

for item in other.inventory:
if not item.age is None:
if item.age < their_newest.age:
their_newest = item
Comment on lines +119 to +130

Choose a reason for hiding this comment

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

This section is the same as lines 106-117. I recommend pulling this out into a helper method.


self.swap_items(other, my_newest, their_newest)

Choose a reason for hiding this comment

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

👍

return True







8 changes: 4 additions & 4 deletions tests/integration_tests/test_wave_01_02_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
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
vendor = Vendor()


assert len(vendor.inventory) == 0

# add an item
Expand Down Expand Up @@ -49,6 +51,4 @@ def test_integration_wave_01_02_03():
assert len(vendor.inventory) == 1
assert len(other_vendor.inventory) == 1
assert item2 in other_vendor.inventory
assert item3 in 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
16 changes: 9 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,18 @@ 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(
inventory=["a", "b", "c"]
)

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 == False
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.")
#raise Exception("Complete this test according to comments below.")
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
assert item_a not in items
assert item_b not in items
assert item_c not in items

Choose a reason for hiding this comment

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

Tests work better when there is no room for ambiguity. In this case, checking that these items are not in the list of items is great, but it leaves room for there to be some other item that is also not wanted. Adding something like:

    assert len(items) == 0

helps to make it clear that the items list should be empty.

Loading