-
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
swap-meet, C17, Sea Turtles, Shelby Faulconer #109
base: master
Are you sure you want to change the base?
Changes from all commits
608aca8
4cd5d39
31c51b1
5f0cc2d
0033577
6398165
b4d3ee2
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,3 +1,4 @@ | ||
|
||
# Swap Meet | ||
|
||
## Skills Assessed | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
class Clothing: | ||
pass | ||
from .item import Item | ||
|
||
class Clothing(Item): | ||
|
||
def __init__(self, condition = 0): | ||
super().__init__(category = "Clothing", condition = condition) | ||
self.condition = condition | ||
|
||
|
||
def __str__(self): | ||
return "The finest clothing you could wear." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
class Decor: | ||
pass | ||
from .item import Item | ||
|
||
class Decor(Item): | ||
|
||
def __init__(self, condition = 0): | ||
super().__init__(category= "Decor", condition = condition) | ||
|
||
|
||
def __str__(self): | ||
return "Something to decorate your space." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
class Electronics: | ||
pass | ||
from .item import Item | ||
|
||
class Electronics(Item): | ||
|
||
def __init__(self, condition = 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. The subclasses look great! |
||
super().__init__(category= "Electronics", condition = condition) | ||
|
||
|
||
def __str__(self): | ||
return "A gadget full of buttons and secrets." |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,2 +1,23 @@ | ||||||||||
CONDITION_DESCRIPTION = {0:"One person's trash is another's treasure.", | ||||||||||
1:"Probably not it's first swap meet.", | ||||||||||
2:"Stained but functional", | ||||||||||
3:"Used enough to know it works juuust right.", | ||||||||||
4:"Stored for ages and finally seeing the light.", | ||||||||||
5:"The tag is still on it."} | ||||||||||
|
||||||||||
class Item: | ||||||||||
pass | ||||||||||
|
||||||||||
def __init__(self, category = "", condition = 0): | ||||||||||
self.category = category | ||||||||||
self.condition = condition | ||||||||||
|
||||||||||
|
||||||||||
def __str__(self): | ||||||||||
return "Hello World!" | ||||||||||
|
||||||||||
|
||||||||||
def condition_description(self): | ||||||||||
for key, value in CONDITION_DESCRIPTION.items(): | ||||||||||
if self.condition is key: | ||||||||||
return value | ||||||||||
Comment on lines
+20
to
+22
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. Since
Suggested change
Since we don't currently check if the condition passed to an Item is strictly between 0-5, we might want to wrap it in a try/except in case we get a KeyError. Something else to consider: what would happen if we tried to get the description for an Item with a condition of 3.5? There are tests that set the condition to 3.5, but don't try to read the description. The description test checks for a condition of 1 and a description of 5, but we should still make sure our code covers any other reasonable conditions, including floating point values (since we've seen them used in other tests). How could we modify this (slightly) to cover the values between the whole numbers? |
||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,2 +1,88 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
from unicodedata import category | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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. It looks like VS Code might have been a little overzealous with auto-imports. We should check for and remove imports that are unused as part of our code clean up when we think we're about done. |
||||||||||||||||||||||||||||||||||||||||||||||||||||
from swap_meet.item import Item | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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. Because of the way python handles typing and looking up attributes & methods, if we do not directly reference a class or its constructor, we do not need to import the class into another file, even if it uses instances of that type. Ansel shared out a slide deck on Slack that talks about how this works that could be helpful if you haven't seen it yet: https://docs.google.com/presentation/d/1PMSNIHgfBViYQ_MlP59qAm5ylT9njZfS9YQ6-xsvKhU/edit?usp=sharing Here in |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
class Vendor: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
def __init__(self, inventory = None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if not inventory: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
self.inventory = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
self.inventory = inventory | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+7
to
+10
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. Nice handling of the self.inventory = inventory if inventory else [] This can be read as "If the parameter inventory is truthy, assign self.inventory to inventory, else assign self.inventory to an empty list". The general blueprint for a ternary operator looks like: The solution above will ignore if the user passes in an empty list (since an empty list is falsy) and create a new empty list. If we're worried about space and want to avoid that, we could be more explicit with our check for self.inventory = inventory if inventory is not None else [] More info: https://book.pythontips.com/en/latest/ternary_operators.html |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
def add(self, item): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
self.inventory.append(item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return item | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
def remove(self, item): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if item in self.inventory: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
self.inventory.remove(item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return item | ||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+19
to
+23
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. I like inverting the logic of the condition to be able to treat it like a guard clause, which allows us to outdent the main logic of the function something like the following: if item not in self.inventory:
return False
self.inventory.remove(item)
return item Another approach we could take is to try to remove the item directly, and handle the |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
def get_by_category(self, category): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if not category: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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. I like this check, since it prevents us from looping over the list if the category doesn't exist! I would suggest returning an empty list here so that the function returns the same type of data in success or failure scenarios, making it easier for users to reason about what they can expect from the function. We could also return None instead since it's a common enough pattern to return None in place of an object if that object does not exist or cannot be created. |
||||||||||||||||||||||||||||||||||||||||||||||||||||
category_list = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
for item in self.inventory: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if item.category == category: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
category_list.append(item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+29
to
+32
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. Notice this pattern of: result_list = []
for element in source_list:
if some_condition(element):
result_list.append(element) can be rewritten using a list comprehension as: result_list = [element for element in source_list if some_condition(element)] Which here would look like: category_list = [item for item in self.inventory if item.category == category] At first, this may seem more difficult to read, but comprehensions are a very common python style, so try getting used to working with them! |
||||||||||||||||||||||||||||||||||||||||||||||||||||
return category_list | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
def swap_items(self, friend, my_item, their_item): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if my_item not in self.inventory: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if their_item not in friend.inventory: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+37
to
+40
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. We could use
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
self.inventory.remove(my_item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
friend.inventory.append(my_item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+42
to
+43
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
friend.inventory.remove(their_item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
self.inventory.append(their_item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
if their_item not in self.inventory: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return True | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+42
to
+50
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. Since we can assume that the python built-in functions
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
def swap_first_item(self, friend): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if self.inventory == []: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
my_item = self.inventory[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if friend.inventory == []: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
their_item = friend.inventory[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+54
to
+61
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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
# use swap_items method | ||||||||||||||||||||||||||||||||||||||||||||||||||||
self.swap_items(friend, my_item, their_item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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. Nice code reuse! Something to consider: by using self.inventory[0], friend.inventory[0] = friend.inventory[0], self.inventory[0] |
||||||||||||||||||||||||||||||||||||||||||||||||||||
return True | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
def get_best_by_category(self, category): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
desired_category = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
for item in self.inventory: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if category in item.category: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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. I recommend using the
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
desired_category.append(item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
# lambda will sort objects by instance's "condtion" attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||||
sort_list = sorted(desired_category, key=lambda item: item.condition) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+73
to
+74
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. We should line up comments with the code they refer to. This makes it easier to see at a glance the indentation (and thus various scopes) across a file.
Suggested change
Comment on lines
+70
to
+74
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. Because line 74 is inside the for loop, every time an item is added to
Suggested change
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 a lambda function!
Comment on lines
+68
to
+74
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. Since we're building up a list of items with a particular category we could reuse the function
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
if desired_category == []: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return None | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return sort_list[-1] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+74
to
+77
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. We can keep
Suggested change
If we wanted to keep
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
def swap_best_by_category(self, other, my_priority, their_priority): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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 approach for this function! |
||||||||||||||||||||||||||||||||||||||||||||||||||||
# created variables for self and other to use the get_best_by_category method | ||||||||||||||||||||||||||||||||||||||||||||||||||||
their_desired_item = self.get_best_by_category(their_priority) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
my_desired_item = other.get_best_by_category(my_priority) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
if their_desired_item is None or my_desired_item is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||
self.swap_items(other, their_desired_item, my_desired_item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return True |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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" | ||
|
@@ -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( | ||
|
@@ -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( | ||
|
@@ -49,7 +49,5 @@ 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 ********** | ||
# ********************************************************************* | ||
# raise Exception("Complete this test according to comments below.") | ||
assert result == False | ||
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 assert is great for confirming the return value is correct, but what else could we assert to make sure there were no side affects like changes to the inventory? |
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.
We can remove this line like in the
Decor
&Electronics
classes since thecondition
attribute is set by thesuper()
dunder init function.