-
Notifications
You must be signed in to change notification settings - Fork 113
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
Otters - Nikki Torab #104
base: master
Are you sure you want to change the base?
Otters - Nikki Torab #104
Changes from all commits
0e3b752
122b93e
f579517
b7e4353
07d9e1d
c5334e5
0a1f453
594e113
5fe22f8
cd9db2b
abd7230
34cb624
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
class Clothing: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
|
||
class Clothing(Item): | ||
|
||
|
||
def __init__(self, condition = 0, age = None): | ||
self.category = "Clothing" | ||
self.condition = condition | ||
self.age = age | ||
|
||
|
||
#override str() for Clothing, returns "The finest clothing you could wear." | ||
def __str__(self): | ||
return "The finest clothing you could wear." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
class Decor: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
|
||
class Decor(Item): | ||
|
||
|
||
def __init__(self, condition = 0, age = None): | ||
self.category = "Decor" | ||
self.condition = condition | ||
self.age = age | ||
|
||
#override str() for Decor, returns "Something to decorate your space." | ||
def __str__(self): | ||
return "Something to decorate your space." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
class Electronics: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
|
||
class Electronics(Item): | ||
|
||
def __init__(self, condition = 0, age = None): | ||
self.category = "Electronics" | ||
self.condition = condition | ||
self.age = age | ||
|
||
#override str() for Electronics, returns "A gadget full of buttons and secrets." | ||
def __str__(self): | ||
return "A gadget full of buttons and secrets." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,36 @@ | ||
|
||
|
||
class Item: | ||
pass | ||
|
||
def __init__(self, category = "", condition = 0, age = None): | ||
self.category = category | ||
self.condition = condition | ||
self.age = age | ||
|
||
#override str() for Item, returns "Hello World!" instead | ||
def __str__(self): | ||
return "Hello World!" | ||
|
||
|
||
#describes condition based on the value, range from 0 to 5 | ||
def condition_description(self): | ||
|
||
if self.condition == 0: | ||
return "Terrible condition" | ||
|
||
if self.condition == 1: | ||
return "Bad condition" | ||
|
||
if self.condition == 2: | ||
return "Slightly bad condition" | ||
|
||
if self.condition == 3: | ||
return "Acceptable condition" | ||
|
||
if self.condition == 4: | ||
return "Good condition" | ||
|
||
if self.condition == 5: | ||
return "Mint conditon" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition can be a float, so it can contain a decimal. In the cases where the condition has a decimal, this function will return None. There are a few solutions, one is to cast the condition to an int, which will drop the decimal portion of the number and round down. Another possible solution is to test for a range of values (ex: |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,140 @@ | ||
from swap_meet.item import Item | ||
|
||
class Vendor: | ||
pass | ||
def __init__(self, inventory = None): | ||
#code on lines 6-7 from https://docs.python-guide.org/writing/gotchas/ | ||
if inventory == None: | ||
inventory = [] | ||
self.inventory = inventory | ||
|
||
# Adds the item to the inventory | ||
def add(self, item): | ||
self.inventory.append(item) | ||
return item | ||
|
||
|
||
# Removes the matching item from the inventory | ||
def remove(self, item): | ||
if item not in self.inventory: | ||
return False | ||
else: | ||
self.inventory.remove(item) | ||
return item | ||
|
||
|
||
# Takes one argument: a string, representing a category | ||
# Returns a list of Items in the inventory with that category | ||
def get_by_category(self, category): | ||
#self.item = Item(category) | ||
item_list = [] | ||
for item in self.inventory: | ||
if item.category == category: | ||
item_list.append(item) | ||
|
||
|
||
return item_list | ||
|
||
|
||
# removes my_item from this Vendor's inventory | ||
# adds my_item to the other Vendor's inventory | ||
# removes their_item from other Vendor's inventory | ||
# adds their_item to the this Vendor's inventory | ||
def swap_items(self, other_vendor, my_item, their_item): | ||
|
||
if (my_item not in self.inventory) or (their_item not in other_vendor.inventory): | ||
return False | ||
|
||
self.remove(my_item) | ||
other_vendor.add(my_item) | ||
|
||
other_vendor.remove(their_item) | ||
self.add(their_item) | ||
Comment on lines
+47
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of helper methods! |
||
|
||
|
||
return True | ||
|
||
|
||
#removes first item from this Vendor's inventory | ||
# adds the other Vendor's first item | ||
# removes first item from other Vendor's inventory | ||
# adds this Vendor's first item | ||
def swap_first_item(self, other_vendor): | ||
if len(self.inventory) == 0 or len(other_vendor.inventory) == 0: | ||
return False | ||
|
||
self.swap_items(other_vendor, self.inventory[0], other_vendor.inventory[0]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
|
||
return True | ||
|
||
|
||
#gets the item with the best condition in a certain category | ||
def get_best_by_category(self, category): | ||
|
||
category_inventory = self.get_by_category(category) | ||
|
||
if len(category_inventory) == 0: | ||
return None | ||
|
||
highest_rated = category_inventory[0] | ||
|
||
for item in category_inventory: | ||
|
||
if (item.condition) > (highest_rated.condition): | ||
highest_rated = item | ||
|
||
return highest_rated | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
|
||
#swap the best item of certain categories with another Vendor | ||
def swap_best_by_category(self, other, my_priority, their_priority): | ||
|
||
#find best item in my inventory that matches their_priority | ||
my_best = self.get_best_by_category(their_priority) | ||
|
||
their_best = other.get_best_by_category(my_priority) | ||
|
||
if my_best == None or their_best == None: | ||
return False | ||
|
||
self.swap_items(other, my_best, their_best) | ||
Comment on lines
+92
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fantastic code re-use! |
||
|
||
return True | ||
|
||
|
||
|
||
def swap_by_newest(self, other): | ||
my_newest = self.inventory[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the inventory is empty, this will throw an error. Consider wrapping this line in a try/except block that returns None if this line fails. |
||
for item in self.inventory: | ||
if not item.age is None: | ||
my_newest = item | ||
break | ||
if my_newest.age is None: | ||
return False | ||
|
||
for item in self.inventory: | ||
if not item.age is None: | ||
if item.age < my_newest.age: | ||
my_newest = item | ||
|
||
their_newest = other.inventory[0] | ||
for item in other.inventory: | ||
if not item.age is None: | ||
their_newest = item | ||
break | ||
if their_newest.age is None: | ||
return False | ||
|
||
for item in other.inventory: | ||
if not item.age is None: | ||
if item.age < their_newest.age: | ||
their_newest = item | ||
Comment on lines
+119
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section is the same as lines 106-117. I recommend pulling this out into a helper method. |
||
|
||
self.swap_items(other, my_newest, their_newest) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return True | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
|
@@ -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") | ||
|
@@ -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.") | ||
#raise Exception("Complete this test according to comments below.") | ||
# ********************************************************************* | ||
# ****** Complete Assert Portion of this test ********** | ||
# ********************************************************************* | ||
assert item_a not in items | ||
assert item_b not in items | ||
assert item_c not in items | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests work better when there is no room for ambiguity. In this case, checking that these items are not in the list of items is great, but it leaves room for there to be some other item that is also not wanted. Adding something like:
helps to make it clear that the items list should be empty. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will work, but it's duplicating code that is already in Item. Using the super constructor allows this function to take advantage of all of the code in Item: