-
Notifications
You must be signed in to change notification settings - Fork 590
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
C18 Cheetahs - Sarah Sanborn #25
base: main
Are you sure you want to change the base?
Changes from all commits
36d5563
92ef56e
1700634
8a44ef6
638005f
fe96f48
9d92db0
82bb400
dd2a068
31e0e50
0d557b3
64888fc
6dcc516
18e7270
cce24a0
fd4ad66
f23421a
c8fc034
23795d9
aba2f2e
3975c2f
f055e10
2d644aa
1af9f0f
4b3538d
34ef29e
608628f
ca0636e
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,11 @@ | ||
class Clothing: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Clothing(Item): | ||
|
||
def __init__(self, category = "Clothing", condition = 0): | ||
super().__init__(category = None, condition = 0) | ||
self.category = category | ||
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,11 @@ | ||
class Decor: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Decor(Item): | ||
|
||
def __init__(self, category = "Decor", condition = 0): | ||
super().__init__(category = None, condition = 0) | ||
self.category = category | ||
self.condition = condition | ||
|
||
def __str__(self): | ||
return "Something to decorate your space." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
class Electronics: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Electronics(Item): | ||
|
||
def __init__(self, category = "Electronics", condition = 0): | ||
super().__init__(category = None, condition = 0) | ||
self.category = category | ||
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,25 @@ | ||
class Item: | ||
pass | ||
|
||
def __init__(self, category = None, condition = 0): | ||
if category is None: | ||
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. Strings are an immutable data type so it's safe to set them as a default value in your constructor :) |
||
self.category = category | ||
self.condition = condition | ||
|
||
def __str__(self): | ||
return "Hello World!" | ||
|
||
def condition_description(self): | ||
if self.condition >= 5: | ||
return "Perfect in every way!" | ||
elif self.condition >= 4: | ||
return "Not bad, not bad" | ||
elif self.condition >= 3: | ||
return "I've seen worse" | ||
elif self.condition >= 2: | ||
return "Well, at least it's free" | ||
elif self.condition >= 1: | ||
return "Better than a poke in the eye with a sharp stick" | ||
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. Lol love this condition 😆 Nice job with your |
||
elif self.condition > 0: | ||
return "Oof" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,66 @@ | ||
class Vendor: | ||
pass | ||
from operator import itemgetter | ||
from swap_meet.item import Item | ||
|
||
|
||
class Vendor(Item): | ||
def __init__(self, inventory = None): | ||
if inventory is None: | ||
inventory = [] | ||
super().__init__(category = None, condition = 0) | ||
self.inventory = inventory | ||
|
||
|
||
def add(self, item): | ||
self.inventory.append(item) | ||
return item | ||
|
||
def remove(self, item): | ||
if item not in self.inventory: | ||
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 approach works, but consider how a |
||
return False | ||
self.inventory.remove(item) | ||
return item | ||
|
||
def get_by_category(self, category): | ||
items_list = [item for item in self.inventory if item.category == 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. Nice list comprehension 😎 |
||
|
||
if items_list == []: | ||
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. Consider if this conditional is necessary. What would happen if this function only had the list comprehension and Also! this is a small note but it's more Pythonic to write |
||
return [] | ||
return items_list | ||
|
||
def swap_items(self, vendor, my_item, their_item): | ||
if my_item not in self.inventory or their_item not in vendor.inventory: | ||
return False | ||
|
||
# I hard-coded this, but was curious about how this could be scaled-up. | ||
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. Not sure what you mean by "hard coded". That usually refers to having hard coded data where it would be better to use a variable or something that would leave room for different values. That said, this function looks great! You could probably use a |
||
# What would happen if this swap-meet went viral? | ||
self.add(their_item) | ||
self.remove(my_item) | ||
vendor.add(my_item) | ||
vendor.remove(their_item) | ||
return True | ||
|
||
def swap_first_item(self, vendor): | ||
if len(self.inventory) == 0 or len(vendor.inventory) == 0: | ||
return False | ||
|
||
self.swap_items(vendor, self.inventory[0], vendor.inventory[0]) | ||
return True | ||
|
||
def get_best_by_category(self, category): | ||
if self.get_by_category(category) == []: | ||
return None | ||
best_item = max(self.get_by_category(category), key = lambda i : i.condition) | ||
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. Ooo! Great usage of the |
||
return best_item | ||
|
||
def swap_best_by_category(self, other, my_priority, their_priority): | ||
''' | ||
Input: other (for other vendor), my_priority (category vendor prefers),and their_priority (category other vendor prefers) | ||
Output: Returns True if swap is successful. Returns False if swap is unsuccessful | ||
''' | ||
vendor_best_item = self.get_best_by_category(their_priority) | ||
other_best_item = other.get_best_by_category(my_priority) | ||
|
||
if vendor_best_item is None or other_best_item is None: | ||
return False | ||
self.swap_items(other, vendor_best_item, other_best_item) | ||
return True |
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 constructor works but it's actually doing more work than is necessary.
Let's say a user calls your constructor and creates a new instance of the clothing class like this:
The function now has the following variables:
category = "shirt"
andcondition = 3
The next thing that happens is
This line calls
super()
which in this case is theItem
class. And then it calls theItem
class's__init__()
method. Doingsuper().__init__()
specifies that we want to take advantage of inheritance by using the code already written in the parent class here. So, under the hood, Python is going to grab that code, but run it here in the context of this class. So, it's going to passcategory = None
andcondition = 0
into the function which will updateshirt
's attributes to be:Next we have lines 7 & 8 which update
shirt
's attributes again, but this time with the arguments passed into theClothing
constructor and now stored in thecategory
andcondition
variables. This gives us:To prevent your code from updating the attributes twice, we should either call
super().__init__()
and pass the variables in there or declare the attributes and bind them locally like you did on lines 7 and 8. I would recommend usingsuper().__init__()
because it ensures that attributes ofClothing
andItem
are being handled the same way.Another thing to note is that I was able to update the
category
to be "shirt" even though it should have a category of "Clothing". To prevent changes to theClothing
class's category, you can removecategory
from theClothing
constructor's parameters and hardcode the value instead.So, you could refactor this constructor to be:
Now, users won't be able to change the category and the attributes will only be updated once. 😄