-
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
C17-Sharks-Raha #15
base: main
Are you sure you want to change the base?
C17-Sharks-Raha #15
Changes from all commits
74cd3aa
1bea868
6857e8a
3e6b403
8b0b469
6ab3895
3ee9786
fe3572f
ea6bffc
1024133
f5578c5
2accffd
43f0b60
e90cdf7
f736e39
bd7494c
4261992
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,39 @@ | ||
from swap_meet.vendor import Vendor | ||
from swap_meet.item import Item | ||
from swap_meet.clothing import Clothing | ||
from swap_meet.decor import Decor | ||
from swap_meet.electronics import Electronics | ||
|
||
# vendor = Vendor(inventory=["a", "b", "c"]) | ||
# print(vendor.inventory) | ||
# item_a = Item(category = "clothing", condition = 1, age = 2) | ||
# item_b = Item(category = "Decor", condition = 3, age = 1) | ||
# item_c = Item(category = "Electerical", condition = 2, age = 3) | ||
# item_d = Item(category = "clothing", condition = 4, age = 5) | ||
# item_e = Item(category = "Decor", condition = 2, age = 4) | ||
# item_f = Item(category = "Electronics", condition = 3, age = 3) | ||
# raha = Vendor([item_a,item_b,item_c]) | ||
# # print (raha.inventory) | ||
# mitra = Vendor([item_d,item_e,item_f]) | ||
# print(vendor.add("d")) | ||
# print(vendor.remove("d")) | ||
# print(vendor.inventory) | ||
item_a = Decor(age = 2.0) | ||
item_b = Electronics(age=4.0) | ||
item_c = Decor(age=4.0) | ||
tai = Vendor( | ||
inventory=[item_b, item_a, item_c] | ||
) | ||
|
||
# them | ||
item_d = Clothing(age=2.0) | ||
item_e = Decor(age=4.0) | ||
item_f = Clothing(age=4.0) | ||
jesse = Vendor( | ||
inventory=[item_e, item_d,item_f] | ||
) | ||
print(tai.inventory) | ||
# print(jesse.inventory) | ||
|
||
print(tai.swap_first_item(jesse)) | ||
print(tai.swap_by_newest(jesse)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,29 @@ | ||
class Clothing: | ||
pass | ||
from swap_meet.item import Item | ||
class Clothing(Item): | ||
|
||
""" | ||
A class to represent a cloth. | ||
|
||
... | ||
|
||
Attributes | ||
---------- | ||
category : string (optional) | ||
a string that represents the category of the cloth | ||
condition: | ||
a float number between 0 and 5 that represents the current condition of the cloth | ||
age : | ||
an integer that represents the age of the cloth | ||
Methods | ||
------- | ||
__str__: | ||
- returns "The finest clothing you could wear." | ||
""" | ||
def __init__(self, condition = 0, age = 0): | ||
super().__init__(category = "Clothing", condition = condition, age = age) | ||
|
||
def __str__(self): | ||
return "The finest clothing you could wear." | ||
|
||
|
||
# pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,24 @@ | ||
class Decor: | ||
pass | ||
from swap_meet.item import Item | ||
class Decor(Item): | ||
""" | ||
A class to represent a decor. | ||
... | ||
|
||
Attributes | ||
---------- | ||
category : string (optional) | ||
a string that represents the category of the decor | ||
condition: | ||
a float number between 0 and 5 that represents the current condition of the decor | ||
age : | ||
an integer that represents the age of the decor | ||
Methods | ||
------- | ||
__str__: | ||
- returns "Something to decorate your space." | ||
""" | ||
def __init__(self, condition = 0, age = 0): | ||
super().__init__(category = "Decor", condition = condition, age = age) | ||
|
||
def __str__(self): | ||
return "Something to decorate your space." | ||
Comment on lines
+20
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. 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,26 @@ | ||
class Electronics: | ||
pass | ||
|
||
from swap_meet.item import Item | ||
class Electronics(Item): | ||
""" | ||
A class to represent an electronic item. | ||
... | ||
Attributes | ||
---------- | ||
category : string (optional) | ||
a string that represents the category of the electronic item | ||
condition: | ||
a float number between 0 and 5 that represents the current condition of the electronic item | ||
age : | ||
an integer that represents the age of the electronic item | ||
Methods | ||
------- | ||
__str__: | ||
- returns "A gadget full of buttons and secrets." | ||
""" | ||
def __init__(self, condition = 0, age = 0): | ||
super().__init__(category = "Electronics", condition = condition, age = age) | ||
|
||
def __str__(self): | ||
return "A gadget full of buttons and secrets." | ||
Comment on lines
+22
to
+26
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,51 @@ | ||
class Item: | ||
pass | ||
""" | ||
A class to represent an item. | ||
|
||
... | ||
|
||
Attributes | ||
---------- | ||
category : string (optional) | ||
a string that represents the category of the item | ||
condition: | ||
a float number between 0 and 5 that represents the current condition of the item | ||
|
||
age : | ||
an integer that represents the age of the item | ||
|
||
Methods | ||
------- | ||
condition_description: | ||
- returns "Just trash it!" if condition is between 0 and 1 | ||
- returns "Enjoy the last moments!" if condition is between 1 and 2 | ||
- returns "As good as 30 year old wine!" if condition is between 2 and 3 | ||
- returns "Better than the new one" if condition is between 3 and 4 | ||
- returns "No need to ask!" if condition is between 4 and 5 | ||
- returns False if condition is not between 0 and 5 | ||
""" | ||
def __init__(self,category = "", condition = 0.0, age = 0): | ||
self.category = category | ||
self.condition = condition | ||
self.age = age | ||
def __str__(self): | ||
|
||
return f"Hello World!" | ||
|
||
def condition_description(self): | ||
if 0 <= self.condition < 1: | ||
return "Just trash it!" | ||
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. Love this description 😆🗑️ |
||
elif 1 <= self.condition < 2: | ||
return "Enjoy the last moments!" | ||
elif 2 <= self.condition < 3: | ||
return "As good as 30 year old wine!" | ||
elif 3 <= self.condition < 4: | ||
return "Better than the new one" | ||
elif 4 <= self.condition <= 5: | ||
return "No need to ask!" | ||
else: | ||
return False | ||
Comment on lines
+27
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 work! One minor suggestion:
|
||
|
||
|
||
|
||
# pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,159 @@ | ||
import operator | ||
|
||
class Vendor: | ||
pass | ||
|
||
""" | ||
A class to represent a vendor. | ||
|
||
... | ||
|
||
Attributes | ||
---------- | ||
inventory : list (optional) | ||
a list of available items for each vendor | ||
|
||
|
||
Methods | ||
------- | ||
add(item): | ||
- adds a new item to the vendor's inventory | ||
- Returns the updated inventory list | ||
|
||
remove(item): | ||
- removes an item from the vendor's inventory list | ||
- Returns the updated inventory list | ||
|
||
get_best_by_category(category): | ||
- takes a string representing a category | ||
- Returns a list of items in the inventory with the same category | ||
|
||
swap_items(other_vendor,my_item,their_item): | ||
- takes an instance of another vendor, two instances of Item item (my_item) | ||
and item(their_item) | ||
- removes my_item from vendor's inventory and adds it to the other_vendor inventory | ||
- removes their_item from other_vendor inventory and adds it to the vendor inventory | ||
- return True | ||
- returns False if my_item not in vendor's inventory or their item not in | ||
other_vendor's inventory | ||
|
||
swap_first_item(friend_vendor): | ||
- takes in an instance of another vendor friend_vendor | ||
- removes the first item in vendor's inventory and add that to the friend_vendor inventory | ||
- removes the first item in friend_vendor inventory and add that to the vendor inventory | ||
- returns True | ||
- returns False if vendor inventory or friend_vendor inventory is empty | ||
|
||
get_best_by_category(category): | ||
- takes in a string representing the category | ||
- looks through vendor's inventory for the item with the highest condition | ||
and matching category | ||
- returns one item that matches the aforementioned criteria | ||
|
||
swap_best_by_category(other,my_priority,their_priority): | ||
- takes in an instance of another vendor (other), a string for a category that the vendor wants | ||
to receive (my_priority) and a string for a category that the other vendors wants to receive (other) | ||
- passes my_priority and their_priority to the get_best_by_category function to find items with highest | ||
condition in each category | ||
- passes items to the swap_items to swap those items | ||
- returns True | ||
- returns False if my_priority not in vendor's inventory or their priority not in the | ||
other's inventory | ||
|
||
swap_by_newest(other): | ||
- takes in another instance of vendor (other) | ||
- sorts the vendor inventory and other inventory based on the age of items | ||
- returns swap_first_item for the vendor and other vendor with sorted inventories | ||
|
||
""" | ||
|
||
|
||
def __init__(self, inventory = None): | ||
if not inventory: | ||
inventory = [] | ||
self.inventory = inventory | ||
Comment on lines
+71
to
+73
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. Good work! We can also use a ternary to assign the value of self.inventory = inventory if inventory else [] |
||
|
||
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. 👍 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. Following pep8 guidelines, we should add a space after every comma when listing parameters/arguments: def add(self, item): |
||
self.inventory.append(item) | ||
return item | ||
|
||
def remove(self,item): | ||
try: | ||
self.inventory.remove(item) | ||
return item | ||
except ValueError as ve: | ||
return False | ||
Comment on lines
+79
to
+84
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 use of a try/except! |
||
|
||
def get_by_category(self,category): | ||
result = [item for item in self.inventory if item.category == category] | ||
return result | ||
Comment on lines
+86
to
+88
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 a ternary! |
||
|
||
def swap_items(self,other_vendor,my_item,their_item): | ||
if not all ((other_vendor, my_item, their_item)): | ||
return False | ||
if my_item == their_item: | ||
return True | ||
elif my_item in self.inventory and their_item in other_vendor.inventory: | ||
self.add(their_item) | ||
other_vendor.add(my_item) | ||
other_vendor.remove(their_item) | ||
self.remove(my_item) | ||
return True | ||
Comment on lines
+90
to
+100
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. 👍 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. Another approach: def swap_items(self,other_vendor,my_item,their_item):
if not all ((other_vendor, my_item, their_item)):
return False
if my_item == their_item:
return True
if my_item not in self.inventory or their_item not in other_vendor.inventory:
return False
self.add(their_item)
other_vendor.add(my_item)
other_vendor.remove(their_item)
self.remove(my_item)
return True This was a minor change but notice how the primary logic of swapping items is now unintended and more in focus. This is due to the handy guard clause in the previous line. |
||
|
||
def swap_first_item(self, friend_vendor): | ||
if not all((friend_vendor.inventory, self.inventory)): | ||
return False | ||
|
||
friend_vendor.add(self.inventory[0]) | ||
self.add(friend_vendor.inventory[0]) | ||
friend_vendor.remove(friend_vendor.inventory[0]) | ||
self.remove(self.inventory[0]) | ||
return True | ||
Comment on lines
+102
to
+110
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! Another option is to use the 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. Currently, swapping any items has a time complexity of O(n) due to the
Here's more info on Python multiple-assignment: https://www.w3schools.com/python/gloss_python_assign_value_to_multiple_variables.asp |
||
|
||
def get_best_by_category(self,category): | ||
matched_category = [item for item in self.inventory if category == item.category] | ||
if len(matched_category) == 0: | ||
return None | ||
max = matched_category[0] | ||
for item in matched_category: | ||
if item.condition > max.condition: | ||
max = item | ||
return max | ||
Comment on lines
+113
to
+120
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! We could also use the |
||
|
||
def swap_best_by_category(self,other,my_priority,their_priority): | ||
their_choice = self.get_best_by_category(their_priority) | ||
my_choice = other.get_best_by_category(my_priority) | ||
|
||
if my_choice and their_choice: | ||
self.swap_items(other, their_choice, my_choice) | ||
return True | ||
return False | ||
Comment on lines
+122
to
+129
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 swap_by_newest(self,other): | ||
if not all ((self.inventory , other.inventory)): | ||
return None | ||
self.inventory = sorted(self.inventory, key=lambda item: item.age, reverse=False) | ||
other.inventory = sorted(other.inventory, key=lambda item: item.age, reverse=False) | ||
return self.swap_first_item(other) | ||
Comment on lines
+133
to
+138
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 use of |
||
|
||
|
||
|
||
|
||
|
||
# def newest_item(self): | ||
# if self.inventory == []: | ||
# return None | ||
# new_item = self.inventory[0] | ||
# for item in self.inventory: | ||
# if item.age < new_item.age: | ||
# new_item = item | ||
# return new_item | ||
|
||
# sorted_inventory = self.inventory.sort(key = lambda item: item.age) | ||
# return sorted_inventory | ||
# new_self = self.newest_item() | ||
# new_other = other.newest_item() | ||
# return new_self.inventory.swap_first_item(new_other.inventory) | ||
# return self.swap_items(other, new_self,new_other) | ||
# pass |
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.
👍 Good work in ensuring that all
Clothing
instances have acategory
of"Clothing"
by passing the category value into the parent constructor.