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

Scissors - Stella #63

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
Empty file added practice.py
Empty file.
10 changes: 10 additions & 0 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from swap_meet.item import Item

#Wave 5
class Clothing(Item):
def __init__(self,condition = 0, age =0):
super().__init__("Clothing", condition,age)


def __str__(self):
return "The finest clothing you could wear."
9 changes: 9 additions & 0 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from swap_meet.item import Item

#Wave 5:
class Decor(Item):
def __init__(self,condition = 0, age =0):

Choose a reason for hiding this comment

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

Good use of super/inheritance.

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

def __str__(self):
return "Something to decorate your space."
9 changes: 9 additions & 0 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from swap_meet.item import Item

#Wave 5:
class Electronics(Item):
def __init__(self,condition=0, age = 0):
super().__init__("Electronics",condition, age)

def __str__(self):
return "A gadget full of buttons and secrets."
22 changes: 22 additions & 0 deletions swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#Wave 2:
class Item:

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

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

#Wave 5:
def condition_description(self):
condition_note = ""
if self.condition >=0 and self.condition <= 2:
condition_note = "oh no! please throw"
elif self.condition >2 and self.condition <= 3.5:
condition_note = "used for a bit but can keep going..."
elif self.condition >3.5 and self.condition <= 5:
condition_note = "fresh and new!"
return condition_note
96 changes: 96 additions & 0 deletions swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from swap_meet.item import Item

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

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 +16 to +20

Choose a reason for hiding this comment

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

Consider using the pattern where you check the edge case first:

Suggested change
if item in self.inventory:
self.inventory.remove(item)
return 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):
category_list = []

for item in self.inventory:
if item.category == category:
category_list.append(item)
return category_list

#Wave 3:
def swap_items(self,friend, my_item, their_item):
#if len(self.inventory) == 0 or len(friend.inventory) ==0:
#return False
if my_item in self.inventory and their_item in friend.inventory:

Choose a reason for hiding this comment

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

Consider refactoring so you check the condition where you return False first, and then move the main logic out of the conditional.

self.inventory.remove(my_item)
friend.inventory.append(my_item)
friend.inventory.remove(their_item)
self.inventory.append(their_item)
return True
else:
return False

#Wave 4
def swap_first_item(self,friend):
if len(self.inventory) == 0 or len(friend.inventory) ==0:
return False
else:
return 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.

You can move this line our of the else to enhance readability.



#Wave 6
def get_best_by_category(self,category):
best_item = None
best_condition = 0
category_items = self.get_by_category(category)
if len(category_items) != 0:
for item in category_items:
if item.condition > best_condition:
best_item = item
best_condition = item.condition
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.

Wow! So efficient. Good use of the previous methods as helper functions.

their_offer = other.get_best_by_category(my_priority)
my_offer = self.get_best_by_category(their_priority)
swap_result = self.swap_items(other,my_offer,their_offer)
return swap_result


#Wave 7 - Optional Enhancement
#Helper function - get item with the min age

Choose a reason for hiding this comment

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

Great work implementing this optional part.

def get_min_age(self):
newest_item = None
min_age = 100

for item in self.inventory:
if item.age < min_age:
newest_item = item
min_age = item.age
return newest_item

def swap_by_newest(self,other):
my_newest = self.get_min_age()
their_newest = other.get_min_age()
return self.swap_items(other,my_newest,their_newest)



'''
#Swap the newest items
def swap_by_newest(self,other,my_item,their_item):
my_newest = self.get_min_age(their_item)
their_newest = other.get_min_age(my_item)
return self.swap_items(other,my_newest,their_newest)
'''
30 changes: 30 additions & 0 deletions tests/test_wave_07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
from swap_meet.vendor import Vendor
from swap_meet.item import Item
from swap_meet.clothing import Clothing
from swap_meet.decor import Decor
from swap_meet.electronics import Electronics


def test_get_min_age():
item_a = Clothing(age = 1)
item_b = Clothing(age = 4)
item_c = Decor(age = 2)
item_d = Decor(age = 3.5)
tai = Vendor(inventory = [item_a,item_b,item_c, item_d])
newest_item = tai.get_min_age()

assert newest_item.age == 1

def test_swap_by_newest():
item_a = Item(age=2)
item_b = Item(age=4)
tai = Vendor(inventory=[item_a, item_b])

item_d = Item(age=2)
item_e = Item(age=4)
jesse = Vendor(inventory=[item_d, item_e])
result = tai.swap_by_newest(jesse)

assert result is True

Choose a reason for hiding this comment

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

Good work writing your own tests! It's good practice to add a few more assertions here to make sure that the swap actually happened. Check that tai now has item_d and jesse has item_a (did I get that right?). I would also add an edge case test. What happens when the vendors have no inventory? What happens when all the items in their inventory have the same age?


2 changes: 2 additions & 0 deletions travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
language: python
script: python pytest.py