From 47dc35b52991a4dae8c5ec0bfade3f996a51c111 Mon Sep 17 00:00:00 2001 From: cathos Date: Tue, 5 Apr 2022 17:47:30 -0700 Subject: [PATCH 1/5] finished waves 1 and 2, through test 2 in wave 3 --- swap_meet/item.py | 6 ++++- swap_meet/vendor.py | 39 +++++++++++++++++++++++++++++++- tests/unit_tests/test_wave_01.py | 13 ++++++----- tests/unit_tests/test_wave_02.py | 10 ++++---- tests/unit_tests/test_wave_03.py | 12 +++++----- 5 files changed, 62 insertions(+), 18 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index 560d759c2..7cc7c3670 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,2 +1,6 @@ class Item: - pass \ No newline at end of file + def __init__ (self, category = ""): + self.category = category + + def __str__(self): + return "Hello World!" \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 87302c056..759058c23 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,2 +1,39 @@ class Vendor: - pass \ No newline at end of file + 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 + ''' + try: + vendor.add(my_item) + self.remove(my_item) + self.add(their_item) + vendor.remove(their_item) + return True + except: + return False diff --git a/tests/unit_tests/test_wave_01.py b/tests/unit_tests/test_wave_01.py index 58478ccf9..b7208d659 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,8 @@ 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.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* + assert result == False diff --git a/tests/unit_tests/test_wave_02.py b/tests/unit_tests/test_wave_02.py index 3d7060d7c..88d54b6ea 100644 --- a/tests/unit_tests/test_wave_02.py +++ b/tests/unit_tests/test_wave_02.py @@ -2,12 +2,12 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip +# @pytest.mark.skip def test_items_have_blank_default_category(): item = Item() assert item.category == "" -@pytest.mark.skip +# @pytest.mark.skip def test_get_items_by_category(): item_a = Item(category="clothing") item_b = Item(category="electronics") @@ -23,7 +23,7 @@ def test_get_items_by_category(): assert item_c in items assert item_b not in items -@pytest.mark.skip +# @pytest.mark.skip def test_get_no_matching_items_by_category(): item_a = Item(category="clothing") item_b = Item(category="clothing") @@ -34,7 +34,9 @@ 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.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* + + assert len(items) == 0 \ 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 0300b638f..2d573fa52 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(): item = Item() @@ -10,7 +10,7 @@ def test_item_overrides_to_string(): assert stringified_item == "Hello World!" -@pytest.mark.skip +# @pytest.mark.skip def test_swap_items_returns_true(): item_a = Item(category="clothing") item_b = Item(category="clothing") @@ -38,7 +38,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(category="clothing") item_b = Item(category="clothing") @@ -65,7 +65,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(category="clothing") item_b = Item(category="clothing") @@ -92,7 +92,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=[] @@ -112,7 +112,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(category="clothing") item_b = Item(category="clothing") From f4fc49ba87430987096e653d82b43b67c7372e6c Mon Sep 17 00:00:00 2001 From: cathos Date: Tue, 5 Apr 2022 21:22:01 -0700 Subject: [PATCH 2/5] finished through wave 4 --- swap_meet/vendor.py | 15 +++++++++++++++ tests/unit_tests/test_wave_04.py | 6 +++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 759058c23..86bebbb93 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -30,6 +30,8 @@ def swap_items(self, vendor, my_item, their_item): remove their item from their inventory ''' try: + if (my_item not in self.inventory) or (their_item not in vendor.inventory): + raise vendor.add(my_item) self.remove(my_item) self.add(their_item) @@ -37,3 +39,16 @@ def swap_items(self, vendor, my_item, their_item): 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) + return True + except: + return False \ 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 8190a4ebb..4ef21ff8e 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(category="clothing") item_b = Item(category="clothing") @@ -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(category="clothing") item_b = Item(category="clothing") From 90ffa5bf75917316c88e9652c4cc736a2280d2e0 Mon Sep 17 00:00:00 2001 From: cathos Date: Wed, 6 Apr 2022 17:39:57 -0700 Subject: [PATCH 3/5] finished, passed all tests --- main.py | 3 ++ swap_meet/clothing.py | 8 +++- swap_meet/decor.py | 8 +++- swap_meet/electronics.py | 8 +++- swap_meet/item.py | 24 +++++++++- swap_meet/vendor.py | 33 ++++++++++++- tests/integration_tests/test_wave_01_02_03.py | 2 +- tests/integration_tests/test_wave_04_05_06.py | 2 +- tests/unit_tests/test_wave_05.py | 10 ++-- tests/unit_tests/test_wave_06.py | 47 ++++++++++++++----- 10 files changed, 116 insertions(+), 29 deletions(-) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 000000000..dba6fedb7 --- /dev/null +++ b/main.py @@ -0,0 +1,3 @@ +from swap_meet.vendor import Vendor +from swap_meet.item import Item + diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index b8afdeb1e..f7fc65535 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,2 +1,6 @@ -class Clothing: - pass \ No newline at end of file +from swap_meet.item import Item, CATEGORIES, CONDITION_DESCRIPTORS + +class Clothing(Item): + def __init__(self, condition = 0): + super().__init__(category = "Clothing", condition = condition) + # self.category = "Clothing" \ No newline at end of file diff --git a/swap_meet/decor.py b/swap_meet/decor.py index eab7a9dbe..27d51e6db 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,2 +1,6 @@ -class Decor: - pass \ No newline at end of file +from swap_meet.item import Item, CATEGORIES, CONDITION_DESCRIPTORS + +class Decor(Item): + def __init__(self, condition = 0): + super().__init__(category = "Decor", condition = condition) + # self.category = "Decor" \ No newline at end of file diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 2f9dff68a..63955f162 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,2 +1,6 @@ -class Electronics: - pass +from swap_meet.item import Item, CATEGORIES, CONDITION_DESCRIPTORS + +class Electronics(Item): + def __init__(self, condition = 0): + super().__init__(category = "Electronics", condition = condition) + # self.category = "Electronics" \ No newline at end of file diff --git a/swap_meet/item.py b/swap_meet/item.py index 7cc7c3670..cbf22b76a 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,6 +1,26 @@ +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." +} + class Item: - def __init__ (self, category = ""): + def __init__ (self, category = "", condition = 0): self.category = category + self.condition = condition def __str__(self): - return "Hello World!" \ No newline at end of file + return CATEGORIES[self.category] + + def condition_description(self): + return CONDITION_DESCRIPTORS[int(self.condition)] \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 86bebbb93..0425fdf6c 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -51,4 +51,35 @@ def swap_first_item(self, vendor): self.swap_items(vendor, my_item, their_item) return True except: - return False \ No newline at end of file + return False + + def get_best_by_category(self, category): + ''' + 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) + # if len(items) > 0: + 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) + print(f"my item: {my_item}") + their_item = other.get_best_by_category(my_priority) + print(f"their item: {their_item}") + + if None in (my_item, their_item): + return False + # else: + self.swap_items(other, my_item, their_item) + return True \ No newline at end of file diff --git a/tests/integration_tests/test_wave_01_02_03.py b/tests/integration_tests/test_wave_01_02_03.py index 9912414da..25707d998 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 4d0be9909..91b1362b6 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() diff --git a/tests/unit_tests/test_wave_05.py b/tests/unit_tests/test_wave_05.py index 7abea06cd..fdeb063a9 100644 --- a/tests/unit_tests/test_wave_05.py +++ b/tests/unit_tests/test_wave_05.py @@ -3,25 +3,25 @@ from swap_meet.decor import Decor from swap_meet.electronics import Electronics -@pytest.mark.skip +# @pytest.mark.skip def test_clothing_has_default_category_and_to_str(): cloth = Clothing() assert cloth.category == "Clothing" assert str(cloth) == "The finest clothing you could wear." -@pytest.mark.skip +# @pytest.mark.skip def test_decor_has_default_category_and_to_str(): decor = Decor() assert decor.category == "Decor" assert str(decor) == "Something to decorate your space." -@pytest.mark.skip +# @pytest.mark.skip def test_electronics_has_default_category_and_to_str(): electronics = Electronics() assert electronics.category == "Electronics" assert str(electronics) == "A gadget full of buttons and secrets." -@pytest.mark.skip +# @pytest.mark.skip def test_items_have_condition_as_float(): items = [ Clothing(condition=3.5), @@ -31,7 +31,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), diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index 1f7065ab4..c5056368d 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -4,7 +4,7 @@ from swap_meet.decor import Decor from swap_meet.electronics import Electronics -@pytest.mark.skip +# @pytest.mark.skip def test_best_by_category(): item_a = Clothing(condition=2.0) item_b = Decor(condition=2.0) @@ -20,7 +20,7 @@ def test_best_by_category(): assert best_item.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) @@ -33,7 +33,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) @@ -50,7 +50,7 @@ def test_best_by_category_with_duplicates(): assert best_item.category == "Clothing" assert best_item.condition == pytest.approx(4.0) -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category(): # Arrange # me @@ -76,7 +76,7 @@ def test_swap_best_by_category(): their_priority="Decor" ) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* @@ -84,8 +84,13 @@ def test_swap_best_by_category(): # - That the results is truthy # - 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 + assert result == True + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + assert item_a and item_b and item_f in tai.inventory + assert item_c and item_d and item_e in jesse.inventory -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_reordered(): # Arrange item_a = Decor(condition=2.0) @@ -109,7 +114,7 @@ def test_swap_best_by_category_reordered(): their_priority="Decor" ) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* @@ -117,8 +122,14 @@ def test_swap_best_by_category_reordered(): # - That result is truthy # - 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 + assert result == True + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + assert item_a and item_b and item_f in tai.inventory + assert item_c and item_d and item_e in jesse.inventory + -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_no_inventory_is_false(): tai = Vendor( inventory=[] @@ -144,7 +155,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) @@ -170,7 +181,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) @@ -194,7 +205,7 @@ def test_swap_best_by_category_no_match_is_false(): their_priority="Clothing" ) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* @@ -202,8 +213,13 @@ def test_swap_best_by_category_no_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 + assert result == False + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + assert item_a and item_b and item_c in tai.inventory + assert item_f and item_d and item_e in jesse.inventory -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_no_other_match_is_false(): # Arrange item_a = Decor(condition=2.0) @@ -227,7 +243,7 @@ def test_swap_best_by_category_no_other_match_is_false(): their_priority="Decor" ) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* @@ -235,3 +251,8 @@ 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 + assert result == False + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + assert item_a and item_b and item_c in tai.inventory + assert item_f and item_d and item_e in jesse.inventory From 0999a13773ffa72e5d63fd5fb2dd5833b635f5db Mon Sep 17 00:00:00 2001 From: cathos Date: Thu, 7 Apr 2022 17:28:02 -0700 Subject: [PATCH 4/5] changed implementation of swap_items and moved global variables in item.py into the Item class --- swap_meet/clothing.py | 3 ++- swap_meet/decor.py | 3 ++- swap_meet/electronics.py | 3 ++- swap_meet/item.py | 51 ++++++++++++++++++++++++++-------------- swap_meet/vendor.py | 26 +++++++++++++------- 5 files changed, 58 insertions(+), 28 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index f7fc65535..5a2e7a3fa 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,4 +1,5 @@ -from swap_meet.item import Item, CATEGORIES, CONDITION_DESCRIPTORS +from swap_meet.item import Item +# , CATEGORIES, CONDITION_DESCRIPTORS class Clothing(Item): def __init__(self, condition = 0): diff --git a/swap_meet/decor.py b/swap_meet/decor.py index 27d51e6db..dc0238f94 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,4 +1,5 @@ -from swap_meet.item import Item, CATEGORIES, CONDITION_DESCRIPTORS +from swap_meet.item import Item +# , CATEGORIES, CONDITION_DESCRIPTORS class Decor(Item): def __init__(self, condition = 0): diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 63955f162..717965baf 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,4 +1,5 @@ -from swap_meet.item import Item, CATEGORIES, CONDITION_DESCRIPTORS +from swap_meet.item import Item +# , CATEGORIES, CONDITION_DESCRIPTORS class Electronics(Item): def __init__(self, condition = 0): diff --git a/swap_meet/item.py b/swap_meet/item.py index cbf22b76a..75f103324 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,26 +1,43 @@ -CATEGORIES = { - "": "Hello World!", - "Clothing": "The finest clothing you could wear.", - "Decor": "Something to decorate your space.", - "Electronics": "A gadget full of buttons and secrets.", -} +## Attempting to define these below in Item instead of globally. +# 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." -} +# 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.", +# } class Item: - def __init__ (self, category = "", condition = 0): + def __init__(self, category = "", condition = 0): self.category = category self.condition = condition + 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.", + } + def __str__(self): - return CATEGORIES[self.category] + return Item.CATEGORIES[self.category] def condition_description(self): - return CONDITION_DESCRIPTORS[int(self.condition)] \ No newline at end of file + return Item.CONDITION_DESCRIPTORS[int(self.condition)] \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 0425fdf6c..56db722be 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -28,18 +28,30 @@ def swap_items(self, vendor, my_item, their_item): remove my item from my inventory add their item to my inventory remove their item from their inventory + + trying this using tuple swap instead: ''' try: - if (my_item not in self.inventory) or (their_item not in vendor.inventory): - raise - vendor.add(my_item) - self.remove(my_item) - self.add(their_item) - vendor.remove(their_item) + 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 return True except: return False + # Old method. perhaps the above is more efficient? + # try: + # if (my_item not in self.inventory) or (their_item not in vendor.inventory): + # raise + # vendor.add(my_item) + # self.remove(my_item) + # self.add(their_item) + # vendor.remove(their_item) + # return True + # except: + # return False + def swap_first_item(self, vendor): ''' swap the first item between my inventory and vendor's inventory. @@ -74,9 +86,7 @@ def swap_best_by_category(self, other, my_priority, their_priority): if no items are in the category in either vendor's inventory, return False ''' my_item = self.get_best_by_category(their_priority) - print(f"my item: {my_item}") their_item = other.get_best_by_category(my_priority) - print(f"their item: {their_item}") if None in (my_item, their_item): return False From 3c61ec89b245b6b7dbb30cdba6111315f49b60e9 Mon Sep 17 00:00:00 2001 From: cathos Date: Thu, 7 Apr 2022 20:40:11 -0700 Subject: [PATCH 5/5] cleaned up the code --- swap_meet/clothing.py | 4 +--- swap_meet/decor.py | 4 +--- swap_meet/electronics.py | 4 +--- swap_meet/item.py | 52 +++++++++++++++------------------------- swap_meet/vendor.py | 32 ++++++++++++------------- 5 files changed, 38 insertions(+), 58 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index 5a2e7a3fa..a303ddd3b 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,7 +1,5 @@ from swap_meet.item import Item -# , CATEGORIES, CONDITION_DESCRIPTORS class Clothing(Item): def __init__(self, condition = 0): - super().__init__(category = "Clothing", condition = condition) - # self.category = "Clothing" \ No newline at end of file + super().__init__(category = "Clothing", condition = condition) \ No newline at end of file diff --git a/swap_meet/decor.py b/swap_meet/decor.py index dc0238f94..df5acf6c2 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,7 +1,5 @@ from swap_meet.item import Item -# , CATEGORIES, CONDITION_DESCRIPTORS class Decor(Item): def __init__(self, condition = 0): - super().__init__(category = "Decor", condition = condition) - # self.category = "Decor" \ No newline at end of file + super().__init__(category = "Decor", condition = condition) \ No newline at end of file diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 717965baf..ff73875c1 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,7 +1,5 @@ from swap_meet.item import Item -# , CATEGORIES, CONDITION_DESCRIPTORS class Electronics(Item): def __init__(self, condition = 0): - super().__init__(category = "Electronics", condition = condition) - # self.category = "Electronics" \ No newline at end of file + super().__init__(category = "Electronics", condition = condition) \ No newline at end of file diff --git a/swap_meet/item.py b/swap_meet/item.py index 75f103324..975945bfd 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,43 +1,29 @@ -## Attempting to define these below in Item instead of globally. -# CATEGORIES = { -# "": "Hello World!", -# "Clothing": "The finest clothing you could wear.", -# "Decor": "Something to decorate your space.", -# "Electronics": "A gadget full of buttons and secrets.", -# } +## 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.* -# 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.", -# } +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.", +} class Item: def __init__(self, category = "", condition = 0): self.category = category self.condition = condition - 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.", - } - def __str__(self): - return Item.CATEGORIES[self.category] + return CATEGORIES[self.category] def condition_description(self): - return Item.CONDITION_DESCRIPTORS[int(self.condition)] \ No newline at end of file + return CONDITION_DESCRIPTORS[int(self.condition)] \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 56db722be..bb824760b 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -2,6 +2,7 @@ class Vendor: def __init__ (self, inventory = None): if not inventory: inventory = [] + self.inventory = inventory def add(self, item): @@ -12,14 +13,17 @@ 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): @@ -28,30 +32,24 @@ def swap_items(self, vendor, my_item, their_item): remove my item from my inventory add their item to my inventory remove their item from their inventory - - trying this using tuple swap instead: + --- + 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 return True + except: return False - # Old method. perhaps the above is more efficient? - # try: - # if (my_item not in self.inventory) or (their_item not in vendor.inventory): - # raise - # vendor.add(my_item) - # self.remove(my_item) - # self.add(their_item) - # vendor.remove(their_item) - # return True - # except: - # return False - def swap_first_item(self, vendor): ''' swap the first item between my inventory and vendor's inventory. @@ -62,6 +60,7 @@ def swap_first_item(self, vendor): their_item = vendor.inventory[0] self.swap_items(vendor, my_item, their_item) return True + except: return False @@ -73,11 +72,12 @@ def get_best_by_category(self, category): condition = -1 best_item = None items = self.get_by_category(category) - # if len(items) > 0: + 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): @@ -90,6 +90,6 @@ def swap_best_by_category(self, other, my_priority, their_priority): if None in (my_item, their_item): return False - # else: + self.swap_items(other, my_item, their_item) return True \ No newline at end of file