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 #74

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

class Clothing(Item):

def __init__(self, category = "", condition=0):
self.category = "Clothing"
self.condition = condition
Comment on lines +3 to +7

Choose a reason for hiding this comment

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

this works! But since you are using inheritance by importing Item and passing it in, we can take advantage of the parent class's __init__ method that's already been constructed.

Suggested change
class Clothing(Item):
def __init__(self, category = "", condition=0):
self.category = "Clothing"
self.condition = condition
class Clothing(Item):
def __init__(self, condition=0): # because we know Clothing class will always be category "clothing", we don't need to give the user the ability to pass in a value
super().__init__("clothing", condition) # here we are calling the parent class's __init__ method and we pass in the argument from the Child class down to the parent with "clothing". Now we don't need to assign self.category or self.condition again

here's another way we could do it based on Learn's super() lesson:

Suggested change
class Clothing(Item):
def __init__(self, category = "", condition=0):
self.category = "Clothing"
self.condition = condition
class Clothing(Item):
# if we don't change the parameters and keep them exactly like the parent class's __init__ method, then we don't technically have to create a child __init__ method because it will inherit the parent's. So only the __str__ method will be in this class

Both options work, but I would say the top suggestion is better practice, and you will see it in industry


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




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

class Decor(Item):

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

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

Choose a reason for hiding this comment

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

👍 think about what I talked about in clothing.py and apply it here as well!







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

class Electronics(Item):

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

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

Choose a reason for hiding this comment

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

👍 think about what I talked about in clothing.py and apply it here as well!





19 changes: 19 additions & 0 deletions swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Item:
def __init__(self, item = "Hello World!", category="", condition = 0):
self.category = category
self.item = item

def __str__(self):
return f"{self.item}"

def __float__(self):
return f"{self.condition}"

def condition_description(self):
return f"{self.condition}"
Comment on lines +1 to +13

Choose a reason for hiding this comment

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

looks like self.condition is being used in condition_description method, but it is not assigned in the __init__ method. Let's try something like this:

Suggested change
class Item:
def __init__(self, item = "Hello World!", category="", condition = 0):
self.category = category
self.item = item
def __str__(self):
return f"{self.item}"
def __float__(self):
return f"{self.condition}"
def condition_description(self):
return f"{self.condition}"
class Item:
def __init__(self, category="", condition = 0):
self.category = category
self.condition = condition
def __str__(self):
return "Hello world!"
def condition_description(self):
if self.condition == 0:
return "This item is trash!"
# keep adding all conditions ( 0 thru 5) to this method to give a description to the item, not just the condition number







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

class Vendor:

Choose a reason for hiding this comment

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

👍


def __init__(self, inventory=None):

Choose a reason for hiding this comment

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

👍

if inventory == None:
self.inventory = []
else:
self.inventory = inventory


def add(self, item):

Choose a reason for hiding this comment

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

👍

self.inventory.append(item)
return item


def remove(self, item):

Choose a reason for hiding this comment

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

👍

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

def get_by_category(self, category):

Choose a reason for hiding this comment

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

👍

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

def swap_items(self, other_vendor, my_item, their_item):

if their_item not in other_vendor.inventory or my_item not in self.inventory:

return False

else:

other_vendor.inventory.append(my_item)
other_vendor.remove(their_item)
self.inventory.remove(my_item)
self.inventory.append(their_item)

return True

def swap_first_item(self, other_vendor):


if self.inventory == [] or other_vendor.inventory == []:

return False

else:

other_vendor.inventory.append(self.inventory[0])
self.inventory.remove(self.inventory[0])
self.inventory.append(other_vendor.inventory[0])
other_vendor.inventory.remove(other_vendor.inventory[0])

return True
Comment on lines +45 to +59

Choose a reason for hiding this comment

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

👍 this works perfectly! but let's look at lines 32 - 43 above. We are almost doing the same thing twice, so how could we fix that? We could use the swap_items method inside of this one.

Suggested change
def swap_first_item(self, other_vendor):
if self.inventory == [] or other_vendor.inventory == []:
return False
else:
other_vendor.inventory.append(self.inventory[0])
self.inventory.remove(self.inventory[0])
self.inventory.append(other_vendor.inventory[0])
other_vendor.inventory.remove(other_vendor.inventory[0])
return True
def swap_first_item(self, other_vendor):
return self.swap_items(other_vendor, self.inventory[0], other_vendor.inventory[0])

So, we call swap_items and pass in the arguments it needs: the other vendor, our item, other vendor's item. So we grab the first item out of the inventories by using index 0. Now we let the method return True or False


def get_best_by_category(self, category=""):
items_in_category = []

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

best_item = None
best_condition = 0
for item in items_in_category:
if item.condition > best_condition:
best_condition = item.condition
best_item = item
return best_item
Comment on lines +61 to +74

Choose a reason for hiding this comment

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

👍 heck yeah this is great! But don't lines 62 - 66 look familiar? I bet we've got a method already built for this!

Suggested change
def get_best_by_category(self, category=""):
items_in_category = []
for item in self.inventory:
if item.category == category:
items_in_category.append(item)
best_item = None
best_condition = 0
for item in items_in_category:
if item.condition > best_condition:
best_condition = item.condition
best_item = item
return best_item
def get_best_by_category(self, category=""):
items_in_category = get_by_category(category)
best_item = None
best_condition = 0
for item in items_in_category:
if item.condition > best_condition:
best_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.

👍 beautifully done!!!

my_best = self.get_best_by_category(their_priority)
their_best = other.get_best_by_category(my_priority)
return self.swap_items(other, my_best, their_best)