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

C17 Otters Vera Sazonova #93

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
11 changes: 9 additions & 2 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
class Clothing:
pass
from .item import Item

class Clothing(Item):
def __init__(self, condition = 0.0):
super().__init__(category = "Clothing", condition = condition)

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

11 changes: 9 additions & 2 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
class Decor:
pass
from .item import Item

class Decor(Item):
def __init__(self, condition = 0.0):
super().__init__(category = "Decor", condition = condition)

def __str__(self):
return "Something to decorate your space."

11 changes: 9 additions & 2 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
class Electronics:
pass
from .item import Item

class Electronics(Item):
def __init__(self, condition = 0.0):
super().__init__(category = "Electronics", condition = condition)

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

38 changes: 37 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
class Item:
pass
def __init__(self, category = None, condition = 0.0):
if category is None:
category = ""

Choose a reason for hiding this comment

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

Strings are immutable objects so it's safe to set an empty string as a default value in your parameters :)

self.category = category
self.condition = condition


def __repr__(self):

Choose a reason for hiding this comment

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

Love that you're using the repr() function!

'''Return name of category in the
terminal, when instance is assign'''

return self.category


def __str__(self):
'''Return string "Hello World!" if
category is not defined'''

return "Hello World!"


def condition_description(self):
'''Method assigning specific
string-output depends of condition'''

if 0 < self.condition <= 1:
condition_description = "Poor"
elif 1 < self.condition <= 2:
condition_description = "Fair"
elif 2 < self.condition <= 3:
condition_description = "Good"
elif 3 < self.condition <= 4:
condition_description = "Like New"
elif 4 < self.condition <= 5:
condition_description = "New"
return condition_description

97 changes: 95 additions & 2 deletions swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,95 @@
class Vendor:
pass
class Vendor():
def __init__(self,inventory = None):
if inventory is None:
inventory = []
self.inventory = inventory


def add(self,item):
'''Instance method add, which

Choose a reason for hiding this comment

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

Nice usage of docstrings! 👏

add itens in the inventory list'''

self.inventory.append(item)
return item


def remove(self,item):
'''Instance method remove which
remove item from inventory list'''

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


def get_by_category(self,category):
'''Instance method which return
a list of items in the inventory
with that category'''

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


def swap_items(self, friend, my_item, friends_item):
'''Instance method implement swaping process between vendor and
other person'''

if my_item not in self.inventory or friends_item not in friend.inventory:
return False
self.remove(my_item)
friend.add(my_item)
friend.remove(friends_item)
self.add(friends_item)
return True


def swap_first_item(self, friend):
'''Instance method implement swaping process first item'''

if len(self.inventory) == 0 or len(friend.inventory) == 0:

Choose a reason for hiding this comment

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

small note - it's more Pythonic to check if a list is empty by doing if not list rather than if len(list) == 0

return False
self.swap_items(friend,self.inventory[0],friend.inventory[0])

Choose a reason for hiding this comment

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

Nice usage of self.swap_items()

return True


def get_best_by_category(self, consider_category):
'''Instance method will get the item with the best
condition in a certain category'''

best_dict_category = {}
best_dict_category[consider_category] = 0.0

Choose a reason for hiding this comment

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

small note - you can create a dictionary and specify its values in the same line. So, line 64 could be best_dict_category = {consider_category: 0.0} and you wouldn't need line 65.

# creating counter to check no matches categories in vendors inventory
count = 0
# consider given represents string of category, add to dictionary
# matching category is key and value is rate. Setup first element
# of dictionary like max and matching other rate, if higher add to dict
for category_from_inv in self.inventory:

Choose a reason for hiding this comment

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

small note - variable names should describe the data they are holding. category_from_inv is holding items, not their categories. If you were working in a team of developers, this could cause confusion for other people. Something you might find helpful - before I consider any code I write finished, I like to think about how easy it would be for someone else to pick up my code and understand it.

if consider_category == category_from_inv.category:
count += 1
if category_from_inv.condition > best_dict_category[consider_category]:
best_dict_category[category_from_inv.category] = category_from_inv.condition
best_category = category_from_inv
if count == 0:
return None
return best_category

Choose a reason for hiding this comment

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

Something to consider - Python doesn't create a local scope for loops which is why you can return best_category outside of the loop. However, most other languages don't allow access to variables created inside of loops. Going forward, it could be good to treat loops as having a local scope to build that habit.



def swap_best_by_category(self, other, my_priority, their_priority):
'''Instance method will swap the item with the best condition
in a certain category'''

if len(self.inventory) and len(other.inventory) != 0:

Choose a reason for hiding this comment

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

  1. Careful with how you are combining conditional statements with and. The != 0 check isn't being applied to len(self.inventory). In order to apply it to both lists, this should be written as if len(self.inventory) != 0 and len(other.inventory) != 0:. Python knows len(self.inventory) evaluates to a truthy value, which is why your version still works.
  2. This line of code can be simplified to be if self.inventory and other.inventory: because Python knows to evaluate an empty list as False. This way is also considered more Pythonic.
  3. Consider if this conditional statement is even necessary. Input validation is great but sometimes the rest of the code is set up in a way that it isn't necessary to validate the input. Does the get_best_by_category() method know how to handle empty lists? If so, is it necessary to check that the list isn't empty here?

best_item_for_friend = self.get_best_by_category(their_priority)
best_item_for_vendor = other.get_best_by_category(my_priority)
if best_item_for_friend is None or best_item_for_vendor is None:
return False
else:
self.swap_items(other,best_item_for_friend,best_item_for_vendor)
return True
return False

4 changes: 2 additions & 2 deletions tests/integration_tests/test_wave_01_02_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from swap_meet.vendor import Vendor
from swap_meet.item import Item

@pytest.mark.skip
@pytest.mark.integration_test
# @pytest.mark.skip
# @pytest.mark.integration_test

Choose a reason for hiding this comment

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

In the future, don't worry about commenting out @pytest.mark.integration. This decorator just specifies that these tests are integration tests so they should run after all the unit tests have been executed. :)

def test_integration_wave_01_02_03():
# make a vendor
vendor = Vendor()
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_tests/test_wave_04_05_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from swap_meet.decor import Decor
from swap_meet.electronics import Electronics

@pytest.mark.skip
@pytest.mark.integration_test
# @pytest.mark.skip
# @pytest.mark.integration_test
def test_integration_wave_04_05_06():
camila = Vendor()
valentina = Vendor()
Expand Down
15 changes: 9 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,10 @@ 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 len(vendor.inventory) == 3
assert item not in vendor.inventory
assert result == False
# *********************************************************************
# ****** 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 len(items) == 0
assert items == []
assert item_a not in items
assert item_c not in items
assert item_b not in 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
Loading