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

Panthers_A18 - Lee Reyes and Sika Sarpong #85

Open
wants to merge 16 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ In Wave 4 we will write one method, `swap_first_item`.
- Instances of `Vendor` have an instance method named `swap_first_item`
- It takes one argument: an instance of another `Vendor`, representing the friend that the vendor is swapping with
- This method considers the first item in the instance's `inventory`, and the first item in the friend's `inventory`
- It removes the first item from its `inventory`, and adds the friend's first item
- It removes the first item from its `inventory`, and adds the friend's item
- It removes the first item from the friend's `inventory`, and adds the instances first item
- It returns `True`
- If either itself or the friend have an empty `inventory`, the method returns `False`
Expand Down
11 changes: 9 additions & 2 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
class Clothing:
pass
from swap_meet.item import Item

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

def __str__(self):
return "The finest clothing you could wear."

Comment on lines +3 to +9
Copy link

@audreyandoy audreyandoy Oct 26, 2022

Choose a reason for hiding this comment

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

Setting a default value will still allow us to reassign the value. In this case, every Clothing instance can change its category to 'Electronics':

pants = Clothing('Electronics', 4.0) 
print(pants.category) # returns 'Electronics'
#what are electronic pants? lol

We can move the default assignment of category to the parent super() constructor. This will ensure that every Clothing instance will be assigned 'Clothing' as its category.

pants = Clothing(4.0) 
print(pants.category) # returns clothing

11 changes: 9 additions & 2 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
class Decor:
pass
from swap_meet.item import Item


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

def __str__(self):
return "Something to decorate your space."
Comment on lines +4 to +9

Choose a reason for hiding this comment

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

Same comment about category in the child class constructor can also be applied here.

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 swap_meet.item import Item


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

def __str__(self):
return "A gadget full of buttons and secrets."
Comment on lines +4 to +9

Choose a reason for hiding this comment

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

Same comment about category in the child class constructor can also be applied here.

28 changes: 27 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
class Item:
pass
'''
All three classes and the Item class have an attribute called condition,
which can be optionally provided in the initializer. The default value
should be 0.
'''

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

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

def condition_description(self):
if self.condition == 0:
return "Ew. You don't want that."
elif self.condition == 1:
return "Maybe you need gloves for that."
elif self.condition == 2:
return "Heavily Used"
elif self.condition == 3:
return "Decent"
elif self.condition == 4:
return "Lightly Used"
elif self.condition == 5:
return "Like new!"
Comment on lines +16 to +28

Choose a reason for hiding this comment

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

🤣 lol Love the condition descriptions. "maybe you need gloves for that" is my fav.

72 changes: 71 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,72 @@
class Vendor:
pass
def __init__(self, inventory=None) -> None:
self.inventory = inventory if inventory is not None else []

def __str__(self, item):
f"{self.item}"
# return f"my_item: {self.my_item}, their_item: {self.their_item}"

def add(self, item):
self.inventory.append(item)
return item
Comment on lines +9 to +11

Choose a reason for hiding this comment

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

👍


def remove(self, item):
if item not in self.inventory:
return False
self.inventory.remove(item)
return item
Comment on lines +13 to +17

Choose a reason for hiding this comment

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

👍


def get_by_category(self, category):
category_list = [item for item in self.inventory if item.category == category]
return category_list
Comment on lines +19 to +21

Choose a reason for hiding this comment

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

👍 Nice list comprehension!


def swap_items(self, vendor, my_item, their_item):
'''If this Vendor's inventory doesn't contain my_item or the friend's inventory doesn't contain their_item,
the method returns False'''
if my_item not in self.inventory or their_item not in vendor.inventory:
return False
self.remove(my_item)
vendor.add(my_item)
vendor.remove(their_item)
self.add(their_item)
return True
Comment on lines +23 to +32

Choose a reason for hiding this comment

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

👍


def swap_first_item(self, friend):
if not self.inventory or not friend.inventory:
return False
self.swap_items(friend, self.inventory[0],friend.inventory[0])
return True
Comment on lines +34 to +38

Choose a reason for hiding this comment

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

Great use of helper functions!

Currently, swapping any items has a time complexity of O(n) due to the remove method in swap_items. We can make a subtle improvement to O(1) by using multiple assignment instead:

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

Here's more info on Python multiple assignment: https://www.w3schools.com/python/gloss_python_assign_value_to_multiple_variables.asp


def get_best_by_category(self, category):
category_list = self.get_by_category(category)
if not category_list:
return None
return max(category_list, key=lambda x: x.condition)
Comment on lines +40 to +44

Choose a reason for hiding this comment

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

👍 Great use of max() and a lambda function!


def swap_best_by_category(self, other, my_priority, their_priority):
vendor_wants = other.get_best_by_category(my_priority)
other_wants = self.get_best_by_category(their_priority)

if not vendor_wants or not other_wants:
return False

self.swap_items(other, other_wants, vendor_wants)
return True
Comment on lines +46 to +54

Choose a reason for hiding this comment

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

👍 Great use of helper functions! This solution is very clean 😄


def swap_by_newest(self, other, my_priority,their_priority):
# Our logic is the user would say what category they want,
# and then swap the newest item in that category
my_newest_list = self.get_by_category(my_priority)
other_newest_list = other.get_by_category(their_priority)

if not my_newest_list or not other_newest_list:
return None

my_newest_list.sort(key = lambda x:x.newest)
other_newest_list.sort(key = lambda x:x.newest)

self.swap(other,other_newest_list[0], my_newest_list[0])
return True
Comment on lines +56 to +69

Choose a reason for hiding this comment

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

👍 Nicely done!




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
4 changes: 2 additions & 2 deletions tests/integration_tests/test_wave_04_05_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from swap_meet.decor import Decor
from swap_meet.electronics import Electronics

@pytest.mark.skip
@pytest.mark.integration_test
# @pytest.mark.skip
# @pytest.mark.integration_test
def test_integration_wave_04_05_06():
camila = Vendor()
valentina = 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,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)

assert result is False
Comment on lines 44 to +52

Choose a reason for hiding this comment

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

👍


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

👍

# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
13 changes: 7 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,16 @@
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 +39,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 +66,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 +93,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 +113,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
16 changes: 11 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,30 @@
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 +36,8 @@ 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