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

Whales C17 - Julie Warren #94

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
Show all changes
16 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
26 changes: 24 additions & 2 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
class Clothing:
pass
from .item import Item

class Clothing(Item):
'''
A sub-class of Item, indicating a type of item a vendor might have.

Attributes:
condition (float): optional descr. of item condition, 0 if not defined
category (str): description of item category, always set to "Clothing"
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.

Yay docstrings! Great use of them here and throughout your code!

'''

def __init__(self, condition=0):
'''
Parameter: condition (float): optional description of item
condition, 0 if not defined
'''

self.category = "Clothing"
self.condition = condition

def __str__(self):
'''Stringifies Clothing and returns a string describing the class.'''

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

class Decor(Item):
'''
A sub-class of Item, indicating a type of item a vendor might have.

Attributes:
condition (float): optional descr. of item condition, 0 if not defined
category (str): description of item category, always set to "Decor"
'''

def __init__(self, condition=0):
'''
Parameter: condition (float): optional description of item
condition, 0 if not defined
'''

self.category = "Decor"
self.condition = condition
Comment on lines +18 to +19

Choose a reason for hiding this comment

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

Great! Another option that works is calling the constructor of the parent (Item) class like super().__init__(condition=condition, category='Decor')


def __str__(self):
'''Stringifies Decor and returns a string describing the class.'''

return "Something to decorate your space."
27 changes: 25 additions & 2 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
class Electronics:
pass
from .item import Item

class Electronics(Item):
'''
A sub-class of Item, indicating a type of item a vendor might have.

Attributes:
condition (float): optional descr. of item condition, 0 if not defined
category (str): description of item category, always set to "Electronics"
'''

def __init__(self, condition=0):
'''
Parameter: condition (float): optional description of item
condition, 0 if not defined
'''

self.category = "Electronics"
self.condition = condition


def __str__(self):
'''Stringifies Electronics and returns a string describing the class.'''

return "A gadget full of buttons and secrets."
47 changes: 46 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,47 @@
class Item:
pass
'''
A class indicating an object Item that a vendor might have.

Attributes:
category (str): optional descr. of item category. None if not defined,
w/attribute set to an empty string. Otherwise set to input param.
condition (float): optional descr. of item condition, 0 if not defined
Comment on lines +3 to +8

Choose a reason for hiding this comment

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

Nice details!

'''

def __init__(self, category=None, condition=0):
'''
Parameters:
category (str): optional descr. of item category. None if not defined,
w/attribute set to an empty string. Otherwise set to input param.
condition (float): optional descr. of item condition, 0 if not defined
'''

if category is None:

Choose a reason for hiding this comment

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

👍🏻

self.category = ""
else:
self.category = category
self.condition = condition

def __str__(self):
'''Stringifies Item and returns a string to say hello.'''

return "Hello World!"

def condition_description(self):
'''
Method to return string description related to item condition.
Returns: a string for default condition of 0, and any float up to 5.0
'''

if self.condition == 0:
return "I'm not sure about the condition of this item."
elif self.condition <= 1:
return "Why are you even swapping this?"
elif self.condition <= 2:
return "You could do better."
elif self.condition <= 3:
return "Blisteringly mediocre."
elif self.condition <= 4:
return "I'll accept this. I guess."
elif self.condition <= 5:
return "Wow, this is amazing!"
121 changes: 120 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,121 @@
class Vendor:
pass
'''
A class for a Vendor at a swap-meet who has items. The Vendor may want to
trade with other vendors based on item category, condition, or both.
'''

def __init__(self, inventory=None):
'''
Parameter: inventory (list): optional list of vendor inventory.
None if not defined, w/attribute set to an empty string.
Otherwise set to input parameter.
'''

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

def add(self, item):
'''
Adds an item to a vendor's inventory
Parameter: item (string, or class object): represents a swappable item
Returns: the added item
'''

self.inventory.append(item)
return item

def remove(self, item):
'''
Removes an item from a vendor's inventory
Parameter: item (string, or class object): represents a swappable item
Returns: the removed item if removed, otherwise False
'''

if item in self.inventory:
self.inventory.remove(item)
return item
else:
return False

def get_by_category(self, category):
'''
Gets a list of every item a vendor has that is in a certain category
Parameter: category (string): represents the category of a swappable item
Returns: a list of items with the correct desired category
'''

items_in_category = []
for item in self.inventory:
if item.category == category:
items_in_category.append(item)
return items_in_category

def swap_items(self, other_vendor, self_item, their_item):
'''
Facilitates a swap between two vendors for two given items, and updates
each vendor's inventory list accordingly.
Parameters:
other_vendor (another Vendor instance): vendor to swap with
self_item (string): an item in the Vendor's own inventory
their_item (string): an item in the other Vendor's inventory
Returns: True if the swap was completed, otherwise False
'''

if self_item in self.inventory and their_item in other_vendor.inventory:
self.remove(self_item)
other_vendor.add(self_item)
self.add(their_item)
other_vendor.remove(their_item)
return True
else:
return False

def swap_first_item(self, other_vendor):
'''
Facilitates a swap of the first inventory item for two vendors
and updates each vendor's inventory list accordingly.
Parameter: other_vendor (another Vendor instance): vendor to swap with
Returns: True if the swap was completed, otherwise False
'''

if len(self.inventory) >= 1 and len(other_vendor.inventory) >= 1:
self.swap_items(other_vendor, self.inventory[0], other_vendor.inventory[0])
return True

Choose a reason for hiding this comment

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

Yes! You could also take advantage of the fact that .swap_items returns True or False and return self.swap_items(...,...)

else:
return False

def get_best_by_category(self,category):
'''
For a given item category, searches a vendor's inventory to find and
return the item in that category with the highest rated condition.
Parameter: category (string): a string attribute for an item's type
Returns: The highest rate item in that category, returns none if no
item in that category
'''

items_in_category = self.get_by_category(category)
if len(items_in_category) > 0:
items_in_category.sort(key=lambda item: item.condition, reverse=True)

Choose a reason for hiding this comment

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

Very nice!

return items_in_category[0]
return None

def swap_best_by_category(self, other, my_priority, their_priority):
'''
Facilitates a swap between two vendors for the highest rated item
of a desired category that the vendor specifies
Parameters:
other (another Vendor instance): vendor to swap with
my_priority (string): the desired type of item to swap for
their_priority (string): the other vendor's desired type
of item to swap for
Returns: True if the swap was completed, otherwise False
'''

best_their_priority = self.get_best_by_category(their_priority)
best_my_priority = other.get_best_by_category(my_priority)
if best_their_priority and best_my_priority:
self.swap_items(other, best_their_priority, best_my_priority)

Choose a reason for hiding this comment

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

Awesome use of helper functions!

return True
return False
1 change: 0 additions & 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,6 @@
from swap_meet.vendor import Vendor
from swap_meet.item import Item

@pytest.mark.skip
@pytest.mark.integration_test
def test_integration_wave_01_02_03():
# make a vendor
Expand Down
1 change: 0 additions & 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,6 @@
from swap_meet.decor import Decor
from swap_meet.electronics import Electronics

@pytest.mark.skip
@pytest.mark.integration_test
def test_integration_wave_04_05_06():
camila = Vendor()
Expand Down
17 changes: 8 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
# Wave 1 / Test 1
def test_vendor_has_inventory():
vendor = Vendor()
assert len(vendor.inventory) == 0

@pytest.mark.skip
# Wave 1 / Test 2
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
# Wave 1 / Test 3
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
# Wave 1 / Test 4
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
# Wave 1 / Test 5
def test_removing_not_found_is_false():
item = "item to remove"
vendor = Vendor(
Expand All @@ -49,7 +49,6 @@ 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 item not in vendor.inventory
assert len(vendor.inventory) == 3
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.

👍🏻

11 changes: 4 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
# Wave 2 / Test 1
def test_items_have_blank_default_category():
item = Item()
assert item.category == ""

@pytest.mark.skip
# Wave 2 / Test 2
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
# Wave 2 / Test 3
def test_get_no_matching_items_by_category():
item_a = Item(category="clothing")
item_b = Item(category="clothing")
Expand All @@ -34,7 +34,4 @@ 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
Loading