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

Sharks, Kassidy Buslach Swap Meet #97

Open
wants to merge 2 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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ In Wave 5 we will create three additional modules with three additional classes:
- Has an attribute `category` that is `"Decor"`
- Its stringify method returns `"Something to decorate your space."`
- `Electronics`

- Has an attribute `category` that is `"Electronics"`
- Its stringify method returns `"A gadget full of buttons and secrets."`

Expand Down
12 changes: 10 additions & 2 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
class Clothing:
pass
from swap_meet.item import Item


class Clothing(Item):
def __init__(self,category ='', condition = 0):

Choose a reason for hiding this comment

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

White space should be consistent with the Python style guide.

Here we have some missing white spaces. this line can be refactored to look like:

def __init__(self, category = '', condition = 0)

Where there is white space after the comma and white space after the equal sign

You can read more here if you'd like: https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements

super().__init__(category = "Clothing", condition = condition)
def __str__(self, category = "Clothing"):
category = "The finest clothing you could wear."
return category
Comment on lines +8 to +9

Choose a reason for hiding this comment

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

Feel free to directly return the string literal like this:

 def __str__(self):
       return "The finest clothing you could wear."


12 changes: 10 additions & 2 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
class Decor:
pass
from pyparsing import condition_as_parse_action

Choose a reason for hiding this comment

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

You can remove unused imports to keep your code clean

from swap_meet.item import Item
class Decor(Item):
def __init__(self, condition = 0 ):
super().__init__(category= "Decor", condition = condition)
def __str__(self, category= "Decor"):
self.category = "Something to decorate your space."
return self.category


15 changes: 13 additions & 2 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
class Electronics:
pass
from sqlite3 import connect

Choose a reason for hiding this comment

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

Remove unused imports

from swap_meet.item import Item
class Electronics(Item):
def __init__(self, condition = 0):
super().__init__(category= "Electronics", condition = condition)

def __str__(self, category= "Electronics"):
self.category = "A gadget full of buttons and secrets."
return self.category




26 changes: 25 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@

from swap_meet.vendor import Vendor

class Item:
pass
def __init__(self, category = "", condition = 0):
self.category = category
self.condition = condition

def __str__(self):
item ="Hello World!"

Choose a reason for hiding this comment

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

Whitespace should be added after equal sign for consistency
item = "Hello World!"

return item

def condition_description(self):
if self.condition == 0:
return "About to fall to pieces."
elif self.condition == 1:
return "This is a fixer-upper."
elif self.condition == 2:
return "Needs a little bit of love."
elif self.condition == 3:
return "It works, but has been well loved."
elif self.condition == 4:
return "Good condition"
elif self.condition == 5:
return "Pristine, impeccable, immaculate!!!"
Comment on lines +14 to +25

Choose a reason for hiding this comment

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

🙂


77 changes: 76 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,77 @@

class Vendor:
pass
def __init__(self, inventory= None):
inventory = inventory if inventory is not None else []
self.inventory = inventory
Comment on lines +4 to +5

Choose a reason for hiding this comment

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

Nice ternary here, you can do the assignment all in one line like this too:
self.inventory = inventory if inventory is not None else []



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

def remove(self, item_to_remove):
if not self.inventory:
return False
Comment on lines +13 to +14

Choose a reason for hiding this comment

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

Nice guard clause

elif item_to_remove in self.inventory:
item_index = self.inventory.index(item_to_remove)
popped_item = self.inventory.pop(item_index)
return popped_item
else:
return False
Comment on lines +15 to +20

Choose a reason for hiding this comment

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

Since you already have a guard clause then you don't need line 15, 19, 20. Getting rid of your elif statement means that the logic to remove something can be unindented one level. Also note that you can directly remove item_to_remove from self.inventory using the remove method instead of getting the index and then using pop

The refactor would look like this:

def remove(self, item_to_remove):
        if not self.inventory:
            return False
       
        self.inventory.remove(item_to_remove) 
        return item_to_remove


def get_by_category(self, category):
category_list = []
for item in self.inventory:
if item.category == category:
category_list.append(item)
return category_list
Comment on lines +23 to +27

Choose a reason for hiding this comment

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

Nice!. This method is a great candidate for using list comprehension if you want to refactor it since our conditional statement we check is pretty simple.

General syntax for list comp:

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]

You can also forgo saving the list to the variable category_list and do something like:

def get_by_category(self, category):
    return [item for item in self.inventory if item.category == category]


def swap_items(self, Vendor, my_item, their_item):
Copy link

@yangashley yangashley Apr 11, 2022

Choose a reason for hiding this comment

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

Here you passed in the class Vendor instead of a variable vendor. Maybe this was an accident because of VSCode autocompleting the word?

In Python we don't need to specify types so the Vendor class can just be the variable vendor (with a lowercase v). If you really want to ensure that the parameter actually is a Vendor, you can add an assert isinstance(vendor, Vendor) or if not isinstance(vendor, Vendor): raise TypeError

But that's not recommended in Python.

The point of "duck typing" is that you write a function that takes "anything that acts like a Vendor instance" rather than a function that takes "a Vendor instance".

Here's a resource on duck typing if you're interested in reading more: https://realpython.com/lessons/duck-typing/#:~:text=Duck%20typing%20is%20a%20concept,a%20given%20method%20or%20attribute.

if my_item not in self.inventory or their_item not in Vendor.inventory:
return False
else:
Vendor.inventory.append(self.remove(my_item))
self.inventory.append(Vendor.remove(their_item))
return True
Comment on lines +33 to +35

Choose a reason for hiding this comment

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

Since you have a guard clause, you don't need this dangling else block and lines 33-35 can be unindented one level


def swap_first_item(self, Vendor):
if not Vendor.inventory or not self.inventory:
return False
else:

Choose a reason for hiding this comment

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

Since you have a guard clause, you can remove the dangling else

self.swap_items(Vendor, self.inventory[0], Vendor.inventory[0])

Choose a reason for hiding this comment

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

Nice work reusing your swap_items method

return True

"""Refactored Swap_first_item() post Swap Meet Wrap up
def swap_first_item(self, Vendor):
if not Vendor.inventory or not self.inventory:
return False
else:
ven_inventory = Vendor.inventory[0]
self_inventory = self.inventory[0]
Vendor.inventory.append(self.remove(self_inventory))
self.inventory.append(Vendor.remove(ven_inventory))
return True"""


def get_best_by_category(self, category):
categorized_list= self.get_by_category(category)
if not categorized_list:
return None
best_condition = categorized_list[0]
for item in categorized_list:
if item.condition > best_condition.condition:
best_condition = item
return best_condition
Comment on lines +58 to +64

Choose a reason for hiding this comment

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

Nice! What you have here works!

Suggesting another way you might see this written. A refactor can remove lines 58 and 59. To do so, you'd need to assign best_condition to None instead of the first element in categorized_list.

If best_condition is None and if categorized_list is empty, then nothing happens in the for loop and None is returned on line 64 because best_condition was never reassigned.

If categorized_list is not empty then best_condition gets reassigned to item while looping.

The refactor would look like:

def get_best_by_category(self, category):
        categorized_list = self.get_by_category(category)
      
        best_condition = None
        for item in categorized_list:
            if item.condition > best_condition.condition:
                best_condition = item
        return best_condition 



def swap_best_by_category(self, other, my_priority, their_priority):
vendors_best= other.get_best_by_category(my_priority)

Choose a reason for hiding this comment

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

Add a whitespace to the left of the equal sign for consistency

my_best = self.get_best_by_category(their_priority)
if not vendors_best or not my_best:
return False
else:
self.swap_items(other, my_best, vendors_best)
return True
Comment on lines +70 to +74

Choose a reason for hiding this comment

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

Because of how swap_items is implemented, checking if my_best and vendors_best are valid here isn't necessary. See line 30 in vendor.py - you have a check in there that makes sure the args passed into swap_items() are valid.

Also swap_items() returns True if swapping happened and False if no swapping happened. Therefore, you can leverage the return value from swap_items() and refactor this method so it looks like this:

def swap_best_by_category(self, other, my_priority, their_priority):
    vendors_best = other.get_best_by_category(my_priority)       
    my_best = self.get_best_by_category(their_priority)
    return self.swap_items(other, my_best, vendors_best)




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
17 changes: 11 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,12 @@ 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
assert len(vendor.inventory) == 3
assert "a" in vendor.inventory
assert "b" in vendor.inventory
assert "c" in vendor.inventory
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
13 changes: 9 additions & 4 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,12 @@ 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.")
assert bool(items) == False
assert item_a not in items
assert item_b not in items
assert item_c not in items
assert not items
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
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
10 changes: 5 additions & 5 deletions tests/unit_tests/test_wave_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
from swap_meet.decor import Decor
from swap_meet.electronics import Electronics

@pytest.mark.skip
# @pytest.mark.skip
def test_clothing_has_default_category_and_to_str():
cloth = Clothing()
assert cloth.category == "Clothing"
assert str(cloth) == "The finest clothing you could wear."

@pytest.mark.skip
# @pytest.mark.skip
def test_decor_has_default_category_and_to_str():
decor = Decor()
assert decor.category == "Decor"
assert str(decor) == "Something to decorate your space."

@pytest.mark.skip
# @pytest.mark.skip
def test_electronics_has_default_category_and_to_str():
electronics = Electronics()
assert electronics.category == "Electronics"
assert str(electronics) == "A gadget full of buttons and secrets."

@pytest.mark.skip
# @pytest.mark.skip
def test_items_have_condition_as_float():
items = [
Clothing(condition=3.5),
Expand All @@ -31,7 +31,7 @@ def test_items_have_condition_as_float():
for item in items:
assert item.condition == pytest.approx(3.5)

@pytest.mark.skip
# @pytest.mark.skip
def test_items_have_condition_descriptions_that_are_the_same_regardless_of_type():
items = [
Clothing(condition=5),
Expand Down
Loading