Skip to content

Vivian Zhu, C18 Cheetahs #84

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

Open
wants to merge 11 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):

Choose a reason for hiding this comment

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

Great start on adding Item as the parent class for Clothing! However, you are not fully using all of the things that parent classes offer. Rather than setting category and condition in the child class, you can send those values to the super class and it can be set there for you. You would use this line of code:

super().__init__("Clothing", condition)

and then you could remove lines 7 and 8. This works for Decor and Electronics as well!


# Wave 5
def __init__(self, condition = 0.0):
self.category = "Clothing"
self.condition = condition

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

class Decor(Item):

# Wave 5
def __init__(self, condition = 0.0):
self.category = "Decor"
self.condition = condition

#super().__init__("Decor", condition, 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):

# Wave 5
def __init__(self, condition = 0.0):
self.category = "Electronics"
self.condition = condition

def __str__(self):
return "A gadget full of buttons and secrets."
32 changes: 31 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
class Item:
pass

# Wave 2 & 5
def __init__(self, category = None, condition = 0.0):
# category & condition are the properties of Item
if category is None:
category = ""
Comment on lines +6 to +7

Choose a reason for hiding this comment

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

You do not need to use None if the field is a string! Strings are immutable, so you can have the default be "". You want to use None for things that are mutable, like lists or dictionaries.

self.category = category
self.condition = condition


# Wave 3
def __str__(self):
return "Hello World!"


# Wave 5
def condition_description(self, condition = 0.0):
'''
Describe the condition in words based on the value.
'''
if self.condition <= 1:
return "Uhhh"
elif self.condition <= 2 and self.condition >= 1:

Choose a reason for hiding this comment

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

Since you use elif, you can make your statements cleaner. For instance, you don't need to use >= 1 here because you know in order to get here the condition has to be higher than 1 because it skipped the first if.

return "Bad Enough"
elif self.condition <= 3 and self.condition >= 2:
return "So so"
elif self.condition <= 4 and self.condition >= 5:
return "Nice Enough"
elif self.condition <= 5:
return "Sparkling"

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

class Vendor:
pass
# def __init__(self, inventory = []):
# default value None is IMMUTABLE. MUTABLE type should not be used in DEFAULT argument.

# Wave 1
def __init__(self, inventory = None):

Choose a reason for hiding this comment

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

Excellent!

if inventory is None:
inventory = []
self.inventory = inventory

def add(self, add_item):

Choose a reason for hiding this comment

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

Excellent!

'''
Returns the item that was added
'''
self.inventory.append(add_item)
return add_item

def remove(self, remove_item):
'''
Returns the item that was removed
'''
if remove_item in self.inventory:
self.inventory.remove(remove_item)
return remove_item
else:

Choose a reason for hiding this comment

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

This else is not necessary! Since the if has a return statement, you know that if the code goes past the if we must be in whatever did not fit in the if. You can just have return False by itself without the else.

return False

# Wave 2
def get_by_category(self, category_str):

Choose a reason for hiding this comment

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

Excellent!

'''
Returns a list of Strings (items) in the inventory with given category.
'''
same_category_list = []
for item in self.inventory:
# does not need import at this point
# cause of "dynamic type" does not care about ".category" until it sees Item class
# varies in different languages
if item.category == category_str:
same_category_list.append(item)
return same_category_list


# Wave 3
def swap_items(self, swap_vendor, my_item, their_item):

Choose a reason for hiding this comment

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

Excellent!

'''
Returns True if swap items action is successful.
Otherwise, returns False.
'''
if their_item in swap_vendor.inventory and my_item in self.inventory:
# Mistake made; using swap_vendor.inventory
# Class methods must be called on a class objects, not a list
swap_vendor.remove(their_item)
self.add(their_item)
self.remove(my_item)
swap_vendor.add(my_item)
return True
# okay to delete, clear to leave it?
return False


# Wave 4
def swap_first_item(self, swap_vendor):

Choose a reason for hiding this comment

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

Great job! One thing to note: as you wrote in your docstring, this function should return False if the swap can't happen, but you don't do that! The tests still pass because by default functions return None and our test does assert not result, which works for both None and False. However, you should have a return False after the if statement.

'''
Returns True if swap first avaiable item action is successful.
Otherwise, returns False.
'''
if len(self.inventory) >= 1 and len(swap_vendor.inventory) >= 1:
return self.swap_items(swap_vendor, self.inventory[0], swap_vendor.inventory[0])


# Wave 6
def get_best_by_category(self, given_category):

Choose a reason for hiding this comment

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

Excellent!

'''
Returns the item with the best condition in a certain category.
'''
items = self.get_by_category(given_category)
if len(items) > 0:
best_item = max(items, key=lambda i:i.condition)
return best_item
return None

def swap_best_by_category(self, other, my_priority, their_priority):
'''
Returns True if swap the best item (category) action is successful.
Otherwise, returns False.
'''
# watch out for self.func or other.func
my_best_item = self.get_best_by_category(their_priority)
their_best_item = other.get_best_by_category(my_priority)
if my_best_item is not None and their_best_item is not None:
self.swap_items(other, my_best_item, their_best_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
18 changes: 9 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,7 @@ 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 **********
# *********************************************************************
assert len(vendor.inventory) == 3
assert item not in vendor.inventory
assert result == False
Comment on lines +52 to +54

Choose a reason for hiding this comment

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

Excellent!


14 changes: 7 additions & 7 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,7 @@ 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
assert item_a not in items
assert item_b not in items
assert item_c not in items
Comment on lines +38 to +40

Choose a reason for hiding this comment

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

Great asserts! These three are actually not needed. Since the first assert tells us that there is nothing in the list, we don't need to check if these individual items are in the list.

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
Loading