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

Cheetahs - June V #97

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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, category = "Clothing", condition = 0.0, age = 0):
super().__init__( category = "", condition = 0.0, age = 0)
self.category = category
self.condition = condition
self.age = age
Comment on lines +4 to +8

Choose a reason for hiding this comment

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

This setup makes it possible to pass in a value for category that is not "Clothing", which could result in an instance of Clothing that has some other category. Right now the values being passed to the __init__ function of the parent class are default values, so lines 6-8 are necessary to initialize this instance with the values that are being passed in to the constructor. However, changing the values that are being passed into the super constructor makes it possible to use the code in the parent class, and then lines 6-8 will no longer be necessary.

Suggested change
def __init__(self, category = "Clothing", condition = 0.0, age = 0):
super().__init__( category = "", condition = 0.0, age = 0)
self.category = category
self.condition = condition
self.age = age
def __init__(self, condition = 0.0, age = 0):
super().__init__( category = "Clothing", condition = condition, age = age)

or

Suggested change
def __init__(self, category = "Clothing", condition = 0.0, age = 0):
super().__init__( category = "", condition = 0.0, age = 0)
self.category = category
self.condition = condition
self.age = age
def __init__(self, condition = 0.0, age = 0):
super().__init__( "Clothing", condition, age)


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

class Decor(Item):
def __init__(self, category = "Decor", condition = 0.0, age = 0):
super().__init__( category = "", condition = 0.0, age = 0)
self.category = category
self.condition = condition
self.age = age

def __str__(self):
return "Something to decorate your 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, category = "Electronics", condition = 0.0, age = 0):
super().__init__( category = "", condition = 0.0, age = 0)
self.category = category
self.condition = condition
self.age = 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 @@
import math

class Item:
pass
def __init__(self, category = "", condition = 0.0, age = 0):
self.category = category
self.condition = condition
self.age = age

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

def condition_description(self):
if math.floor(self.condition) == 0:
return "damaged"
elif math.floor(self.condition) == 1:
return "poor"
elif math.floor(self.condition) == 2:
return "average"
elif math.floor(self.condition) == 3:
return "good"
elif math.floor(self.condition) == 4:
return "mint"
elif math.floor(self.condition) == 5:
return "new"

Choose a reason for hiding this comment

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

Neat use of math.floor!

138 changes: 137 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,138 @@
from swap_meet.item import Item
from swap_meet.electronics import Electronics
from swap_meet.decor import Decor
from swap_meet.clothing import Clothing

class Vendor:
pass
'''
object contains an attribute: inventory that is an empty list with an optional parameter
it also contains the methods: add, remove, get_by_category, swap_items, swap_first_item,
get_best_by_category
'''
def __init__(self, inventory = None):
if inventory is None:
inventory = []
self.inventory = inventory

def add(self, item):
'''
appends item parameter to inventory list
returns the last item in the inventory list
'''
self.inventory.append(item)
last_inventory_item = self.inventory[-1]

return last_inventory_item
Comment on lines +23 to +25

Choose a reason for hiding this comment

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

This last_inventory_item code works, but it's doing some extra work. It is possible to just return item here.


def remove(self, item):
'''
removes item parameter from inventory
returns item if item is removed, returns False if nothing is removed
'''
for product in self.inventory:
if product == item:
self.inventory.remove(item)
return product
Comment on lines +34 to +35

Choose a reason for hiding this comment

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

👍


return False

def get_by_category(self, category):
'''
appends items from inventory that match the category parameter
'''
item_list = []

for item in self.inventory:
if item.category == category:
item_list.append(item)

return item_list

def swap_items(self, other_vendor, my_item, their_item):
'''
swaps items between self and other inventories
'''
# Helpers
self_inventory = self.inventory
other_inventory = other_vendor.inventory

if my_item not in self_inventory or their_item not in other_inventory or len(other_inventory) == 0 or len(self_inventory) == 0:
return False

for item in self_inventory:
if item == my_item:
other_vendor.add(item)
self.remove(item)

for item in other_inventory:
if item == their_item:
self.add(item)
other_vendor.remove(item)
Comment on lines +62 to +70

Choose a reason for hiding this comment

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

These loops are not necessary here. The check on line 59 has already verified that my_item is in self_inventory and their_item is in other_inventory. The reference to the item in the list will be the same as any other reference to the item, so we do not have to find the item in the list in order to work with it. When we have an object in memory, all of the variables that are pointing to that object have the same value (ie, the reference to that object), and can be used for comparison purposes interchangably. Fantastic use of helper methods, though!

Suggested change
for item in self_inventory:
if item == my_item:
other_vendor.add(item)
self.remove(item)
for item in other_inventory:
if item == their_item:
self.add(item)
other_vendor.remove(item)
other_vendor.add(my_item)
self.remove(my_item)
self.add(their_item)
other_vendor.remove(their_item)


return True

def swap_first_item(self, other_vendor):
'''
swaps the first items of self and other inventories
'''
# Swap 1st items
try:
# Helpers
self_inventory_item = self.inventory[0]
other_inventory_item = other_vendor.inventory[0]
return self.swap_items(other_vendor, self_inventory_item, other_inventory_item)

Choose a reason for hiding this comment

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

Great use of a helper method!


except:

Choose a reason for hiding this comment

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

Nice use of try/except!

return False

def get_best_by_category(self, category):
'''
returns the best condition item by category
'''
result = []

for item in self.inventory:
if item.category == category:
result.append(item)

if len(result) == 0:
return None

return max(result, key = lambda c: c.condition)

Choose a reason for hiding this comment

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

Nice! Snazzy use of lambda!


# condition_check = 0.0
# best_in_category = []

# for item in self.inventory:
# if item.category == category and item.condition > condition_check:
# condition_check = item.condition
# best_in_category.clear()
# best_in_category.append(item)

# return best_in_category[0]



def swap_best_by_category(self, other, my_priority, their_priority):
'''
takes the best item by category for two vendors and swaps them with each other
'''
# Helpers
my_best_category = self.get_best_by_category(their_priority)
their_best_category = other.get_best_by_category(my_priority)

Choose a reason for hiding this comment

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

🎉


if len(self.inventory) == 0 or len(other.inventory) == 0 or my_best_category is None or their_best_category is None:

Choose a reason for hiding this comment

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

This conditional only needs to check if the best_category items are not None, because the length of the inventory doesn't tell us anything about the number of items they have in that category.

Suggested change
if len(self.inventory) == 0 or len(other.inventory) == 0 or my_best_category is None or their_best_category is None:
if my_best_category is None or their_best_category is None:

return False

self.swap_items(other, my_best_category, their_best_category)
return True


## WIP ##
'''
def swap_by_newest(self, other, my_newest, their_newest):
result = swap_best_by_category(other, my_newest, their_newest)

return result
'''

4 changes: 2 additions & 2 deletions tests/integration_tests/test_wave_01_02_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from swap_meet.vendor import Vendor
from swap_meet.item import Item

@pytest.mark.skip
@pytest.mark.integration_test
# @pytest.mark.skip
# @pytest.mark.integration_test
def test_integration_wave_01_02_03():
# make a vendor
vendor = 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
14 changes: 8 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,9 @@ def test_removing_not_found_is_false():

result = vendor.remove(item)

raise Exception("Complete this test according to comments below.")
# raise Exception("Complete this test according to comments below.")
assert result == False

Choose a reason for hiding this comment

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

I suggest adding a check to verify that the inventory wasn't changed by the remove action:

Suggested change
assert result == False
assert result == False
assert len(vendor.inventory) == 3


# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
9 changes: 5 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,8 @@ 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.")
assert len(items) == 0
# *********************************************************************
# ****** 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
Loading