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

swap_meet_Sumitra Chhetri, Scissor class #60

Open
wants to merge 13 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
20 changes: 20 additions & 0 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from swap_meet.item import Item

#wave_5
class Clothing(Item):
"""
inheritance code from method condittion_description

"""
def __init__(self, category = "Clothing", condition = 0):
self.category = "Clothing"
self.condition = condition

super().__init__("Clothing", condition)
Comment on lines +9 to +13

Choose a reason for hiding this comment

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

You've done the same work multiple times, consider refactoring like this:

Suggested change
def __init__(self, category = "Clothing", condition = 0):
self.category = "Clothing"
self.condition = condition
super().__init__("Clothing", condition)
def __init__(self, condition = 0):
super().__init__(condition, category="Clothing")


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

def condition_description(self):
return super().condition_description()

19 changes: 19 additions & 0 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from swap_meet.item import Item

#wave_5
class Decor(Item):
"""
inheritance code from method condittion_description
"""
def __init__(self, category = "Decor", condition = 0):
self.category = "Decor"
self.condition = condition

super().__init__("Decor", condition)

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

def condition_description(self):
return super().condition_description()

21 changes: 21 additions & 0 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from swap_meet.item import Item

#wave_5
class Electronics(Item):
"""
inheritance code from method condittion_description
"""
def __init__(self, category = "Electronics", condition = 0):
self.category = "Electronics"
self.condition = condition

super().__init__("Electronics", condition)

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

def condition_description(self):
return super().condition_description()



30 changes: 30 additions & 0 deletions swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#wave_2
class Item():
def __init__(self, category ="", condition=0):
self.category = category
self.conditon = condition

#wave_3
def __str__(self):
return "Hello World!"

#wave_5
def condition_description(self):
"""
input: checks the condition of items
output: the condition of items

"""
if self.conditon <= 1:
return "This is useless and need to recyle."
elif self.conditon <= 2:
return "This looks dirty and whole in them so need to clean and sew them before using."
elif self.conditon <= 3:
return "This is okay but need to clean them."
elif self.conditon <= 4:
return "This is in good condition."
elif self.condition <= 4.9:
return "Basically Brand new."
else:
return "Brand new"
112 changes: 112 additions & 0 deletions swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

#wave_1
class Vendor:
def __init__(self, inventory = None):
if inventory is None:
self.inventory = []
else:
self.inventory = inventory

def adding_to_inventory(Vendor): #checks item in inventory & returns True or False
if item in inventory:
self.inventory.add(item)
else:
self.inventory.remove(item)
return False

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

def remove(self, item): #remove item from inventory
if item in self.inventory:
self.inventory.remove(item)
else:
return False
Comment on lines +22 to +25

Choose a reason for hiding this comment

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

Consider refactoring to address the edge case first:

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


return item

#wave_2
def get_by_category(self, category):
"""
gets items by category
input: inventory from items
output: list of category

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

#wave_3
def swap_items(self, friend_vendor, my_item, their_item):
"""
check inventory items of each Vendor
input: adds and removes items from each Vendor inventory
output: Return True or False
"""
if my_item in self.inventory and their_item in friend_vendor.inventory:

Choose a reason for hiding this comment

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

Consider refactoring to check the edge case first (similar to the comment for remove

self.add(their_item)
self.remove(my_item)
friend_vendor.add(my_item)
friend_vendor.remove(their_item)
return True
else:
return False

#wave_4
def swap_first_item(self, friend):
"""
checks 1st item in self.inventory & friend's inventory
input: remove 1st items from self & friend
output: adds 1st items to self & friend. Returns True or False
"""
if self.inventory and friend.inventory:
own_item = self.inventory[0]
friend_item = friend.inventory[0]

self.remove(own_item)
friend.add(own_item)
friend.remove(friend_item)
self.add(friend_item)
Comment on lines +70 to +73

Choose a reason for hiding this comment

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

You can make use of swap_items here!


return True
else:
return False


#wave_6
def get_best_by_category(self, category):
"""
Gets item with the best condition in a certain category
input:
output:
"""
category_by_list = self.get_by_category(category)

if category_by_list == []:
return None
Comment on lines +89 to +90

Choose a reason for hiding this comment

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

consider initializing best_item = None which removes the need for this conditional test.


best_item = category_by_list[0]

for item in category_by_list:
if best_item.condition < item.condition:
best_item = item
return best_item


def swap_best_by_category(self, other, my_priority, their_priority):

Choose a reason for hiding this comment

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

This function is clear and concise! Great work.

"""
swaps the best item of certain categories with another vendor
input:
output:

"""
my_item = self.get_best_by_category(their_priority)
their_item = other.get_best_by_category(my_priority)
if my_item == None or their_item == None:
return False
return self.swap_items(other, my_item, their_item)

5 changes: 5 additions & 0 deletions tests/test_wave_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ def test_swap_items_returns_true():
item_a = Item(category="clothing")
item_b = Item(category="clothing")
item_c = Item(category="clothing")

fatimah = Vendor(
inventory=[item_a, item_b, item_c]
)

item_d = Item(category="electronics")
item_e = Item(category="decor")

jolie = Vendor(
inventory=[item_d, item_e]
)
Expand All @@ -40,12 +42,14 @@ def test_swap_items_when_my_item_is_missing_returns_false():
item_a = Item(category="clothing")
item_b = Item(category="clothing")
item_c = Item(category="clothing")

fatimah = Vendor(
inventory=[item_a, item_b, item_c]
)

item_d = Item(category="electronics")
item_e = Item(category="decor")

jolie = Vendor(
inventory=[item_d, item_e]
)
Expand Down Expand Up @@ -95,6 +99,7 @@ def test_swap_items_from_my_empty_returns_false():

item_d = Item(category="electronics")
item_e = Item(category="decor")

jolie = Vendor(
inventory=[item_d, item_e]
)
Expand Down
1 change: 0 additions & 1 deletion tests/test_wave_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def test_swap_best_by_category_no_other_match():
my_priority="Decor",
their_priority="Clothing"
)

assert result is False
assert len(tai.inventory) is 3
assert len(jesse.inventory) is 0
Expand Down