-
Notifications
You must be signed in to change notification settings - Fork 26
C22 - Phoenix - Elzara Turakulova #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
3f319b1
cf99cac
79e2084
cf28447
1886a33
4062753
f8de486
0e072a7
5af68f9
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 .item import Item | ||
| class Clothing(Item): | ||
| def __init__(self, id=None, fabric="Unknown", condition=0, age=0): | ||
| super().__init__(id, condition, age) | ||
| self.fabric = fabric | ||
|
|
||
| def __str__(self): | ||
| parent_str = super().__str__() | ||
| return f"{parent_str} It is made from {self.fabric} fabric." | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,10 @@ | ||
| class Decor: | ||
| pass | ||
| from .item import Item | ||
| class Decor(Item): | ||
| def __init__(self, id=None, width=0, length=0, condition=0, age=0): | ||
| super().__init__(id, condition, age) | ||
| self.width = width | ||
| self.length = length | ||
|
|
||
| def __str__(self): | ||
| parent_str = super().__str__() | ||
| return f"{parent_str} It takes up a {self.width} by {self.length} sized space." | ||
|
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 function looks great! Good use of the default parameters! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,9 @@ | ||
| class Electronics: | ||
| pass | ||
| from .item import Item | ||
| class Electronics(Item): | ||
| def __init__(self, id=None, type="Unknown", condition=0, age=0): | ||
| super().__init__(id, condition, age) | ||
| self.type = type | ||
|
|
||
| def __str__(self): | ||
| parent_str = super().__str__() | ||
| return f"{parent_str} This is a {self.type} device." | ||
|
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! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,30 @@ | ||
| import uuid | ||
| class Item: | ||
| pass | ||
| def __init__(self, id=None, condition=0, age=0): | ||
| self.id = uuid.uuid4().int if id is None else id | ||
|
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 ternary here! |
||
| self.condition = condition | ||
| self.age = age | ||
|
|
||
| def __str__(self): | ||
| return f"An object of type {self.get_category()} with id {self.id}." | ||
|
|
||
| def get_category(self): | ||
| return type(self).__name__ | ||
|
|
||
| def condition_description(self): | ||
| x = self.condition | ||
| if x < 0 or x > 5: | ||
| return "Condition unknown. Is this from another dimension?" | ||
| elif x < 1: | ||
| return "Yikes! This thing's seen better centuries." | ||
| elif x < 2: | ||
| return "Handle with care... and maybe a hazmat suit." | ||
| elif x < 3: | ||
| return "It's got 'character' (that's code for 'issues')." | ||
| elif x < 4: | ||
| return "Not too shabby, just don't look too closely." | ||
| elif x < 5: | ||
| return "Almost new, if you squint hard enough." | ||
| else: | ||
| return "Mint condition! Did you steal this from a time machine?" | ||
|
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. Your Item class looks really good too! I love the descriptions you have for your conditions! I also appreciate that you have given ranges for each condition description! this allows for a wider range of possibilities! |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,85 @@ | ||
| class Vendor: | ||
| pass | ||
| def __init__(self, inventory=None): | ||
| self.inventory = inventory or [] | ||
|
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 is not syntax I had seen before and I intuitively didn't think it would work (I knew it did because all of your tests pass), but great job researching and finding this particular syntax! |
||
|
|
||
| def add(self, item): | ||
| self.inventory.append(item) | ||
| return item | ||
|
|
||
| def remove(self, item): | ||
| if item not in self.inventory: | ||
| return False | ||
|
|
||
| self.inventory.remove(item) | ||
| return item | ||
|
|
||
| def get_by_id(self, num=None): | ||
| for item in self.inventory: | ||
| if item.id == num: | ||
| return item | ||
| return None | ||
|
Comment on lines
+5
to
+20
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. Your add, remove and get_by_id functions look great! |
||
|
|
||
| def swap_items(self, other_vendor, my_item, their_item): | ||
| if my_item not in self.inventory or their_item not in other_vendor.inventory: | ||
| return False | ||
|
Comment on lines
+23
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. Great job on this guard clause! |
||
|
|
||
| other_vendor.add(my_item) | ||
| self.add(their_item) | ||
| other_vendor.remove(their_item) | ||
| self.remove(my_item) | ||
|
Comment on lines
+26
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. My one challenge here would to be to see if you can combine these four lines into just two! Otherwise it looks great! |
||
|
|
||
| return True | ||
|
|
||
| def swap_first_item(self, other_vendor): | ||
| if not self.inventory or not other_vendor.inventory: | ||
| return False | ||
|
|
||
| self.swap_items(other_vendor, self.inventory[0], other_vendor.inventory[0]) | ||
|
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 use of the previous function! |
||
|
|
||
| return True | ||
|
|
||
| def get_by_category(self, category): | ||
|
|
||
| items = [item for item in self.inventory if item.get_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. Great list comprehension! |
||
| return items | ||
|
|
||
| def get_best_by_category(self, category): | ||
|
|
||
| items = self.get_by_category(category) | ||
|
|
||
| if not items: | ||
| return None | ||
|
|
||
| best_item = None | ||
| for item in items: | ||
| if best_item is None or item.condition > best_item.condition: | ||
| best_item = item | ||
|
Comment on lines
+53
to
+56
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 super small tweak, but if we can avoid having to check and see if best_item is None along with the more important condition of comparing the conditions, we should try that! |
||
|
|
||
| return best_item | ||
|
|
||
| def swap_best_by_category(self, other_vendor, my_priority, their_priority): | ||
|
|
||
| best_from_other = other_vendor.get_best_by_category(my_priority) | ||
| best_from_self = self.get_best_by_category(their_priority) | ||
|
|
||
| return self.swap_items(other_vendor, best_from_self, best_from_other) | ||
|
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. As always, this looks good too! |
||
|
|
||
| def find_newest_item(self, items): | ||
| if not items: | ||
| return None | ||
|
|
||
| newest_item = None | ||
| for item in items: | ||
| if newest_item is None or item.age < newest_item.age: | ||
| newest_item = item | ||
|
|
||
| return newest_item | ||
|
|
||
| def swap_by_newest(self, other_vendor): | ||
| my_new_item = self.find_newest_item(self.inventory) | ||
| their_new_item = self.find_newest_item(other_vendor.inventory) | ||
|
|
||
| if not my_new_item or not their_new_item: | ||
| return False | ||
|
|
||
| return self.swap_items(other_vendor, my_new_item, their_new_item) | ||
|
Comment on lines
+67
to
+85
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. Thank you for adding in these optional enhancements! They look really good! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import pytest | ||
| from swap_meet.item import Item | ||
| from swap_meet.vendor import Vendor | ||
| from swap_meet.clothing import Clothing | ||
| from swap_meet.decor import Decor | ||
| from swap_meet.electronics import Electronics | ||
|
|
||
| def test_age_valid_is_true(): | ||
| num = 5 | ||
|
|
||
| item = Item(age=num) | ||
| assert isinstance(item.age, int) | ||
| assert item.age == num | ||
|
|
||
| def test_swap_by_newest_no_inventory_is_false(): | ||
| tai = Vendor( | ||
| inventory=[] | ||
| ) | ||
|
|
||
| item_a = Clothing(age=1) | ||
| item_b = Decor(age=3) | ||
| item_c = Clothing(age=5) | ||
| jesse = Vendor( | ||
| inventory=[item_a, item_b, item_c] | ||
| ) | ||
|
|
||
| result = tai.swap_by_newest( | ||
| other_vendor=jesse | ||
| ) | ||
|
|
||
| assert not result | ||
| assert len(tai.inventory) == 0 | ||
| assert len(jesse.inventory) == 3 | ||
| assert item_a in jesse.inventory | ||
| assert item_b in jesse.inventory | ||
| assert item_c in jesse.inventory | ||
|
|
||
| def test_swap_by_newest_valid_input_true(): | ||
| # Arrange | ||
| # me | ||
| item_a = Decor(age=2) | ||
| item_b = Electronics(age=4) | ||
| item_c = Decor(age=4) | ||
| tai = Vendor( | ||
| inventory=[item_a, item_b, item_c] | ||
| ) | ||
|
|
||
| # them | ||
| item_d = Clothing(age=2) | ||
| item_e = Decor(age=4) | ||
| item_f = Clothing(age=4) | ||
| jesse = Vendor( | ||
| inventory=[item_d, item_e, item_f] | ||
| ) | ||
|
|
||
| # Act | ||
| result = tai.swap_by_newest( | ||
| other_vendor=jesse | ||
| ) | ||
| assert result | ||
| assert len(tai.inventory) == 3 | ||
| assert len(jesse.inventory) == 3 | ||
| assert item_a in jesse.inventory | ||
| assert item_d in tai.inventory | ||
| assert item_b in tai.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. Your optional test suite looks great! |
||
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.
I really like the way you've parsed out the parent_str here. Feel free to skip that step and just include the
super().__str__()in the f String!