-
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
Sea Turtles - Heather M. #14
base: main
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from swap_meet.vendor import Vendor | ||
from swap_meet.item import Item | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
class Clothing: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Clothing(Item): | ||
def __init__(self, condition = 0): | ||
super().__init__(category = "Clothing", condition = condition) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
class Decor: | ||
pass | ||
from swap_meet.item import Item | ||
|
||
class Decor(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): | ||
super().__init__(category = "Decor", condition = condition) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
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): | ||
super().__init__(category = "Electronics", condition = condition) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,29 @@ | ||
## I attempted to define these variables below in Item instead of globally, | ||
## but it made the code less readable, and required calling them with Items.* or self.* | ||
|
||
CATEGORIES = { | ||
"": "Hello World!", | ||
"Clothing": "The finest clothing you could wear.", | ||
"Decor": "Something to decorate your space.", | ||
"Electronics": "A gadget full of buttons and secrets.", | ||
} | ||
|
||
CONDITION_DESCRIPTORS = { | ||
0: "Oof. Are you sure you want this?", | ||
1: "Wow. It's pretty bad.", | ||
2: "Significant damage.", | ||
3: "Needs some love.", | ||
4: "Minor signs of wear or damage.", | ||
5: "So good it might be a scam.", | ||
} | ||
Comment on lines
+4
to
+18
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. Heather great job making these global "constant" variables. This helps eliminate redundant code blocks and makes this accessible to other subclasses. |
||
|
||
class Item: | ||
pass | ||
def __init__(self, category = "", condition = 0): | ||
self.category = category | ||
self.condition = condition | ||
|
||
def __str__(self): | ||
return CATEGORIES[self.category] | ||
|
||
def condition_description(self): | ||
return CONDITION_DESCRIPTORS[int(self.condition)] | ||
Comment on lines
+21
to
+29
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. 👍🏽 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,95 @@ | ||
class Vendor: | ||
pass | ||
def __init__ (self, inventory = None): | ||
if not inventory: | ||
inventory = [] | ||
|
||
self.inventory = inventory | ||
|
||
def add(self, item): | ||
self.inventory.append(item) | ||
return item | ||
|
||
def remove(self, item): | ||
try: | ||
self.inventory.remove(item) | ||
return item | ||
|
||
except: | ||
return False | ||
|
||
def get_by_category(self, category): | ||
items = [] | ||
|
||
for item in self.inventory: | ||
if item.category == category: | ||
items.append(item) | ||
|
||
return items | ||
|
||
def swap_items(self, vendor, my_item, their_item): | ||
''' | ||
add my item to vendor's inventory | ||
remove my item from my inventory | ||
add their item to my inventory | ||
remove their item from their inventory | ||
--- | ||
I initially used the above add() and remove() methods, but wanted something a bit more logically streamlined. | ||
Accomplishing the swap of items using a tuple allows for a direct in-place substitution without iterating over the lists. | ||
However, I'm not sure if list index() and list remove() have significantly different time complexity - both seem to be O(n). | ||
In addition, using this method throws an error if either item is not in the list, | ||
which allows me to return False using try-except, instead of manually checking and returning False. | ||
''' | ||
try: | ||
my_item_index = self.inventory.index(my_item) | ||
their_item_index = vendor.inventory.index(their_item) | ||
|
||
swap_items = self.inventory[my_item_index], vendor.inventory[their_item_index] | ||
vendor.inventory[their_item_index], self.inventory[my_item_index] = swap_items | ||
Comment on lines
+46
to
+47
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 on this streamlined approach. The |
||
return True | ||
|
||
except: | ||
return False | ||
|
||
def swap_first_item(self, vendor): | ||
''' | ||
swap the first item between my inventory and vendor's inventory. | ||
return false if either inventory is empty. | ||
''' | ||
try: | ||
my_item = self.inventory[0] | ||
their_item = vendor.inventory[0] | ||
self.swap_items(vendor, my_item, their_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. great use of your previously created method. |
||
return True | ||
|
||
except: | ||
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. 👍🏽 |
||
''' | ||
return item with best condition in a given category (returns one item only) | ||
if no items match the given category, return None | ||
''' | ||
condition = -1 | ||
best_item = None | ||
items = self.get_by_category(category) | ||
|
||
for item in items: | ||
if item.condition > condition: | ||
best_item = item | ||
condition = item.condition | ||
|
||
return best_item | ||
|
||
def swap_best_by_category(self, other, my_priority, their_priority): | ||
''' | ||
swap items with best condition between my preferred category and another vendor's preferred category | ||
if no items are in the category in either vendor's inventory, return False | ||
''' | ||
my_item = self.get_best_by_category(their_priority) | ||
their_item = other.get_best_by_category(my_priority) | ||
Comment on lines
+88
to
+89
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 use of previously used methods. |
||
|
||
if None in (my_item, their_item): | ||
return False | ||
|
||
self.swap_items(other, my_item, their_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.
👍🏽