-
Notifications
You must be signed in to change notification settings - Fork 94
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
Maple- Sabrina L. #94
base: master
Are you sure you want to change the base?
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 |
---|---|---|
@@ -1,2 +1,10 @@ | ||
class Clothing: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Clothing(Item): | ||
def __init__(self, condition = 0): | ||
# super().__init__(category = "Clothing") | ||
## super().__init__(condition) | ||
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 |
---|---|---|
@@ -1,2 +1,10 @@ | ||
class Decor: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Decor(Item): | ||
|
||
def __init__(self, condition = 0): | ||
self.category = "Decor" | ||
self.condition = condition | ||
Comment on lines
+4
to
+7
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. same as above! we could refactor this a little more to use inheritance more explicitly |
||
|
||
def __str__(self): | ||
return "Something to decorate your space." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
class Electronics: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Electronics(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. 👍 |
||
def __init__(self, condition = 0): | ||
self.category = "Electronics" | ||
self.condition = condition | ||
|
||
def __str__(self): | ||
return "A gadget full of buttons and secrets." | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
class 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. 👍 |
||
pass | ||
def __init__(self, category = ""): | ||
self.category = category | ||
def __str__(self): | ||
return "Hello World!" | ||
def condition_description(self): | ||
if self.condition < 1: | ||
return "poor" | ||
elif self.condition < 2: | ||
return "bad" | ||
elif self.condition < 3: | ||
return "okay" | ||
elif self.condition < 4: | ||
return "decent" | ||
elif self.condition < 5: | ||
return "nearly new" | ||
else: | ||
return "this is so bad it does not have a condition description" |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,2 +1,57 @@ | ||||||||||||||||
from swap_meet.item import 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. so here, we don't actually need to import the literal class Item itself. We aren't instantiating an instance of Item (ie, Item(category="antique",3) ) inside the Vendor anywhere. There are "items" coming in through parameters, but the class "blueprint" itself can't tell that. On line 7 So unless we are actually creating an instance inside vendor (like our composition examples), we don't need to import it |
||||||||||||||||
|
||||||||||||||||
class Vendor: | ||||||||||||||||
pass | ||||||||||||||||
def __init__(self, inventory = []): | ||||||||||||||||
self.inventory = inventory | ||||||||||||||||
Comment on lines
+4
to
+5
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. careful passing in a data structure like a list as a default parameter in a class constructor. Every instance that is made will all use the same list instead of each instance having its own unique inventory list. this is known as a "side effect." so what we can do to stop this side effect is:
Suggested change
now for every instance of Vendor that is created without a pre-made inventory list, the instance will get its own unique empty list. |
||||||||||||||||
|
||||||||||||||||
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. 👍 |
||||||||||||||||
inventory_list = [] | ||||||||||||||||
for item in self.inventory: | ||||||||||||||||
if item.category == category: | ||||||||||||||||
inventory_list.append(item) | ||||||||||||||||
return inventory_list | ||||||||||||||||
|
||||||||||||||||
def swap_items(self, friend, my_item, their_item): | ||||||||||||||||
if their_item in friend.inventory and my_item in self.inventory: | ||||||||||||||||
self.add(their_item) | ||||||||||||||||
friend.add(my_item) | ||||||||||||||||
self.remove(my_item) | ||||||||||||||||
friend.remove(their_item) | ||||||||||||||||
Comment on lines
+27
to
+30
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. great job reusing methods you've already made!we can make this even shorter, though:
Suggested change
if you recall, the |
||||||||||||||||
return True | ||||||||||||||||
else: | ||||||||||||||||
return False | ||||||||||||||||
|
||||||||||||||||
def swap_first_item(self, friend): | ||||||||||||||||
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 self.inventory and friend.inventory: | ||||||||||||||||
self.swap_items(friend, self.inventory[0], friend.inventory[0]) | ||||||||||||||||
return True | ||||||||||||||||
return False | ||||||||||||||||
|
||||||||||||||||
def get_best_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. 👍 |
||||||||||||||||
items = self.get_by_category(category) | ||||||||||||||||
if not items: | ||||||||||||||||
return None | ||||||||||||||||
best = items[0] | ||||||||||||||||
for item in items: | ||||||||||||||||
if item.condition > best.condition: | ||||||||||||||||
best = item | ||||||||||||||||
return best | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
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. 👍 |
||||||||||||||||
my_best = self.get_best_by_category(their_priority) | ||||||||||||||||
their_best = other.get_best_by_category(my_priority) | ||||||||||||||||
if not my_best and their_best: | ||||||||||||||||
return False | ||||||||||||||||
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.
great! lines 7 and 8 totally work, though we could definitely use the parent class' constructor instead! It already creates
category
andcondition
attributes for us, so let's look at that:and I like that you hardcoded "Clothing" since of course this child class Clothing will always be category "Clothing." No reason to give the user the opportunity to mess that up haha