-
Notifications
You must be signed in to change notification settings - Fork 71
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
base: master
Are you sure you want to change the base?
swap_meet #74
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
def __str__(self): | ||
return "The finest clothing you could wear." | ||
|
||
|
||
|
||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 think about what I talked about in |
||
|
||
|
||
|
||
|
||
|
||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 think about what I talked about in |
||
|
||
|
||
|
||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,109 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
class Vendor: | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
def __init__(self, inventory=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||||||||||||||||||||||||||||||
self.inventory.append(item) | ||||||||||||||||||||||||||||||||||||||||||||||||||
return item | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
def remove(self, item): | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
So, we call |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 swap_best_by_category(self, other, my_priority, their_priority): | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
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.here's another way we could do it based on Learn's
super()
lesson:Both options work, but I would say the top suggestion is better practice, and you will see it in industry