Skip to content
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
17 changes: 15 additions & 2 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
class Clothing:
pass
#import parent class
from swap_meet.item import Item

class Clothing(Item):

#apply parent class in constructor
def __init__(self, id=None, fabric="Unknown", condition=None):
super().__init__(id, condition)
self.fabric=fabric

#here I return the string from the parent contstructer inside of an f string with additional changes
#Aka Override
def __str__(self):
return (f"{super().__str__()}. It is made from {self.fabric} fabric.")

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

class Decor(Item):

#parent constructor with additional attributes
def __init__(self, id=None, width=0, length=0, condition=None):
super().__init__(id, condition)
self.width = width
self.length=length

#override that string from parent
def __str__(self):
return (f"{super().__str__()}. It takes up a {self.width} by "
f"{self.length} sized space.")
13 changes: 11 additions & 2 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
class Electronics:
pass

#import parent class
from swap_meet.item import Item
class Electronics(Item):

def __init__(self, id=None, type=None, condition=None):
super().__init__(id, condition)
self.type = type if type is not None else "Unknown"

def __str__(self):
return (f"{super().__str__()}. This is a {self.type} device.")
26 changes: 25 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
import uuid
class Item:
pass

#Instantiate with id, allowing custom or using UUID Int auto generate
def __init__(self, id=None, condition=None):
self.id = id if id is not None else uuid.uuid1().int

Choose a reason for hiding this comment

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

Nice use of ternary operator

self.condition = condition if condition is not None else 0
#get the class name
def get_category(self):
return str(type(self).__name__)

Choose a reason for hiding this comment

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

nice use of python introspection!


#here I am overriding the __str__ method.
def __str__(self):
return (f"An object of type {self.get_category()} with id {self.id}")

#adding a condition description class to describe condition based on value of condition

Choose a reason for hiding this comment

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

typo. "function", not "class"

def condition_description(self):

Choose a reason for hiding this comment

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

clever, but unnecessarily so here. A dict would have sufficed, and been O(1) lookup, vs O(n) to fall through n conditions in the worse case :)

case = lambda x: self.condition < x
if case(1): item_condition_desc = "Very Poor Indeed"

Choose a reason for hiding this comment

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

love the descriptions, though! :D

elif case(2): item_condition_desc = "This will likely be part of a DIY"
elif case(3): item_condition_desc = "Not To Shabby, not to great"
elif case(4): item_condition_desc = "Hey now, this is a tad swanky"
elif case(5): item_condition_desc = "You find yourself a right treasure you did!"
else : item_condition_desc = "This is so pristine its obscene!"
return item_condition_desc

91 changes: 90 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,91 @@
class Vendor:
pass
#constructor for vendor
def __init__(self, inventory = None):
self.inventory = inventory if inventory is not None else []

#function to add new item to inventory list.
def add(self, new_item):
self.inventory.append(new_item)
return new_item

#function to remove a given item from inventory
def remove(self, item_to_remove):
if item_to_remove not in self.inventory:
return None
self.inventory.remove(item_to_remove)

Choose a reason for hiding this comment

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

great use of inventory.remove instead of re-implementing that code.

return item_to_remove

#function to retreive item based on custom Id
def get_by_id(self, id):

for item in self.inventory:
if item.id==id:
return item
return None

#Making a method to swap items

def swap_items(self, vendor_friend, item_to_give, item_to_get):

#since the items are returned from their functions
#I can add and remove at the same time
if item_to_give not in self.inventory or item_to_get not in vendor_friend.inventory:
return False

vendor_friend.add(self.remove(item_to_give))

Choose a reason for hiding this comment

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

concise! for debugging and maintenance, this could be on different lines. That said, you did do error checking above, so that ameliorates some of the chances that you'll hit an error here.

self.add(vendor_friend.remove(item_to_get))

return True


def swap_first_item(self, vendor_friend):

if not self.inventory or not vendor_friend.inventory:
return False
return self.swap_items(vendor_friend, self.inventory[0], vendor_friend.inventory[0])

#get items from inventory by catagory
def get_by_category(self, category_string):

return [item for item in self.inventory if item.get_category() == category_string]

Choose a reason for hiding this comment

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

excellent use of list comprehension



# getting best by for catagory

def get_best_by_category(self, category_string):
if self.get_by_category(category_string):
return max(self.get_by_category(category_string), key=lambda item: item.condition)

Choose a reason for hiding this comment

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

Yay, using max plus lambda here!

return None

#swap best by catagory
def swap_best_by_category(self, other_vendor, my_priority, their_priority):
if not self.get_by_category(their_priority) or not other_vendor.get_by_category(my_priority):
return False
return self.swap_items(other_vendor, self.get_best_by_category(their_priority), other_vendor.get_best_by_category(my_priority))

#create a function to display inventory.
def display_inventory(self, category = None):
#create a list based on catagory or no
inventory = self.get_by_category(category) if category else self.inventory
if not inventory:
print ("No inventory to display.")

counter = 1
for item in inventory:
print (f"{counter}. " + str(item))
counter += 1

#swapping by id.
def swap_by_id(self, other_vendor, my_item_id, their_item_id):
if not self.get_by_id(my_item_id) or not other_vendor.get_by_id(their_item_id):
return False
return self.swap_items(other_vendor,self.get_by_id(my_item_id), other_vendor.get_by_id(their_item_id))

#choose and swap items!
def choose_and_swap_items(self, other_vendor, category = None):
self.display_inventory(category)
other_vendor.display_inventory(category)
my_item_id = input("Please enter the id of the item from the first vendor to swap")
their_item_id = input("Please input item of other vendor item you wish you swap")
return self.swap_by_id(other_vendor, int(my_item_id), int(their_item_id))

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.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_07.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(capfd):
camila = Vendor()
Expand Down
13 changes: 7 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

def test_vendor_has_inventory():
vendor = Vendor()
assert len(vendor.inventory) == 0

@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

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

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

def test_removing_not_found_returns_none():
item = "item to remove"
vendor = Vendor(
Expand All @@ -49,7 +49,8 @@ def test_removing_not_found_returns_none():

result = vendor.remove(item)

raise Exception("Complete this test according to comments below.")
assert result == None
assert len(vendor.inventory) == 3
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
11 changes: 5 additions & 6 deletions tests/unit_tests/test_wave_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@
from swap_meet.vendor import Vendor
from swap_meet.item import Item

@pytest.mark.skip

def test_items_have_default_uuid_length_id():
item = Item()
assert isinstance(item.id, int)
assert len(str(item.id)) >= 32

@pytest.mark.skip
def test_item_instances_have_different_default_ids():
item_a = Item()
item_b = Item()
assert item_a.id != item_b.id

@pytest.mark.skip

def test_items_use_custom_id_if_passed():
item = Item(id=12345)
assert isinstance(item.id, int)
assert item.id == 12345

@pytest.mark.skip

def test_item_obj_returns_text_item_for_category():
item = Item()
assert item.get_category() == "Item"

@pytest.mark.skip

def test_get_item_by_id():
test_id = 12345
item_custom_id = Item(id=test_id)
Expand All @@ -36,7 +35,7 @@ def test_get_item_by_id():
result_item = vendor.get_by_id(test_id)
assert result_item is item_custom_id

@pytest.mark.skip

def test_get_item_by_id_no_matching():
test_id = 12345
item_a = Item()
Expand Down
19 changes: 9 additions & 10 deletions tests/unit_tests/test_wave_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

def test_item_overrides_to_string():
test_id = 12345
item = Item(id=test_id)
Expand All @@ -12,7 +12,7 @@ def test_item_overrides_to_string():
expected_result = f"An object of type Item with id {test_id}"
assert item_as_string == expected_result

@pytest.mark.skip

def test_swap_items_returns_true():
item_a = Item()
item_b = Item()
Expand Down Expand Up @@ -40,7 +40,7 @@ def test_swap_items_returns_true():
assert item_b in jolie.inventory
assert result

@pytest.mark.skip

def test_swap_items_when_my_item_is_missing_returns_false():
item_a = Item()
item_b = Item()
Expand All @@ -67,7 +67,7 @@ def test_swap_items_when_my_item_is_missing_returns_false():
assert item_e in jolie.inventory
assert not result

@pytest.mark.skip

def test_swap_items_when_their_item_is_missing_returns_false():
item_a = Item()
item_b = Item()
Expand All @@ -94,7 +94,7 @@ def test_swap_items_when_their_item_is_missing_returns_false():
assert item_e in jolie.inventory
assert not result

@pytest.mark.skip

def test_swap_items_from_my_empty_returns_false():
fatimah = Vendor(
inventory=[]
Expand All @@ -114,7 +114,7 @@ def test_swap_items_from_my_empty_returns_false():
assert len(jolie.inventory) == 2
assert not result

@pytest.mark.skip

def test_swap_items_from_their_empty_returns_false():
item_a = Item()
item_b = Item()
Expand All @@ -131,7 +131,6 @@ def test_swap_items_from_their_empty_returns_false():

result = fatimah.swap_items(jolie, item_b, nobodys_item)

raise Exception("Complete this test according to comments below.")
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
assert len(jolie.inventory) == 0
assert len(fatimah.inventory) == 3
assert not result
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

def test_swap_first_item_returns_true():
item_a = Item()
item_b = Item()
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

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

def test_swap_first_item_from_their_empty_returns_false():
item_a = Item()
item_b = Item()
Expand Down
Loading