From ac1884d15a967ed5fdef23d52f83d29cca20ce3a Mon Sep 17 00:00:00 2001 From: Laurel Date: Mon, 5 Apr 2021 16:32:21 -0700 Subject: [PATCH 1/4] somewhere wave 5 --- swap_meet/clothing.py | 29 ++++++++++++++++ swap_meet/decor.py | 18 ++++++++++ swap_meet/electronics.py | 11 ++++++ swap_meet/item.py | 19 ++++++++++ swap_meet/vendor.py | 75 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 swap_meet/clothing.py create mode 100644 swap_meet/decor.py create mode 100644 swap_meet/electronics.py create mode 100644 swap_meet/item.py create mode 100644 swap_meet/vendor.py diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py new file mode 100644 index 000000000..43ee2b7e9 --- /dev/null +++ b/swap_meet/clothing.py @@ -0,0 +1,29 @@ +from swap_meet.item import Item + +#wave 5 +class Clothing(Item): + + def __init__(self, condition=0): + super().__init__("Clothing", condition) + + def __str__(self): + return "The finest clothing you could wear." + + + + + + + + + + + + + + + + + + + diff --git a/swap_meet/decor.py b/swap_meet/decor.py new file mode 100644 index 000000000..8905b9a48 --- /dev/null +++ b/swap_meet/decor.py @@ -0,0 +1,18 @@ +from swap_meet.item import Item + +#wave 5 +class Decor(Item): + def __init__(self): + super().__init__("Decor") + + + + + + + + + + + + diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py new file mode 100644 index 000000000..d2af7c697 --- /dev/null +++ b/swap_meet/electronics.py @@ -0,0 +1,11 @@ +from swap_meet.item import Item + +#wave 5 +class Electronics(Item): + def __init__(self): + super().__init__("Electronics") + + def __str__(self): + return "A gadget full of buttons and secrets." + + diff --git a/swap_meet/item.py b/swap_meet/item.py new file mode 100644 index 000000000..e24892ca5 --- /dev/null +++ b/swap_meet/item.py @@ -0,0 +1,19 @@ +'''The first tests in wave 2 imply: + +- There is a module (file) named `item.py` inside of the `swap_meet` package (folder) + +- Inside this module, there is a class named `Item` +- Each `Item` will have an attribute named `category`, which is an empty string by default +- When we initialize an instance of `Item`, we can optionally pass in a string with the keyword argument `category`''' + + + +class Item: + def __init__(self, category = "", condition=0): + self.category = category + self.condition = condition +#wave 3 + def __str__(self): + return "Hello World!" + + diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py new file mode 100644 index 000000000..51e182e13 --- /dev/null +++ b/swap_meet/vendor.py @@ -0,0 +1,75 @@ +#wave 1 read.me +# - There is a module (file) named `vendor.py` inside of the `swap_meet` package (folder) +'''- Inside this module, there is a class named `Vendor` +- Each `Vendor` will have an attribute named `inventory`, which is an empty list by default +- When we create initialize an instance of `Vendor`, we can optionally pass in a list with the keyword argument `inventory` + +The remaining tests in wave 1 imply: + +- Every instance of `Vendor` has an instance method named `add`, which takes in one item +- This method adds the item to the `inventory` +- This method returns the item that was added + +- Similarly, every instance of `Vendor` has an instance method named `remove`, which takes in one item +- This method removes the matching item from the `inventory` +- This method returns the item that was removed +- If there is no matching item in the `inventory`, the method should return `False`''' + +#import item to pass wave two tests +from swap_meet.item import Item + +class Vendor: + def __init__(self, inventory = None): + if inventory == None: + self.inventory = [] + else: + self.inventory = 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 + +#wave two: + def get_by_category(self, category): + matched = [] + for item in self.inventory: + if item.category == category: + matched.append(item) + return matched +#wave 3 + def swap_items(self, them, my_item, their_item): + if their_item not in them.inventory or my_item not in self.inventory: + return False + else: + self.inventory.remove(my_item) + them.inventory.append(my_item) + them.inventory.remove(their_item) + self.inventory.append(their_item) + return True +#wave 4 + def swap_first_item(self, them): + if len(self.inventory) == 0 or len(them.inventory) == 0: + return False + return self.swap_items(them, self.inventory[0], them.inventory[0]) + + + + + + + + + + + + + + + From a2d404e5a523f50c07091bb2ad9fe1fa18993f09 Mon Sep 17 00:00:00 2001 From: Laurel Date: Tue, 6 Apr 2021 20:30:00 -0700 Subject: [PATCH 2/4] all tests passing without max/lambda wave 6 --- swap_meet/clothing.py | 22 +-------------- swap_meet/decor.py | 18 +++--------- swap_meet/electronics.py | 10 +++---- swap_meet/item.py | 26 +++++++++-------- swap_meet/vendor.py | 60 ++++++++++++++++------------------------ 5 files changed, 47 insertions(+), 89 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index 43ee2b7e9..5475dfc5f 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,29 +1,9 @@ from swap_meet.item import Item -#wave 5 class Clothing(Item): def __init__(self, condition=0): super().__init__("Clothing", condition) - + def __str__(self): return "The finest clothing you could wear." - - - - - - - - - - - - - - - - - - - diff --git a/swap_meet/decor.py b/swap_meet/decor.py index 8905b9a48..5602e26f0 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,18 +1,8 @@ from swap_meet.item import Item -#wave 5 class Decor(Item): - def __init__(self): - super().__init__("Decor") - - - - - - - - - - - + def __init__(self, condition=0): + super().__init__("Decor", condition) + def __str__(self): + return "Something to decorate your space." diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index d2af7c697..0685b3683 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,11 +1,9 @@ from swap_meet.item import Item -#wave 5 class Electronics(Item): - def __init__(self): - super().__init__("Electronics") - + def __init__(self, condition=0): + super().__init__("Electronics", condition) + + def __str__(self): return "A gadget full of buttons and secrets." - - diff --git a/swap_meet/item.py b/swap_meet/item.py index e24892ca5..c3fe17cf3 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,19 +1,21 @@ -'''The first tests in wave 2 imply: - -- There is a module (file) named `item.py` inside of the `swap_meet` package (folder) - -- Inside this module, there is a class named `Item` -- Each `Item` will have an attribute named `category`, which is an empty string by default -- When we initialize an instance of `Item`, we can optionally pass in a string with the keyword argument `category`''' - - - class Item: def __init__(self, category = "", condition=0): self.category = category self.condition = condition -#wave 3 + def __str__(self): return "Hello World!" - + def condition_description(self): + if self.condition < 1: + return "I would be cautious handling this, it's really old." + elif self.condition < 2: + return "Probably has some use still left?" + elif self.condition < 3: + return "Doesn't look like much but it will get the job done." + elif self.condition < 4: + return "This has a lot of use left!" + elif self.condition < 5: + return "Really good condition!" + else: + return "It's perfect, never used, still in orginal packaging." diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 51e182e13..835a2c54c 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,23 +1,3 @@ -#wave 1 read.me -# - There is a module (file) named `vendor.py` inside of the `swap_meet` package (folder) -'''- Inside this module, there is a class named `Vendor` -- Each `Vendor` will have an attribute named `inventory`, which is an empty list by default -- When we create initialize an instance of `Vendor`, we can optionally pass in a list with the keyword argument `inventory` - -The remaining tests in wave 1 imply: - -- Every instance of `Vendor` has an instance method named `add`, which takes in one item -- This method adds the item to the `inventory` -- This method returns the item that was added - -- Similarly, every instance of `Vendor` has an instance method named `remove`, which takes in one item -- This method removes the matching item from the `inventory` -- This method returns the item that was removed -- If there is no matching item in the `inventory`, the method should return `False`''' - -#import item to pass wave two tests -from swap_meet.item import Item - class Vendor: def __init__(self, inventory = None): if inventory == None: @@ -36,14 +16,13 @@ def remove(self, item): else: return False -#wave two: def get_by_category(self, category): matched = [] for item in self.inventory: if item.category == category: matched.append(item) return matched -#wave 3 + def swap_items(self, them, my_item, their_item): if their_item not in them.inventory or my_item not in self.inventory: return False @@ -53,23 +32,32 @@ def swap_items(self, them, my_item, their_item): them.inventory.remove(their_item) self.inventory.append(their_item) return True -#wave 4 + def swap_first_item(self, them): if len(self.inventory) == 0 or len(them.inventory) == 0: return False return self.swap_items(them, self.inventory[0], them.inventory[0]) - - - + '''def my_max(items): + macs = None + for i in items: + if macs is None or i.condition > macs.condition: + macs = i + return macs''' + + def get_best_by_category(self, category): + def get_condition(item): + return item.condition + items_in_category = self.get_by_category(category) + #def my_max would go here if using not using built in max + if len(items_in_category) > 0: + #would have returned my_max(items_in_category) if not using built in max + return max(items_in_category, key= get_condition) + else: + return None - - - - - - - - - - + def swap_best_by_category(self, other, my_priority, their_priority): + my_category = self.get_best_by_category(their_priority) + their_category = other.get_best_by_category(my_priority) + total = self.swap_items(other, my_category, their_category) + return total \ No newline at end of file From ab6d854da945ba2ad4a0b82b1e5b7eee219847f6 Mon Sep 17 00:00:00 2001 From: Laurel Date: Tue, 6 Apr 2021 20:35:58 -0700 Subject: [PATCH 3/4] sleek neat one --- swap_meet/vendor.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 835a2c54c..0a1b08ab2 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -38,21 +38,10 @@ def swap_first_item(self, them): return False return self.swap_items(them, self.inventory[0], them.inventory[0]) - '''def my_max(items): - macs = None - for i in items: - if macs is None or i.condition > macs.condition: - macs = i - return macs''' - def get_best_by_category(self, category): - def get_condition(item): - return item.condition items_in_category = self.get_by_category(category) - #def my_max would go here if using not using built in max - if len(items_in_category) > 0: - #would have returned my_max(items_in_category) if not using built in max - return max(items_in_category, key= get_condition) + if len(items_in_category) > 0: + return max(items_in_category, key = lambda item: item.condition) else: return None @@ -60,4 +49,4 @@ def swap_best_by_category(self, other, my_priority, their_priority): my_category = self.get_best_by_category(their_priority) their_category = other.get_best_by_category(my_priority) total = self.swap_items(other, my_category, their_category) - return total \ No newline at end of file + return total From f7b0387462d23be35e1271106877d67e8c5e6f1b Mon Sep 17 00:00:00 2001 From: Laurel Date: Tue, 6 Apr 2021 20:47:28 -0700 Subject: [PATCH 4/4] document lambda --- swap_meet/vendor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 0a1b08ab2..b8ea13813 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -41,6 +41,7 @@ def swap_first_item(self, them): def get_best_by_category(self, category): items_in_category = self.get_by_category(category) if len(items_in_category) > 0: + #lambda lets me define a func in a single line return max(items_in_category, key = lambda item: item.condition) else: return None