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-Sharks-Raha #15

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
39 changes: 39 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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

# vendor = Vendor(inventory=["a", "b", "c"])
# print(vendor.inventory)
# item_a = Item(category = "clothing", condition = 1, age = 2)
# item_b = Item(category = "Decor", condition = 3, age = 1)
# item_c = Item(category = "Electerical", condition = 2, age = 3)
# item_d = Item(category = "clothing", condition = 4, age = 5)
# item_e = Item(category = "Decor", condition = 2, age = 4)
# item_f = Item(category = "Electronics", condition = 3, age = 3)
# raha = Vendor([item_a,item_b,item_c])
# # print (raha.inventory)
# mitra = Vendor([item_d,item_e,item_f])
# print(vendor.add("d"))
# print(vendor.remove("d"))
# print(vendor.inventory)
item_a = Decor(age = 2.0)
item_b = Electronics(age=4.0)
item_c = Decor(age=4.0)
tai = Vendor(
inventory=[item_b, item_a, item_c]
)

# them
item_d = Clothing(age=2.0)
item_e = Decor(age=4.0)
item_f = Clothing(age=4.0)
jesse = Vendor(
inventory=[item_e, item_d,item_f]
)
print(tai.inventory)
# print(jesse.inventory)

print(tai.swap_first_item(jesse))
print(tai.swap_by_newest(jesse))
31 changes: 29 additions & 2 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
class Clothing:
pass
from swap_meet.item import Item
class Clothing(Item):

"""
A class to represent a cloth.

...

Attributes
----------
category : string (optional)
a string that represents the category of the cloth
condition:
a float number between 0 and 5 that represents the current condition of the cloth
age :
an integer that represents the age of the cloth
Methods
-------
__str__:
- returns "The finest clothing you could wear."
"""
def __init__(self, condition = 0, age = 0):
super().__init__(category = "Clothing", condition = condition, age = age)

def __str__(self):
return "The finest clothing you could wear."
Comment on lines +22 to +26

Choose a reason for hiding this comment

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

👍 Good work in ensuring that all Clothing instances have a category of "Clothing" by passing the category value into the parent constructor.



# pass
26 changes: 24 additions & 2 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
class Decor:
pass
from swap_meet.item import Item
class Decor(Item):
"""
A class to represent a decor.
...

Attributes
----------
category : string (optional)
a string that represents the category of the decor
condition:
a float number between 0 and 5 that represents the current condition of the decor
age :
an integer that represents the age of the decor
Methods
-------
__str__:
- returns "Something to decorate your space."
"""
def __init__(self, condition = 0, age = 0):
super().__init__(category = "Decor", condition = condition, age = age)

def __str__(self):
return "Something to decorate your space."
Comment on lines +20 to +24

Choose a reason for hiding this comment

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

👍

24 changes: 24 additions & 0 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
class Electronics:
pass

from swap_meet.item import Item
class Electronics(Item):
"""
A class to represent an electronic item.
...
Attributes
----------
category : string (optional)
a string that represents the category of the electronic item
condition:
a float number between 0 and 5 that represents the current condition of the electronic item
age :
an integer that represents the age of the electronic item
Methods
-------
__str__:
- returns "A gadget full of buttons and secrets."
"""
def __init__(self, condition = 0, age = 0):
super().__init__(category = "Electronics", condition = condition, age = age)

def __str__(self):
return "A gadget full of buttons and secrets."
Comment on lines +22 to +26

Choose a reason for hiding this comment

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

👍

51 changes: 50 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
class Item:
pass
"""
A class to represent an item.

...

Attributes
----------
category : string (optional)
a string that represents the category of the item
condition:
a float number between 0 and 5 that represents the current condition of the item

age :
an integer that represents the age of the item

Methods
-------
condition_description:
- returns "Just trash it!" if condition is between 0 and 1
- returns "Enjoy the last moments!" if condition is between 1 and 2
- returns "As good as 30 year old wine!" if condition is between 2 and 3
- returns "Better than the new one" if condition is between 3 and 4
- returns "No need to ask!" if condition is between 4 and 5
- returns False if condition is not between 0 and 5
"""
def __init__(self,category = "", condition = 0.0, age = 0):
self.category = category
self.condition = condition
self.age = age
def __str__(self):

return f"Hello World!"

def condition_description(self):
if 0 <= self.condition < 1:
return "Just trash it!"

Choose a reason for hiding this comment

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

Love this description 😆🗑️

elif 1 <= self.condition < 2:
return "Enjoy the last moments!"
elif 2 <= self.condition < 3:
return "As good as 30 year old wine!"
elif 3 <= self.condition < 4:
return "Better than the new one"
elif 4 <= self.condition <= 5:
return "No need to ask!"
else:
return False
Comment on lines +27 to +47

Choose a reason for hiding this comment

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

👍 Great work! One minor suggestion:

  • Use 1 line of whitespace to separate function definitions from other logic. So in this case, you should have a space above def __str__(self):.




# pass
159 changes: 158 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,159 @@
import operator

class Vendor:
pass

"""
A class to represent a vendor.

...

Attributes
----------
inventory : list (optional)
a list of available items for each vendor


Methods
-------
add(item):
- adds a new item to the vendor's inventory
- Returns the updated inventory list

remove(item):
- removes an item from the vendor's inventory list
- Returns the updated inventory list

get_best_by_category(category):
- takes a string representing a category
- Returns a list of items in the inventory with the same category

swap_items(other_vendor,my_item,their_item):
- takes an instance of another vendor, two instances of Item item (my_item)
and item(their_item)
- removes my_item from vendor's inventory and adds it to the other_vendor inventory
- removes their_item from other_vendor inventory and adds it to the vendor inventory
- return True
- returns False if my_item not in vendor's inventory or their item not in
other_vendor's inventory

swap_first_item(friend_vendor):
- takes in an instance of another vendor friend_vendor
- removes the first item in vendor's inventory and add that to the friend_vendor inventory
- removes the first item in friend_vendor inventory and add that to the vendor inventory
- returns True
- returns False if vendor inventory or friend_vendor inventory is empty

get_best_by_category(category):
- takes in a string representing the category
- looks through vendor's inventory for the item with the highest condition
and matching category
- returns one item that matches the aforementioned criteria

swap_best_by_category(other,my_priority,their_priority):
- takes in an instance of another vendor (other), a string for a category that the vendor wants
to receive (my_priority) and a string for a category that the other vendors wants to receive (other)
- passes my_priority and their_priority to the get_best_by_category function to find items with highest
condition in each category
- passes items to the swap_items to swap those items
- returns True
- returns False if my_priority not in vendor's inventory or their priority not in the
other's inventory

swap_by_newest(other):
- takes in another instance of vendor (other)
- sorts the vendor inventory and other inventory based on the age of items
- returns swap_first_item for the vendor and other vendor with sorted inventories

"""


def __init__(self, inventory = None):
if not inventory:
inventory = []
self.inventory = inventory
Comment on lines +71 to +73

Choose a reason for hiding this comment

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

Good work! We can also use a ternary to assign the value of self.inventory:

self.inventory = inventory if inventory else []


def add(self,item):

Choose a reason for hiding this comment

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

👍

Choose a reason for hiding this comment

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

Following pep8 guidelines, we should add a space after every comma when listing parameters/arguments:

    def add(self, item):

self.inventory.append(item)
return item

def remove(self,item):
try:
self.inventory.remove(item)
return item
except ValueError as ve:
return False
Comment on lines +79 to +84

Choose a reason for hiding this comment

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

👍 Nice use of a try/except!


def get_by_category(self,category):
result = [item for item in self.inventory if item.category == category]
return result
Comment on lines +86 to +88

Choose a reason for hiding this comment

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

👍 Great use of a ternary!


def swap_items(self,other_vendor,my_item,their_item):
if not all ((other_vendor, my_item, their_item)):
return False
if my_item == their_item:
return True
elif my_item in self.inventory and their_item in other_vendor.inventory:
self.add(their_item)
other_vendor.add(my_item)
other_vendor.remove(their_item)
self.remove(my_item)
return True
Comment on lines +90 to +100

Choose a reason for hiding this comment

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

👍

Choose a reason for hiding this comment

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

Another approach:

    def swap_items(self,other_vendor,my_item,their_item):
        if not all ((other_vendor, my_item, their_item)):
            return False
            
        if  my_item == their_item:
            return True
            
        if my_item not in self.inventory or their_item not in other_vendor.inventory:
            return False
            
        self.add(their_item)
        other_vendor.add(my_item)
        other_vendor.remove(their_item)
        self.remove(my_item)
        return True

This was a minor change but notice how the primary logic of swapping items is now unintended and more in focus. This is due to the handy guard clause in the previous line.


def swap_first_item(self, friend_vendor):
if not all((friend_vendor.inventory, self.inventory)):
return False

friend_vendor.add(self.inventory[0])
self.add(friend_vendor.inventory[0])
friend_vendor.remove(friend_vendor.inventory[0])
self.remove(self.inventory[0])
return True
Comment on lines +102 to +110

Choose a reason for hiding this comment

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

Nice work! Another option is to use the swap_items() as a helper function.

Choose a reason for hiding this comment

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

Currently, swapping any items has a time complexity of O(n) due to the remove method. We can make a subtle improvement to O(1) by using multiple-assignment instead:

        self.inventory[0], vendor.inventory[0] = vendor.inventory[0], self.inventory[0]

Here's more info on Python multiple-assignment: https://www.w3schools.com/python/gloss_python_assign_value_to_multiple_variables.asp


def get_best_by_category(self,category):
matched_category = [item for item in self.inventory if category == item.category]
if len(matched_category) == 0:
return None
max = matched_category[0]
for item in matched_category:
if item.condition > max.condition:
max = item
return max
Comment on lines +113 to +120

Choose a reason for hiding this comment

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

Nice work! We could also use the get_by_category()as a helper function too.


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

if my_choice and their_choice:
self.swap_items(other, their_choice, my_choice)
return True
return False
Comment on lines +122 to +129

Choose a reason for hiding this comment

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

👍




def swap_by_newest(self,other):
if not all ((self.inventory , other.inventory)):
return None
self.inventory = sorted(self.inventory, key=lambda item: item.age, reverse=False)
other.inventory = sorted(other.inventory, key=lambda item: item.age, reverse=False)
return self.swap_first_item(other)
Comment on lines +133 to +138

Choose a reason for hiding this comment

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

Nice use of sorted() and a lambda function!






# def newest_item(self):
# if self.inventory == []:
# return None
# new_item = self.inventory[0]
# for item in self.inventory:
# if item.age < new_item.age:
# new_item = item
# return new_item

# sorted_inventory = self.inventory.sort(key = lambda item: item.age)
# return sorted_inventory
# new_self = self.newest_item()
# new_other = other.newest_item()
# return new_self.inventory.swap_first_item(new_other.inventory)
# return self.swap_items(other, new_self,new_other)
# pass
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
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
Loading