-
Notifications
You must be signed in to change notification settings - Fork 113
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
Sharks - Sana Pournaghshband #103
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,8 @@ | ||
class Clothing: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Clothing(Item): | ||
def __init__(self, condition = 0): | ||
super().__init__("Clothing", condition) | ||
|
||
def __str__(self): | ||
return "The finest clothing you could wear." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
class Decor: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Decor(Item): | ||
def __init__(self, condition = 0): | ||
super().__init__("Decor", condition) | ||
|
||
def __str__(self): | ||
return "Something to decorate your space." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
class Electronics: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Electronics(Item): | ||
def __init__(self, condition = 0): | ||
super().__init__("Electronics", 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,15 @@ | ||
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. Item class looks good! |
||
pass | ||
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 f"condition is {self.condition} and it's very bad" | ||
elif self.condition >= 1 and self.condition<= 3: | ||
return f"condition is {self.condition} and it's moderate" | ||
elif self.condition > 3 and self.condition<=5: | ||
return f"condition is {self.condition} and it's very good" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,64 @@ | ||
class Vendor: | ||
pass | ||
def __init__(self, inventory = None) : | ||
if not inventory: | ||
self.inventory = [] | ||
else: | ||
self.inventory = inventory | ||
Comment on lines
+3
to
+6
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. Since you have a guard clause on line 3, you don't need the dangling if not inventory:
self.inventory = []
self.inventory = inventory You might also see this written as a ternary:
In this situation, that would look like self.inventory = [] if inventory is None else inventory |
||
|
||
def add(self, item): | ||
self.inventory.append(item) | ||
return item | ||
|
||
def remove(self,item): | ||
if item in self.inventory: | ||
self.inventory.remove(item) | ||
return item | ||
return False | ||
Comment on lines
+13
to
+16
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. A more explicit way to if item not in self.inventory:
return False
self.inventory.remove(item)
return item Another approach we could take is to try to remove the item directly, and handle the ValueError that occurs if it's not there, and return False to handle it (try/except) |
||
|
||
def get_by_category(self, category): | ||
item_list = [] | ||
for item in self.inventory: | ||
if category == item.category: | ||
item_list.append(item) | ||
|
||
return item_list | ||
Comment on lines
+19
to
+24
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 good. This method is a great candidate for using list comprehension if you want to refactor it since our conditional statement we check is pretty simple. General syntax for list comp: result_list = [element for element in source_list if some_condition(element)] Which here would look like: item_list = [item for item in self.inventory if item.category == category] You can also forgo saving the list to the variable item_list and do something like: def get_by_category(self, category):
return [item for item in self.inventory if item.category == category] |
||
|
||
def swap_items(self, vendor: 'Vendor', my_item, their_item): | ||
if my_item not in self.inventory: | ||
return False | ||
if their_item not in vendor.inventory: | ||
return False | ||
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. Nice guard clause. You can combine lines 27 and 29 like:
|
||
self.remove(my_item) | ||
vendor.add(my_item) | ||
vendor.remove(their_item) | ||
self.add(their_item) | ||
return True | ||
|
||
def swap_first_item(self, vendor: '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. 👍 |
||
if not self.inventory or not vendor.inventory: | ||
return False | ||
|
||
my_item = self.inventory[0] | ||
their_item = vendor.inventory[0] | ||
|
||
return self.swap_items(vendor, my_item, their_item) | ||
|
||
def get_best_by_category(self, category = ''): | ||
if not self.inventory: | ||
return None | ||
|
||
category_items = [] | ||
for item in self.inventory: | ||
if item.category == category: | ||
category_items.append(item) | ||
Comment on lines
+51
to
+53
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 reusing your get_by_category method that you already defined so you don't have to loop and add to a list again. |
||
if not category_items: | ||
return None | ||
|
||
best_item = max(category_items, key=lambda item: item.condition) | ||
return best_item | ||
|
||
def swap_best_by_category(self, other: 'Vendor', my_priority, their_priority): | ||
my_item = self.get_best_by_category(their_priority) | ||
their_item = other.get_best_by_category(my_priority) | ||
|
||
return self.swap_items(other, my_item, their_item) | ||
Comment on lines
+61
to
+64
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 work reusing your function Also, way to leverage the validation checks in I also like that you return the Boolean value from |
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.
👍