From 09c740aeb422e271ef1ccff79727975db0fdc0ba Mon Sep 17 00:00:00 2001 From: April Zhang Date: Tue, 4 Apr 2023 00:57:59 -0700 Subject: [PATCH 01/21] finished wave 1&2 draft --- swap_meet/item.py | 12 +++++++++++- swap_meet/vendor.py | 21 ++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index 560d759c2..efd0e991d 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,2 +1,12 @@ +import uuid class Item: - pass \ No newline at end of file + + def __init__(self, id=None): + if id: + self.id = id + else: + self.id = uuid.uuid4.int + + def get_category(self): + return self.__class__.__name__ + \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 87302c056..ec2beb221 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,2 +1,21 @@ +from item import Item class Vendor: - pass \ No newline at end of file + def __init__(self): + self.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 + else: + return False + + def get_by_id(self, id): + for item in self.inventory: + if id == self.id: + return item + return None \ No newline at end of file From d07854e8a8f8134774e24a44e301d469712e43d3 Mon Sep 17 00:00:00 2001 From: April Zhang Date: Tue, 4 Apr 2023 11:47:54 -0700 Subject: [PATCH 02/21] create Vendor class & Item class and passed unit test 1&2 --- swap_meet/item.py | 3 ++- swap_meet/vendor.py | 12 ++++++++---- tests/unit_tests/test_wave_01.py | 14 ++++++++------ tests/unit_tests/test_wave_02.py | 12 ++++++------ 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index efd0e991d..d6b34c232 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -5,8 +5,9 @@ def __init__(self, id=None): if id: self.id = id else: - self.id = uuid.uuid4.int + self.id = uuid.uuid4().int def get_category(self): return self.__class__.__name__ + \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index ec2beb221..4480401c9 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,7 +1,10 @@ -from item import Item +from swap_meet.item import Item class Vendor: - def __init__(self): - self.inventory = [] + def __init__(self, inventory=None): + #Why cannot pass test without inventory? + if inventory is None: + inventory = [] + self.inventory = inventory def add(self, item): self.inventory.append(item) @@ -16,6 +19,7 @@ def remove(self, item): def get_by_id(self, id): for item in self.inventory: - if id == self.id: + if id == item.id: + # item.id Is this`item` refers to the string in the inventory? return item return None \ No newline at end of file diff --git a/tests/unit_tests/test_wave_01.py b/tests/unit_tests/test_wave_01.py index 58478ccf9..94f9ee1d8 100644 --- a/tests/unit_tests/test_wave_01.py +++ b/tests/unit_tests/test_wave_01.py @@ -2,12 +2,12 @@ import pytest from swap_meet.vendor import Vendor -@pytest.mark.skip +# @pytest.mark.skip def test_vendor_has_inventory(): vendor = Vendor() assert len(vendor.inventory) == 0 -@pytest.mark.skip +# @pytest.mark.skip def test_vendor_takes_optional_inventory(): inventory = ["a", "b", "c"] vendor = Vendor(inventory=inventory) @@ -16,7 +16,7 @@ def test_vendor_takes_optional_inventory(): assert "b" in vendor.inventory assert "c" in vendor.inventory -@pytest.mark.skip +# @pytest.mark.skip def test_adding_to_inventory(): vendor = Vendor() item = "new item" @@ -27,7 +27,7 @@ def test_adding_to_inventory(): assert item in vendor.inventory assert result == item -@pytest.mark.skip +# @pytest.mark.skip def test_removing_from_inventory_returns_item(): item = "item to remove" vendor = Vendor( @@ -40,7 +40,7 @@ def test_removing_from_inventory_returns_item(): assert item not in vendor.inventory assert result == item -@pytest.mark.skip +# @pytest.mark.skip def test_removing_not_found_is_false(): item = "item to remove" vendor = Vendor( @@ -49,7 +49,9 @@ def test_removing_not_found_is_false(): result = vendor.remove(item) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") + + assert result == False # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* diff --git a/tests/unit_tests/test_wave_02.py b/tests/unit_tests/test_wave_02.py index 229165c75..c683f6b62 100644 --- a/tests/unit_tests/test_wave_02.py +++ b/tests/unit_tests/test_wave_02.py @@ -2,30 +2,30 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip +# @pytest.mark.skip def test_items_have_default_uuid_length_id(): item = Item() assert isinstance(item.id, int) assert len(str(item.id)) >= 32 -@pytest.mark.skip +# @pytest.mark.skip def test_item_instances_have_different_default_ids(): item_a = Item() item_b = Item() assert item_a.id != item_b.id -@pytest.mark.skip +# @pytest.mark.skip def test_items_use_custom_id_if_passed(): item = Item(id=12345) assert isinstance(item.id, int) assert item.id == 12345 -@pytest.mark.skip +# @pytest.mark.skip def test_item_obj_returns_text_item_for_category(): item = Item() assert item.get_category() == "Item" -@pytest.mark.skip +# @pytest.mark.skip def test_get_item_by_id(): test_id = 12345 item_custom_id = Item(id=test_id) @@ -36,7 +36,7 @@ def test_get_item_by_id(): result_item = vendor.get_by_id(test_id) assert result_item is item_custom_id -@pytest.mark.skip +# @pytest.mark.skip def test_get_item_by_id_no_matching(): test_id = 12345 item_a = Item() From af5b00c768f02ece3be3792880ca48f9619485de Mon Sep 17 00:00:00 2001 From: April Zhang Date: Tue, 4 Apr 2023 14:38:05 -0700 Subject: [PATCH 03/21] create swap_items function of Vendor --- swap_meet/item.py | 3 ++- swap_meet/vendor.py | 14 +++++++++++--- tests/unit_tests/test_wave_03.py | 16 +++++++++------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index d6b34c232..d01e6992d 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -10,4 +10,5 @@ def __init__(self, id=None): def get_category(self): return self.__class__.__name__ - \ No newline at end of file + def __str__(self): + return f"An object of type Item with id {self.id}." \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 4480401c9..b02f3b1ef 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,7 +1,6 @@ from swap_meet.item import Item class Vendor: def __init__(self, inventory=None): - #Why cannot pass test without inventory? if inventory is None: inventory = [] self.inventory = inventory @@ -20,6 +19,15 @@ def remove(self, item): def get_by_id(self, id): for item in self.inventory: if id == item.id: - # item.id Is this`item` refers to the string in the inventory? return item - return None \ No newline at end of file + return None + + 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 + else: + self.remove(my_item) + other_vendor.add(my_item) + other_vendor.remove(their_item) + self.add(their_item) + return True \ No newline at end of file diff --git a/tests/unit_tests/test_wave_03.py b/tests/unit_tests/test_wave_03.py index c85da6797..db3a4bd69 100644 --- a/tests/unit_tests/test_wave_03.py +++ b/tests/unit_tests/test_wave_03.py @@ -2,7 +2,7 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip +# @pytest.mark.skip def test_item_overrides_to_string(): test_id = 12345 item = Item(id=test_id) @@ -12,7 +12,7 @@ def test_item_overrides_to_string(): expected_result = f"An object of type Item with id {test_id}." assert item_as_string == expected_result -@pytest.mark.skip +# @pytest.mark.skip def test_swap_items_returns_true(): item_a = Item() item_b = Item() @@ -40,7 +40,7 @@ def test_swap_items_returns_true(): assert item_b in jolie.inventory assert result -@pytest.mark.skip +# @pytest.mark.skip def test_swap_items_when_my_item_is_missing_returns_false(): item_a = Item() item_b = Item() @@ -67,7 +67,7 @@ def test_swap_items_when_my_item_is_missing_returns_false(): assert item_e in jolie.inventory assert not result -@pytest.mark.skip +# @pytest.mark.skip def test_swap_items_when_their_item_is_missing_returns_false(): item_a = Item() item_b = Item() @@ -94,7 +94,7 @@ def test_swap_items_when_their_item_is_missing_returns_false(): assert item_e in jolie.inventory assert not result -@pytest.mark.skip +# @pytest.mark.skip def test_swap_items_from_my_empty_returns_false(): fatimah = Vendor( inventory=[] @@ -114,7 +114,7 @@ def test_swap_items_from_my_empty_returns_false(): assert len(jolie.inventory) == 2 assert not result -@pytest.mark.skip +# @pytest.mark.skip def test_swap_items_from_their_empty_returns_false(): item_a = Item() item_b = Item() @@ -131,7 +131,9 @@ def test_swap_items_from_their_empty_returns_false(): result = fatimah.swap_items(jolie, item_b, nobodys_item) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") + assert result == False + # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* From bfc92c0b11bc3ae728a760979ae7ff60d19062fa Mon Sep 17 00:00:00 2001 From: April Zhang Date: Tue, 4 Apr 2023 15:02:40 -0700 Subject: [PATCH 04/21] W4 is not finished --- swap_meet/vendor.py | 13 +++++++++++++ tests/unit_tests/test_wave_04.py | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index b02f3b1ef..1d1da6ca5 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -30,4 +30,17 @@ def swap_items(self, other_vendor, my_item, their_item): other_vendor.add(my_item) other_vendor.remove(their_item) self.add(their_item) + return True + + def swap_first_item(self, other_vendor): + + if self.inventory is None or other_vendor.inventory is None: + return False + else: + my_first = self.inventory[0] + friend_first = other_vendor.inventory[0] + self.remove(my_first) + other_vendor.add(my_first) + other_vendor.remove(friend_first) + self.add(friend_first) return True \ No newline at end of file diff --git a/tests/unit_tests/test_wave_04.py b/tests/unit_tests/test_wave_04.py index 87addbbf6..135388335 100644 --- a/tests/unit_tests/test_wave_04.py +++ b/tests/unit_tests/test_wave_04.py @@ -2,7 +2,7 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip +# @pytest.mark.skip def test_swap_first_item_returns_true(): item_a = Item() item_b = Item() @@ -30,7 +30,7 @@ def test_swap_first_item_returns_true(): assert item_a in jolie.inventory assert result -@pytest.mark.skip +# @pytest.mark.skip def test_swap_first_item_from_my_empty_returns_false(): fatimah = Vendor( inventory=[] @@ -48,7 +48,7 @@ def test_swap_first_item_from_my_empty_returns_false(): assert len(jolie.inventory) == 2 assert not result -@pytest.mark.skip +# @pytest.mark.skip def test_swap_first_item_from_their_empty_returns_false(): item_a = Item() item_b = Item() From c25d21acc94d0a1ef69fc1e7c00d031c2de8745f Mon Sep 17 00:00:00 2001 From: April Zhang Date: Tue, 4 Apr 2023 15:13:33 -0700 Subject: [PATCH 05/21] create swap_first_item function in Vendor --- swap_meet/vendor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 1d1da6ca5..befca589d 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -34,7 +34,7 @@ def swap_items(self, other_vendor, my_item, their_item): def swap_first_item(self, other_vendor): - if self.inventory is None or other_vendor.inventory is None: + if self.inventory == [] or other_vendor.inventory == []: return False else: my_first = self.inventory[0] From 4cdb9266e6f60e486e612a85c708e922d5bb4236 Mon Sep 17 00:00:00 2001 From: April Zhang Date: Tue, 4 Apr 2023 21:11:02 -0700 Subject: [PATCH 06/21] complete functions in Clothing module --- swap_meet/clothing.py | 11 ++++++++++- swap_meet/vendor.py | 5 +++-- tests/unit_tests/test_wave_05.py | 8 ++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index b8afdeb1e..8adc799b5 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,2 +1,11 @@ +import uuid class Clothing: - pass \ No newline at end of file + def __init__(self, id=uuid.uuid4().int, fabric="Unknown"): + self.id = id + self.fabric = fabric + + def get_category(self): + return "Clothing" + + def __str__(self): + return f"An object of type Clothing with id {self.id}. It is made from {self.fabric} fabric." \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index befca589d..a0dfc84f9 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -14,7 +14,7 @@ def remove(self, item): self.inventory.remove(item) return item else: - return False + return False # IF raise exception it will stop the whole program, for example it will not be necessery to have line 26-28 to return F def get_by_id(self, id): for item in self.inventory: @@ -34,7 +34,8 @@ def swap_items(self, other_vendor, my_item, their_item): def swap_first_item(self, other_vendor): - if self.inventory == [] or other_vendor.inventory == []: + if self.inventory == [] or other_vendor.inventory == []: + # Empty list is not equal to default None. Because line 5 `[]` is passed to inventory. return False else: my_first = self.inventory[0] diff --git a/tests/unit_tests/test_wave_05.py b/tests/unit_tests/test_wave_05.py index 46e208237..90e57e35f 100644 --- a/tests/unit_tests/test_wave_05.py +++ b/tests/unit_tests/test_wave_05.py @@ -7,17 +7,17 @@ # ~~~~~ Clothing Tests ~~~~~ -@pytest.mark.skip +# @pytest.mark.skip def test_clothing_has_default_uuid_length_id(): clothing = Clothing() check_for_default_uuid_length_id(clothing) -@pytest.mark.skip +# @pytest.mark.skip def test_clothing_has_expected_category_and_custom_id(): clothing = Clothing(id=TEST_CUSTOM_ID) check_category_and_custom_id(clothing, TEST_CUSTOM_ID, "Clothing") -@pytest.mark.skip +# @pytest.mark.skip def test_clothing_has_expected_default_to_str(): clothing = Clothing(id=TEST_CUSTOM_ID) expected_str = ( @@ -26,7 +26,7 @@ def test_clothing_has_expected_default_to_str(): ) assert str(clothing) == expected_str -@pytest.mark.skip +# @pytest.mark.skip def test_clothing_has_expected_to_str_with_custom_fabric(): clothing = Clothing(id=TEST_CUSTOM_ID, fabric="Pinstriped") expected_str = ( From b57492b8478a05c1dc8094e4b8e378c5f4d53d2a Mon Sep 17 00:00:00 2001 From: April Zhang Date: Tue, 4 Apr 2023 22:36:07 -0700 Subject: [PATCH 07/21] complete inhatitance str method in three classes --- swap_meet/clothing.py | 11 ++++++----- swap_meet/decor.py | 15 +++++++++++++-- swap_meet/electronics.py | 11 +++++++++-- swap_meet/item.py | 10 +++++++--- tests/unit_tests/test_wave_05.py | 16 ++++++++-------- 5 files changed, 43 insertions(+), 20 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index 8adc799b5..30cc83f8e 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,11 +1,12 @@ import uuid -class Clothing: - def __init__(self, id=uuid.uuid4().int, fabric="Unknown"): - self.id = id +from swap_meet.item import Item +class Clothing(Item): + def __init__(self, id=uuid.uuid4().int, fabric="Unknown", condition=0): + super().__init__(id, condition) self.fabric = fabric - + def get_category(self): return "Clothing" def __str__(self): - return f"An object of type Clothing with id {self.id}. It is made from {self.fabric} fabric." \ No newline at end of file + return super().__str__() + f" It is made from {self.fabric} fabric." \ No newline at end of file diff --git a/swap_meet/decor.py b/swap_meet/decor.py index eab7a9dbe..8b2b86271 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,2 +1,13 @@ -class Decor: - pass \ No newline at end of file +import uuid +from swap_meet.item import Item +class Decor(Item): + def __init__(self, id = uuid.uuid4().int, width=0, length=0, condition=0): + super().__init__(id, condition) + self.width = width + self.length = length + + def get_category(self): + return "Decor" + + def __str__(self): + return super().__str__() + f" It takes up a {self.width} by {self.length} sized space." \ No newline at end of file diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 2f9dff68a..9c17ccf07 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,2 +1,9 @@ -class Electronics: - pass +import uuid +from swap_meet.item import Item +class Electronics(Item): + def __init__(self, id=uuid.uuid4().int, type="Unknown", condition=0): + super().__init__(id, condition) + self.type = type + + def __str__(self): + return super().__str__() + f" This is a {self.type} device." \ No newline at end of file diff --git a/swap_meet/item.py b/swap_meet/item.py index d01e6992d..1d14c88ce 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,14 +1,18 @@ import uuid class Item: - def __init__(self, id=None): + def __init__(self, id=None, condition=0): if id: self.id = id else: self.id = uuid.uuid4().int - + self.condition = condition + def get_category(self): return self.__class__.__name__ def __str__(self): - return f"An object of type Item with id {self.id}." \ No newline at end of file + return f"An object of type {self.__class__.__name__} with id {self.id}." + + def condition_description(self): + pass \ No newline at end of file diff --git a/tests/unit_tests/test_wave_05.py b/tests/unit_tests/test_wave_05.py index 90e57e35f..c7640aa7a 100644 --- a/tests/unit_tests/test_wave_05.py +++ b/tests/unit_tests/test_wave_05.py @@ -37,17 +37,17 @@ def test_clothing_has_expected_to_str_with_custom_fabric(): # ~~~~~ Decor Tests ~~~~~ -@pytest.mark.skip +# @pytest.mark.skip def test_decor_has_default_uuid_length_id(): decor = Decor() check_for_default_uuid_length_id(decor) -@pytest.mark.skip +# @pytest.mark.skip def test_decor_has_expected_category_and_custom_id(): decor = Decor(id=TEST_CUSTOM_ID) check_category_and_custom_id(decor, TEST_CUSTOM_ID, "Decor") -@pytest.mark.skip +# @pytest.mark.skip def test_decor_has_expected_default_to_str(): decor = Decor(id=TEST_CUSTOM_ID) expected_str = ( @@ -56,7 +56,7 @@ def test_decor_has_expected_default_to_str(): ) assert str(decor) == expected_str -@pytest.mark.skip +# @pytest.mark.skip def test_decor_has_expected_to_str_with_custom_size(): decor = Decor(id=TEST_CUSTOM_ID, width=3, length=12) expected_str = ( @@ -67,17 +67,17 @@ def test_decor_has_expected_to_str_with_custom_size(): # ~~~~~ Electronics Tests ~~~~~ -@pytest.mark.skip +# @pytest.mark.skip def test_electronics_has_default_uuid_length_id(): electronics = Electronics() check_for_default_uuid_length_id(electronics) -@pytest.mark.skip +# @pytest.mark.skip def test_electronics_has_expected_category_and_custom_id(): electronics = Electronics(id=TEST_CUSTOM_ID) check_category_and_custom_id(electronics, TEST_CUSTOM_ID, "Electronics") -@pytest.mark.skip +# @pytest.mark.skip def test_electronics_has_expected_default_to_str(): electronics = Electronics(id=TEST_CUSTOM_ID) expected_str = ( @@ -86,7 +86,7 @@ def test_electronics_has_expected_default_to_str(): ) assert str(electronics) == expected_str -@pytest.mark.skip +# @pytest.mark.skip def test_electronics_has_expected_to_str_with_custom_type(): electronics = Electronics(id=TEST_CUSTOM_ID, type="Mobile Phone") expected_str = ( From 647e52d659800a2475b96b1456684950e9324fe1 Mon Sep 17 00:00:00 2001 From: April Zhang Date: Tue, 4 Apr 2023 23:07:13 -0700 Subject: [PATCH 08/21] complete condition_description function with wit description --- swap_meet/item.py | 13 ++++++++++++- swap_meet/vendor.py | 1 - tests/unit_tests/test_wave_05.py | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index 1d14c88ce..23ba35852 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -15,4 +15,15 @@ def __str__(self): return f"An object of type {self.__class__.__name__} with id {self.id}." def condition_description(self): - pass \ No newline at end of file + if self.condition == 5: + return "This item is in tip-top shape!" + elif self.condition == 4: + return "This item still has plenty of life left and character to spare." + elif self.condition == 3: + return "It's not down for the count - it's still got some fight left in it!" + elif self.condition == 2: + return "Don't judge this item by its appearence. It's still got some tricks up its sleeve and plenty of use left." + elif self.condition == 1: + return "This item may not have much life left in it, but it's not quite ready to give up the ghost yet." + else: + return "Oh... :'( This item has seen better days." \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index a0dfc84f9..ce04ae4a6 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,4 +1,3 @@ -from swap_meet.item import Item class Vendor: def __init__(self, inventory=None): if inventory is None: diff --git a/tests/unit_tests/test_wave_05.py b/tests/unit_tests/test_wave_05.py index c7640aa7a..b7f451e1a 100644 --- a/tests/unit_tests/test_wave_05.py +++ b/tests/unit_tests/test_wave_05.py @@ -98,7 +98,7 @@ def test_electronics_has_expected_to_str_with_custom_type(): # ~~~~~ Item Tests ~~~~~ -@pytest.mark.skip +# @pytest.mark.skip def test_items_have_condition_as_float(): items = [ Clothing(condition=3.5), @@ -108,7 +108,7 @@ def test_items_have_condition_as_float(): for item in items: assert item.condition == pytest.approx(3.5) -@pytest.mark.skip +# @pytest.mark.skip def test_items_have_condition_descriptions_that_are_the_same_regardless_of_type(): items = [ Clothing(condition=5), From 7412a5f79df03f7e4d91500316f5ea2a87a4972b Mon Sep 17 00:00:00 2001 From: April Zhang Date: Wed, 5 Apr 2023 01:02:49 -0700 Subject: [PATCH 09/21] complete get_by_category method --- swap_meet/clothing.py | 3 --- swap_meet/decor.py | 4 ++-- swap_meet/vendor.py | 12 +++++++++++- tests/unit_tests/test_wave_06.py | 7 ++++--- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index 30cc83f8e..de984efb4 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -4,9 +4,6 @@ class Clothing(Item): def __init__(self, id=uuid.uuid4().int, fabric="Unknown", condition=0): super().__init__(id, condition) self.fabric = fabric - - def get_category(self): - return "Clothing" def __str__(self): return super().__str__() + f" It is made from {self.fabric} fabric." \ No newline at end of file diff --git a/swap_meet/decor.py b/swap_meet/decor.py index 8b2b86271..254b41b96 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -6,8 +6,8 @@ def __init__(self, id = uuid.uuid4().int, width=0, length=0, condition=0): self.width = width self.length = length - def get_category(self): - return "Decor" + # def get_category(self): + # return "Decor" def __str__(self): return super().__str__() + f" It takes up a {self.width} by {self.length} sized space." \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index ce04ae4a6..7719b806d 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -43,4 +43,14 @@ def swap_first_item(self, other_vendor): other_vendor.add(my_first) other_vendor.remove(friend_first) self.add(friend_first) - return True \ No newline at end of file + return True + + def get_by_category(self, category): + category_items = [] + for item in self.inventory: + if item.get_category() == category: + category_items.append(item) + return category_items + + def get_best_by_category(self, category): + pass \ No newline at end of file diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index ad51bf42d..e99cbd256 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -5,7 +5,7 @@ from swap_meet.decor import Decor from swap_meet.electronics import Electronics -@pytest.mark.skip +# @pytest.mark.skip def test_get_items_by_category(): item_a = Clothing() item_b = Electronics() @@ -22,7 +22,7 @@ def test_get_items_by_category(): assert item_a in items assert item_c in items -@pytest.mark.skip +# @pytest.mark.skip def test_get_no_matching_items_by_category(): item_a = Clothing() item_b = Item() @@ -33,7 +33,8 @@ def test_get_no_matching_items_by_category(): items = vendor.get_by_category("Electronics") - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") + assert items == [] # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* From f41c81985a060c3efcffec39044f8866d2c8b5d7 Mon Sep 17 00:00:00 2001 From: April Zhang Date: Wed, 5 Apr 2023 01:25:35 -0700 Subject: [PATCH 10/21] complete get_best_by_category method without return none part --- swap_meet/vendor.py | 7 ++++++- tests/unit_tests/test_wave_06.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 7719b806d..71fc62706 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -53,4 +53,9 @@ def get_by_category(self, category): return category_items def get_best_by_category(self, category): - pass \ No newline at end of file + highest_con = 0 + for item in self.inventory: + if item.condition >= highest_con and item.get_category() == category: + highest_con = item.condition + best_item = item + return best_item \ No newline at end of file diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index e99cbd256..d83d944fd 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -39,7 +39,7 @@ def test_get_no_matching_items_by_category(): # ****** Complete Assert Portion of this test ********** # ********************************************************************* -@pytest.mark.skip +# @pytest.mark.skip def test_best_by_category(): item_a = Clothing(condition=2.0) item_b = Decor(condition=2.0) From 590aa94d6b393e17a69e2f6bfd8f33fe8e95f0a4 Mon Sep 17 00:00:00 2001 From: April Zhang Date: Wed, 5 Apr 2023 12:01:25 -0700 Subject: [PATCH 11/21] update some notes --- swap_meet/clothing.py | 2 +- swap_meet/decor.py | 2 +- swap_meet/electronics.py | 6 +++++- swap_meet/item.py | 2 +- swap_meet/vendor.py | 18 ++++++++++++------ tests/unit_tests/test_wave_06.py | 8 +++++--- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index de984efb4..1fbf1b1c8 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,7 +1,7 @@ import uuid from swap_meet.item import Item class Clothing(Item): - def __init__(self, id=uuid.uuid4().int, fabric="Unknown", condition=0): + def __init__(self, id=None, fabric="Unknown", condition=0): super().__init__(id, condition) self.fabric = fabric diff --git a/swap_meet/decor.py b/swap_meet/decor.py index 254b41b96..7aa7b6268 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,7 +1,7 @@ import uuid from swap_meet.item import Item class Decor(Item): - def __init__(self, id = uuid.uuid4().int, width=0, length=0, condition=0): + def __init__(self, id=None, width=0, length=0, condition=0): super().__init__(id, condition) self.width = width self.length = length diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 9c17ccf07..f6d210da1 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,7 +1,11 @@ import uuid from swap_meet.item import Item class Electronics(Item): - def __init__(self, id=uuid.uuid4().int, type="Unknown", condition=0): + def __init__(self, id=None, type="Unknown", condition=0): + # Notes: default arguments need to go to the end of this line. + # Do we need to set id=uuid or id= None again if we inherit id from our parent class alreay? + # why we need to set default to condition=0 again too? + # will id=None override our manually set id? super().__init__(id, condition) self.type = type diff --git a/swap_meet/item.py b/swap_meet/item.py index 23ba35852..c1a54bb61 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -12,7 +12,7 @@ def get_category(self): return self.__class__.__name__ def __str__(self): - return f"An object of type {self.__class__.__name__} with id {self.id}." + return f"An object of type {self.get_category()} with id {self.id}." def condition_description(self): if self.condition == 5: diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 71fc62706..b26923788 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -53,9 +53,15 @@ def get_by_category(self, category): return category_items def get_best_by_category(self, category): - highest_con = 0 - for item in self.inventory: - if item.condition >= highest_con and item.get_category() == category: - highest_con = item.condition - best_item = item - return best_item \ No newline at end of file + if self.get_by_category(category) == []: + return None + else: + highest_con = 0 + for item in self.inventory: + if item.condition >= highest_con and item.get_category() == category: + highest_con = item.condition + best_item = item + return best_item + + def swap_best_by_category(self): + pass \ No newline at end of file diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index d83d944fd..3859ab65a 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -55,7 +55,7 @@ def test_best_by_category(): assert best_item.get_category() == "Clothing" assert best_item.condition == pytest.approx(4.0) -@pytest.mark.skip +# @pytest.mark.skip def test_best_by_category_no_matches_is_none(): item_a = Decor(condition=2.0) item_b = Decor(condition=2.0) @@ -68,7 +68,7 @@ def test_best_by_category_no_matches_is_none(): assert best_item is None -@pytest.mark.skip +# @pytest.mark.skip def test_best_by_category_with_duplicates(): # Arrange item_a = Clothing(condition=2.0) @@ -110,7 +110,9 @@ def test_swap_best_by_category(): my_priority="Clothing", their_priority="Decor" ) - + # Assert + # assert result == + raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** From f1a51b41bc0984c98b136200cefc4818b6726946 Mon Sep 17 00:00:00 2001 From: April Zhang Date: Wed, 5 Apr 2023 14:55:37 -0700 Subject: [PATCH 12/21] complete swap_best_by_category method in Vendor & passed unit tests --- swap_meet/vendor.py | 13 ++++++- tests/unit_tests/test_wave_06.py | 63 ++++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index b26923788..c9b048581 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -63,5 +63,14 @@ def get_best_by_category(self, category): best_item = item return best_item - def swap_best_by_category(self): - pass \ No newline at end of file + def swap_best_by_category(self, other_vendor, my_priority, their_priority): + # my_priority = a category that vendor wants to recevie + + # other_vendor.get_by_category(my_priority) = items the V wants to recevie from other_V + my_item = self.get_best_by_category(their_priority) + their_item = other_vendor.get_best_by_category(my_priority) + if not my_item or not their_item: + return False + else: + self.swap_items(other_vendor, my_item, their_item) + return True \ No newline at end of file diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index 3859ab65a..17017cfea 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -85,7 +85,7 @@ def test_best_by_category_with_duplicates(): assert best_item.get_category() == "Clothing" assert best_item.condition == pytest.approx(4.0) -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category(): # Arrange # me @@ -111,9 +111,16 @@ def test_swap_best_by_category(): their_priority="Decor" ) # Assert - # assert result == - - raise Exception("Complete this test according to comments below.") + assert result != False + assert len(jesse.inventory) == 3 + assert len(tai.inventory) == 3 + assert item_a in tai.inventory + assert item_f in tai.inventory + assert item_b in tai.inventory + assert item_d in jesse.inventory + assert item_c in jesse.inventory + assert item_e in jesse.inventory + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* @@ -122,7 +129,7 @@ def test_swap_best_by_category(): # - That tai and jesse's inventories are the correct length # - That all the correct items are in tai and jesse's inventories, including the items which were swapped from one vendor to the other -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_reordered(): # Arrange item_a = Decor(condition=2.0) @@ -146,7 +153,17 @@ def test_swap_best_by_category_reordered(): their_priority="Decor" ) - raise Exception("Complete this test according to comments below.") + # Assert + assert result != False + assert len(jesse.inventory) == 3 + assert len(tai.inventory) == 3 + assert item_a in tai.inventory + assert item_f in tai.inventory + assert item_b in tai.inventory + assert item_d in jesse.inventory + assert item_c in jesse.inventory + assert item_e in jesse.inventory + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* @@ -155,7 +172,7 @@ def test_swap_best_by_category_reordered(): # - That tai and jesse's inventories are the correct length # - That all the correct items are in tai and jesse's inventories, and that the items that were swapped are not there -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_no_inventory_is_false(): tai = Vendor( inventory=[] @@ -181,7 +198,7 @@ def test_swap_best_by_category_no_inventory_is_false(): assert item_b in jesse.inventory assert item_c in jesse.inventory -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_no_other_inventory_is_false(): item_a = Clothing(condition=2.0) item_b = Decor(condition=4.0) @@ -207,7 +224,7 @@ def test_swap_best_by_category_no_other_inventory_is_false(): assert item_b in tai.inventory assert item_c in tai.inventory -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_no_match_is_false(): # Arrange item_a = Decor(condition=2.0) @@ -231,7 +248,18 @@ def test_swap_best_by_category_no_match_is_false(): their_priority="Clothing" ) - raise Exception("Complete this test according to comments below.") + # Assert + assert result == False + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + assert item_a in tai.inventory + assert item_b in tai.inventory + assert item_c in tai.inventory + assert item_d in jesse.inventory + assert item_e in jesse.inventory + assert item_f in jesse.inventory + + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* @@ -240,7 +268,7 @@ def test_swap_best_by_category_no_match_is_false(): # - That tai and jesse's inventories are the correct length # - That all the correct items are in tai and jesse's inventories -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_no_other_match_is_false(): # Arrange item_a = Decor(condition=2.0) @@ -264,7 +292,18 @@ def test_swap_best_by_category_no_other_match_is_false(): their_priority="Decor" ) - raise Exception("Complete this test according to comments below.") + # Assert + assert result == False + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + assert item_a in tai.inventory + assert item_b in tai.inventory + assert item_c in tai.inventory + assert item_d in jesse.inventory + assert item_e in jesse.inventory + assert item_f in jesse.inventory + + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* From 3cd0f99eff0b6545b3052a9c3167ed2acd097f8f Mon Sep 17 00:00:00 2001 From: April Zhang Date: Wed, 5 Apr 2023 14:58:34 -0700 Subject: [PATCH 13/21] passed all test --- tests/integration_tests/test_wave_01_02_03.py | 2 +- tests/integration_tests/test_wave_04_05_06.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration_tests/test_wave_01_02_03.py b/tests/integration_tests/test_wave_01_02_03.py index 0e521771a..92961d11c 100644 --- a/tests/integration_tests/test_wave_01_02_03.py +++ b/tests/integration_tests/test_wave_01_02_03.py @@ -2,7 +2,7 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip +# @pytest.mark.skip @pytest.mark.integration_test def test_integration_wave_01_02_03(): # make a vendor diff --git a/tests/integration_tests/test_wave_04_05_06.py b/tests/integration_tests/test_wave_04_05_06.py index c5953b7f8..6cb02929a 100644 --- a/tests/integration_tests/test_wave_04_05_06.py +++ b/tests/integration_tests/test_wave_04_05_06.py @@ -4,7 +4,7 @@ from swap_meet.decor import Decor from swap_meet.electronics import Electronics -@pytest.mark.skip +# @pytest.mark.skip @pytest.mark.integration_test def test_integration_wave_04_05_06(): camila = Vendor() From bd89d60affd8349b301e03c9647baa18413741da Mon Sep 17 00:00:00 2001 From: April Zhang Date: Thu, 6 Apr 2023 07:29:25 -0700 Subject: [PATCH 14/21] Added docstrings and notes --- swap_meet/clothing.py | 3 ++ swap_meet/decor.py | 8 ++--- swap_meet/electronics.py | 7 ++--- swap_meet/item.py | 17 ++++++++-- swap_meet/vendor.py | 68 +++++++++++++++++++++++++++++++++------- 5 files changed, 81 insertions(+), 22 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index 1fbf1b1c8..c427463e0 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,6 +1,9 @@ import uuid from swap_meet.item import Item class Clothing(Item): + """ + A subclass of Item class, representing clothing item. + """ def __init__(self, id=None, fabric="Unknown", condition=0): super().__init__(id, condition) self.fabric = fabric diff --git a/swap_meet/decor.py b/swap_meet/decor.py index 7aa7b6268..c59c05833 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,13 +1,13 @@ import uuid from swap_meet.item import Item class Decor(Item): + """ + A subclass of Item class, representing Decor item. + """ def __init__(self, id=None, width=0, length=0, condition=0): super().__init__(id, condition) self.width = width self.length = length - - # def get_category(self): - # return "Decor" - + def __str__(self): return super().__str__() + f" It takes up a {self.width} by {self.length} sized space." \ No newline at end of file diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index f6d210da1..5e0dbc854 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,11 +1,10 @@ import uuid from swap_meet.item import Item class Electronics(Item): + """ + A subclass of Item class, representing electronic item. + """ def __init__(self, id=None, type="Unknown", condition=0): - # Notes: default arguments need to go to the end of this line. - # Do we need to set id=uuid or id= None again if we inherit id from our parent class alreay? - # why we need to set default to condition=0 again too? - # will id=None override our manually set id? super().__init__(id, condition) self.type = type diff --git a/swap_meet/item.py b/swap_meet/item.py index c1a54bb61..dc087754f 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,20 +1,31 @@ import uuid class Item: - + """ + A class that represents item. + Each Item have an attribute named id, which is a quique integer as default; an attribute named condition, which is an integer represent the item's condition, default value is 0 (the poorest). + """ def __init__(self, id=None, condition=0): + # when manually set id. if id: self.id = id + # when id is not provided, generate an id with integer. else: self.id = uuid.uuid4().int self.condition = condition - - def get_category(self): + + def get_category(self): + """ + Return the class name as a string. + """ return self.__class__.__name__ def __str__(self): return f"An object of type {self.get_category()} with id {self.id}." def condition_description(self): + """ + Return funny description based on the item's condition. + """ if self.condition == 5: return "This item is in tip-top shape!" elif self.condition == 4: diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index c9b048581..a36674eda 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,27 +1,54 @@ class Vendor: + """ + A class that represents Vendor. + Each Vendor has an attribute named inventory, which is an empty list by default. + """ def __init__(self, inventory=None): if inventory is None: inventory = [] self.inventory = inventory def add(self, item): + """ + Add item to inventory, return item + """ self.inventory.append(item) return item def remove(self, item): + """ + Remove the matching item in the inventory, return the item. + If no matching item, return False. + """ if item in self.inventory: self.inventory.remove(item) return item else: - return False # IF raise exception it will stop the whole program, for example it will not be necessery to have line 26-28 to return F + return False def get_by_id(self, id): + """ + Return the irem with a matching id from the inventory. + If no matching item return None. + + Args: + id (integer): Item's id. + """ for item in self.inventory: if id == item.id: return item return None def swap_items(self, other_vendor, my_item, their_item): + """ + Swap this Vendor's item with the other Vendor's item, return Ture. + If either Vendor does not have a matching item, return False. + + Args: + other_vendor (an instance of Vendor) + my_item (an instance of Item): representing the item this Vendor instance plans to give. + their_item (an instence of Item): representing the item the other Vendor plans to give. + """ if my_item not in self.inventory or their_item not in other_vendor.inventory: return False else: @@ -32,20 +59,26 @@ def swap_items(self, other_vendor, my_item, their_item): return True def swap_first_item(self, other_vendor): - + """ + Swap the first item in this Vendor with the first irem in the other Vendor, return True. + If either Vendor does not have a matching item, return False. + """ if self.inventory == [] or other_vendor.inventory == []: - # Empty list is not equal to default None. Because line 5 `[]` is passed to inventory. return False else: my_first = self.inventory[0] - friend_first = other_vendor.inventory[0] - self.remove(my_first) - other_vendor.add(my_first) - other_vendor.remove(friend_first) - self.add(friend_first) + their_first = other_vendor.inventory[0] + self.swap_items(other_vendor, my_first, their_first) return True def get_by_category(self, category): + """ + Returns a list of objects in the inventory with that category. + If no matching item, return empty list. + + Args: + category (string): representing a category. + """ category_items = [] for item in self.inventory: if item.get_category() == category: @@ -53,6 +86,13 @@ def get_by_category(self, category): return category_items def get_best_by_category(self, category): + """ + Return the item with the best condition in a certain category. + If no matching item, return None. + + Args: + category (string): representing a category. + """ if self.get_by_category(category) == []: return None else: @@ -64,9 +104,15 @@ def get_best_by_category(self, category): return best_item def swap_best_by_category(self, other_vendor, my_priority, their_priority): - # my_priority = a category that vendor wants to recevie - - # other_vendor.get_by_category(my_priority) = items the V wants to recevie from other_V + """ + Swap the best item in this Vendor that matches their_priority category with the best item in the other Vendor that matches my_priority, return True. + If either Vendor has no matching item, return False. + + Args: + other_vendor (an instence of Vendor): _description_ + my_priority (string): epresents a category that this Vendor wants to receive. + their_priority (string): epresents a category that the other Vendor wants to receive. + """ my_item = self.get_best_by_category(their_priority) their_item = other_vendor.get_best_by_category(my_priority) if not my_item or not their_item: From be5fad26333b787bf8b027dfa92d644e03978258 Mon Sep 17 00:00:00 2001 From: April Zhang Date: Thu, 6 Apr 2023 14:42:00 -0700 Subject: [PATCH 15/21] refactored the get_best_by_category in Vendor --- swap_meet/vendor.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index a36674eda..c82a47722 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -96,12 +96,13 @@ def get_best_by_category(self, category): if self.get_by_category(category) == []: return None else: - highest_con = 0 - for item in self.inventory: - if item.condition >= highest_con and item.get_category() == category: - highest_con = item.condition - best_item = item - return best_item + return max(filter(lambda item: item.get_category() == category, self.inventory), key=lambda item: item.condition) + # highest_con = 0 + # for item in self.inventory: + # if item.condition >= highest_con and item.get_category() == category: + # highest_con = item.condition + # best_item = item + # return best_item def swap_best_by_category(self, other_vendor, my_priority, their_priority): """ From 4a23fcb6a5dda1c3ad5d2a03b73652ba5d45cc12 Mon Sep 17 00:00:00 2001 From: April Zhang Date: Thu, 6 Apr 2023 14:45:47 -0700 Subject: [PATCH 16/21] refactored the swap_first_item in Vendor --- swap_meet/vendor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index c82a47722..874550ccf 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -63,7 +63,7 @@ def swap_first_item(self, other_vendor): Swap the first item in this Vendor with the first irem in the other Vendor, return True. If either Vendor does not have a matching item, return False. """ - if self.inventory == [] or other_vendor.inventory == []: + if not self.inventory or not other_vendor.inventory: return False else: my_first = self.inventory[0] From d168067bb42ab003ee92bb72d4558e33aa8257fa Mon Sep 17 00:00:00 2001 From: April Zhang Date: Thu, 6 Apr 2023 21:10:23 -0700 Subject: [PATCH 17/21] refactored get_best_by_category --- swap_meet/vendor.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 874550ccf..bef06b9d0 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -93,17 +93,11 @@ def get_best_by_category(self, category): Args: category (string): representing a category. """ - if self.get_by_category(category) == []: + if not self.get_by_category(category): return None else: - return max(filter(lambda item: item.get_category() == category, self.inventory), key=lambda item: item.condition) - # highest_con = 0 - # for item in self.inventory: - # if item.condition >= highest_con and item.get_category() == category: - # highest_con = item.condition - # best_item = item - # return best_item - + return max(self.get_by_category(category), key=lambda item: item.condition) + def swap_best_by_category(self, other_vendor, my_priority, their_priority): """ Swap the best item in this Vendor that matches their_priority category with the best item in the other Vendor that matches my_priority, return True. From e3c26a4bfb35d17c3b6ba43300b766cd98cbff03 Mon Sep 17 00:00:00 2001 From: April Zhang Date: Thu, 6 Apr 2023 22:38:21 -0700 Subject: [PATCH 18/21] added check if id is an integer --- swap_meet/item.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index dc087754f..05af2f623 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -5,9 +5,12 @@ class Item: Each Item have an attribute named id, which is a quique integer as default; an attribute named condition, which is an integer represent the item's condition, default value is 0 (the poorest). """ def __init__(self, id=None, condition=0): - # when manually set id. + # when manually set id, check if it's an integer, if not: raise TypeError. if id: - self.id = id + if isinstance(id, int): + self.id = id + else: + raise TypeError("Id must be an integer.") # when id is not provided, generate an id with integer. else: self.id = uuid.uuid4().int @@ -31,7 +34,7 @@ def condition_description(self): elif self.condition == 4: return "This item still has plenty of life left and character to spare." elif self.condition == 3: - return "It's not down for the count - it's still got some fight left in it!" + return "It's not down for the count - it's still got some fight left in it!" elif self.condition == 2: return "Don't judge this item by its appearence. It's still got some tricks up its sleeve and plenty of use left." elif self.condition == 1: From de8843f0bfcc76d19b41f0d13866003e0ebcd22a Mon Sep 17 00:00:00 2001 From: April Zhang Date: Thu, 6 Apr 2023 22:44:39 -0700 Subject: [PATCH 19/21] added error handling for condition>5 raise ValueError --- swap_meet/item.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index 05af2f623..0c7fd9663 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -14,7 +14,11 @@ def __init__(self, id=None, condition=0): # when id is not provided, generate an id with integer. else: self.id = uuid.uuid4().int - self.condition = condition + + if condition > 5: + raise ValueError("Condition value range from 0 to 5.") + else: + self.condition = condition def get_category(self): """ From e37b3222ce386dcece69737606dfb1cfb7b0122e Mon Sep 17 00:00:00 2001 From: April Zhang Date: Thu, 6 Apr 2023 22:49:48 -0700 Subject: [PATCH 20/21] added an attribute 'age' to all Items --- swap_meet/clothing.py | 4 ++-- swap_meet/decor.py | 4 ++-- swap_meet/electronics.py | 4 ++-- swap_meet/item.py | 11 ++++++++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index c427463e0..7a38bca8c 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -4,8 +4,8 @@ class Clothing(Item): """ A subclass of Item class, representing clothing item. """ - def __init__(self, id=None, fabric="Unknown", condition=0): - super().__init__(id, condition) + def __init__(self, id=None, fabric="Unknown", condition=0, age=0): + super().__init__(id, condition, age) self.fabric = fabric def __str__(self): diff --git a/swap_meet/decor.py b/swap_meet/decor.py index c59c05833..c5947339f 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -4,8 +4,8 @@ class Decor(Item): """ A subclass of Item class, representing Decor item. """ - def __init__(self, id=None, width=0, length=0, condition=0): - super().__init__(id, condition) + def __init__(self, id=None, width=0, length=0, condition=0, age=0): + super().__init__(id, condition, age) self.width = width self.length = length diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 5e0dbc854..9108969f0 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -4,8 +4,8 @@ class Electronics(Item): """ A subclass of Item class, representing electronic item. """ - def __init__(self, id=None, type="Unknown", condition=0): - super().__init__(id, condition) + def __init__(self, id=None, type="Unknown", condition=0, age=0): + super().__init__(id, condition, age) self.type = type def __str__(self): diff --git a/swap_meet/item.py b/swap_meet/item.py index 0c7fd9663..bacf814e0 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -2,9 +2,12 @@ class Item: """ A class that represents item. - Each Item have an attribute named id, which is a quique integer as default; an attribute named condition, which is an integer represent the item's condition, default value is 0 (the poorest). + Each Item have: + an attribute named id, which is a quique integer as default; + an attribute named condition, which is an integer representing the item's condition, default value is 0 (the poorest); + an attribute named age, which is an integer representing the item's age, default value is 0. """ - def __init__(self, id=None, condition=0): + def __init__(self, id=None, condition=0, age=0): # when manually set id, check if it's an integer, if not: raise TypeError. if id: if isinstance(id, int): @@ -14,11 +17,13 @@ def __init__(self, id=None, condition=0): # when id is not provided, generate an id with integer. else: self.id = uuid.uuid4().int - + # If condition out of range, raise ValueError. if condition > 5: raise ValueError("Condition value range from 0 to 5.") else: self.condition = condition + + self.age = age def get_category(self): """ From f516d058bdd03b1173aa047fca236c16f617fa5e Mon Sep 17 00:00:00 2001 From: April Zhang Date: Thu, 6 Apr 2023 23:28:36 -0700 Subject: [PATCH 21/21] added swap_by_newst function and its unit tests --- swap_meet/item.py | 4 +-- swap_meet/vendor.py | 27 ++++++++++++++------ tests/unit_tests/test_wave_06.py | 42 ++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index bacf814e0..16da951bd 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -3,7 +3,7 @@ class Item: """ A class that represents item. Each Item have: - an attribute named id, which is a quique integer as default; + an attribute named id, which is an unique integer as default; an attribute named condition, which is an integer representing the item's condition, default value is 0 (the poorest); an attribute named age, which is an integer representing the item's age, default value is 0. """ @@ -45,7 +45,7 @@ def condition_description(self): elif self.condition == 3: return "It's not down for the count - it's still got some fight left in it!" elif self.condition == 2: - return "Don't judge this item by its appearence. It's still got some tricks up its sleeve and plenty of use left." + return "Don't judge this item by its appearance. It's still got some tricks up its sleeve and plenty of use left." elif self.condition == 1: return "This item may not have much life left in it, but it's not quite ready to give up the ghost yet." else: diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index bef06b9d0..3eb318a51 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -28,7 +28,7 @@ def remove(self, item): def get_by_id(self, id): """ - Return the irem with a matching id from the inventory. + Return the item with a matching id from the inventory. If no matching item return None. Args: @@ -41,13 +41,13 @@ def get_by_id(self, id): def swap_items(self, other_vendor, my_item, their_item): """ - Swap this Vendor's item with the other Vendor's item, return Ture. + Swap this Vendor's item with the other Vendor's item, return True. If either Vendor does not have a matching item, return False. Args: other_vendor (an instance of Vendor) my_item (an instance of Item): representing the item this Vendor instance plans to give. - their_item (an instence of Item): representing the item the other Vendor plans to give. + their_item (an instance of Item): representing the item the other Vendor plans to give. """ if my_item not in self.inventory or their_item not in other_vendor.inventory: return False @@ -60,7 +60,7 @@ def swap_items(self, other_vendor, my_item, their_item): def swap_first_item(self, other_vendor): """ - Swap the first item in this Vendor with the first irem in the other Vendor, return True. + Swap the first item in this Vendor with the first item in the other Vendor, return True. If either Vendor does not have a matching item, return False. """ if not self.inventory or not other_vendor.inventory: @@ -104,9 +104,9 @@ def swap_best_by_category(self, other_vendor, my_priority, their_priority): If either Vendor has no matching item, return False. Args: - other_vendor (an instence of Vendor): _description_ - my_priority (string): epresents a category that this Vendor wants to receive. - their_priority (string): epresents a category that the other Vendor wants to receive. + other_vendor (an instance of Vendor): _description_ + my_priority (string): represents a category that this Vendor wants to receive. + their_priority (string): represents a category that the other Vendor wants to receive. """ my_item = self.get_best_by_category(their_priority) their_item = other_vendor.get_best_by_category(my_priority) @@ -114,4 +114,17 @@ def swap_best_by_category(self, other_vendor, my_priority, their_priority): return False else: self.swap_items(other_vendor, my_item, their_item) + return True + + def swap_by_newest(self, other_vendor): + """ + Swap the newest item in this Vendor with the newest item in the other Vendor, return True. + If either Vendor does not have a matching item, return False. + """ + my_newest = min(self.inventory, key=lambda item: item.age, default=False) + their_newest = min(other_vendor.inventory, key=lambda item: item.age, default=False) + if not my_newest or not their_newest: + return False + else: + self.swap_items(other_vendor, my_newest, their_newest) return True \ No newline at end of file diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index 17017cfea..d44e0a388 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -311,3 +311,45 @@ def test_swap_best_by_category_no_other_match_is_false(): # - That result is falsy # - That tai and jesse's inventories are the correct length # - That all the correct items are in tai and jesse's inventories + + +# ~~~~~ swap_by_newest Function Tests ~~~~~ +def test_swap_by_newest_no_item_return_false(): + # Arrange + a = Vendor() + b = Vendor() + # Act + result = a.swap_by_newest(b) + # Assert + assert result == False + +def test_swap_by_newest(): + # Arrange + # me + item_a = Decor(age=2) + item_b = Clothing(age=3) + item_c = Decor(age=0) + a = Vendor( + inventory=[item_a, item_b, item_c] + ) + # them + item_d = Clothing(age=0) + item_e = Decor(age=4) + item_f = Clothing(age=3) + b = Vendor( + inventory=[item_d, item_e, item_f] + ) + + # Act + result = a.swap_by_newest(b) + + # Assert + assert result == True + assert len(a.inventory) == 3 + assert len(b.inventory) == 3 + assert item_a in a.inventory + assert item_b in a.inventory + assert item_d in a.inventory + assert item_c in b.inventory + assert item_e in b.inventory + assert item_f in b.inventory \ No newline at end of file