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

Sapphire - April Zhang #68

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
14 changes: 12 additions & 2 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
class Clothing:
pass
import uuid
from swap_meet.item import Item
class Clothing(Item):
"""
A subclass of Item class, representing clothing item.
"""
def __init__(self, id=None, fabric="Unknown", condition=0, age=0):
super().__init__(id, condition, age)
self.fabric = fabric

def __str__(self):
return super().__str__() + f" It is made from {self.fabric} fabric."
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
import uuid
from swap_meet.item import Item
class Decor(Item):
"""
A subclass of Item class, representing Decor item.
"""
def __init__(self, id=None, width=0, length=0, condition=0, age=0):
super().__init__(id, condition, age)
self.width = width
self.length = length

def __str__(self):
return super().__str__() + f" It takes up a {self.width} by {self.length} sized space."
14 changes: 12 additions & 2 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
class Electronics:
pass
import uuid
from swap_meet.item import Item
class Electronics(Item):
"""
A subclass of Item class, representing electronic item.
"""
def __init__(self, id=None, type="Unknown", condition=0, age=0):
super().__init__(id, condition, age)
self.type = type

def __str__(self):
return super().__str__() + f" This is a {self.type} device."
52 changes: 51 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,52 @@
import uuid
class Item:
pass
"""
A class that represents item.
Each Item have:
an attribute named id, which is an unique integer as default;
an attribute named condition, which is an integer representing the item's condition, default value is 0 (the poorest);
an attribute named age, which is an integer representing the item's age, default value is 0.
"""
def __init__(self, id=None, condition=0, age=0):
# when manually set id, check if it's an integer, if not: raise TypeError.
if id:
if isinstance(id, int):
self.id = id
else:
raise TypeError("Id must be an integer.")
# when id is not provided, generate an id with integer.
else:
self.id = uuid.uuid4().int
# If condition out of range, raise ValueError.
if condition > 5:
raise ValueError("Condition value range from 0 to 5.")
Comment on lines +16 to +22
Copy link

Choose a reason for hiding this comment

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

I really like your decision to raise errors in these cases. I wonder what the unit tests for these cases would look like 😄 😄 😄

else:
self.condition = condition

self.age = age

def get_category(self):
"""
Return the class name as a string.
"""
return self.__class__.__name__

def __str__(self):
return f"An object of type {self.get_category()} with id {self.id}."

def condition_description(self):
"""
Return funny description based on the item's condition.
"""
if self.condition == 5:
return "This item is in tip-top shape!"
elif self.condition == 4:
return "This item still has plenty of life left and character to spare."
elif self.condition == 3:
return "It's not down for the count - it's still got some fight left in it!"
Copy link

Choose a reason for hiding this comment

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

💪 !!!

elif self.condition == 2:
return "Don't judge this item by its appearance. It's still got some tricks up its sleeve and plenty of use left."
elif self.condition == 1:
return "This item may not have much life left in it, but it's not quite ready to give up the ghost yet."
else:
return "Oh... :'( This item has seen better days."
130 changes: 129 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,130 @@
class Vendor:
pass
"""
A class that represents Vendor.
Each Vendor has an attribute named inventory, which is an empty list by default.
"""
def __init__(self, inventory=None):
if inventory is None:
inventory = []
self.inventory = inventory

def add(self, item):
"""
Add item to inventory, return item
"""
self.inventory.append(item)
return item

def remove(self, item):
"""
Remove the matching item in the inventory, return the item.
If no matching item, return False.
"""
if item in self.inventory:
self.inventory.remove(item)
return item
else:
return False

def get_by_id(self, id):
"""
Return the item with a matching id from the inventory.
If no matching item return None.

Args:
id (integer): Item's id.
"""
for item in self.inventory:
if id == item.id:
return item
return None

def swap_items(self, other_vendor, my_item, their_item):
"""
Swap this Vendor's item with the other Vendor's item, return True.
If either Vendor does not have a matching item, return False.

Args:
other_vendor (an instance of Vendor)
my_item (an instance of Item): representing the item this Vendor instance plans to give.
their_item (an instance of Item): representing the item the other Vendor plans to give.
"""
if my_item not in self.inventory or their_item not in other_vendor.inventory:
return False
else:
self.remove(my_item)
other_vendor.add(my_item)
other_vendor.remove(their_item)
self.add(their_item)
return True
Comment on lines +52 to +59
Copy link

Choose a reason for hiding this comment

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

Nicely done!


def swap_first_item(self, other_vendor):
"""
Swap the first item in this Vendor with the first item in the other Vendor, return True.
If either Vendor does not have a matching item, return False.
"""
if not self.inventory or not other_vendor.inventory:
return False
else:
my_first = self.inventory[0]
their_first = other_vendor.inventory[0]
self.swap_items(other_vendor, my_first, their_first)
return True

def get_by_category(self, category):
"""
Returns a list of objects in the inventory with that category.
If no matching item, return empty list.

Args:
category (string): representing a category.
"""
category_items = []
for item in self.inventory:
if item.get_category() == category:
category_items.append(item)
return category_items

def get_best_by_category(self, category):
"""
Return the item with the best condition in a certain category.
If no matching item, return None.

Args:
category (string): representing a category.
"""
if not self.get_by_category(category):
return None
else:
return max(self.get_by_category(category), key=lambda item: item.condition)

def swap_best_by_category(self, other_vendor, my_priority, their_priority):
"""
Swap the best item in this Vendor that matches their_priority category with the best item in the other Vendor that matches my_priority, return True.
If either Vendor has no matching item, return False.

Args:
other_vendor (an instance of Vendor): _description_
my_priority (string): represents a category that this Vendor wants to receive.
their_priority (string): represents a category that the other Vendor wants to receive.
"""
my_item = self.get_best_by_category(their_priority)
their_item = other_vendor.get_best_by_category(my_priority)
if not my_item or not their_item:
return False
else:
self.swap_items(other_vendor, my_item, their_item)
return True

def swap_by_newest(self, other_vendor):
"""
Swap the newest item in this Vendor with the newest item in the other Vendor, return True.
If either Vendor does not have a matching item, return False.
"""
my_newest = min(self.inventory, key=lambda item: item.age, default=False)
their_newest = min(other_vendor.inventory, key=lambda item: item.age, default=False)
Comment on lines +124 to +125
Copy link

Choose a reason for hiding this comment

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

Nicely done!

if not my_newest or not their_newest:
return False
else:
self.swap_items(other_vendor, my_newest, their_newest)
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
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
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
12 changes: 6 additions & 6 deletions tests/unit_tests/test_wave_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
from swap_meet.vendor import Vendor
from swap_meet.item import Item

@pytest.mark.skip
# @pytest.mark.skip
def test_items_have_default_uuid_length_id():
item = Item()
assert isinstance(item.id, int)
assert len(str(item.id)) >= 32

@pytest.mark.skip
# @pytest.mark.skip
def test_item_instances_have_different_default_ids():
item_a = Item()
item_b = Item()
assert item_a.id != item_b.id

@pytest.mark.skip
# @pytest.mark.skip
def test_items_use_custom_id_if_passed():
item = Item(id=12345)
assert isinstance(item.id, int)
assert item.id == 12345

@pytest.mark.skip
# @pytest.mark.skip
def test_item_obj_returns_text_item_for_category():
item = Item()
assert item.get_category() == "Item"

@pytest.mark.skip
# @pytest.mark.skip
def test_get_item_by_id():
test_id = 12345
item_custom_id = Item(id=test_id)
Expand All @@ -36,7 +36,7 @@ def test_get_item_by_id():
result_item = vendor.get_by_id(test_id)
assert result_item is item_custom_id

@pytest.mark.skip
# @pytest.mark.skip
def test_get_item_by_id_no_matching():
test_id = 12345
item_a = Item()
Expand Down
Loading