Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sapphire - Melinda M. #72

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Sapphire - Melinda M. #72

wants to merge 12 commits into from

Conversation

MelMott
Copy link

@MelMott MelMott commented Apr 7, 2023

No description provided.

Copy link

@mmcknett-ada mmcknett-ada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Green 🟢!

@@ -2,7 +2,7 @@
from swap_meet.vendor import Vendor
from swap_meet.item import Item

@pytest.mark.skip
#@pytest.mark.skip

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete these vs. just commenting them out

@@ -143,7 +152,15 @@ def test_swap_best_by_category_reordered():
their_priority="Decor"
)

raise Exception("Complete this test according to comments below.")
assert True

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert True is always going to pass. Did you mean assert result is True?

assert item_f in tai.inventory
assert item_d in jesse.inventory
assert item_e in jesse.inventory
assert item_c in jesse.inventory

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate how you ordered these; it made it easy for me to check the arrangement and assertions matched my expectations.

@@ -228,7 +245,15 @@ def test_swap_best_by_category_no_match_is_false():
their_priority="Clothing"
)

raise Exception("Complete this test according to comments below.")
assert not result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use assert result is False to make sure that an actual False is returned, vs. just something falsey.


def get_category(self):
return "Clothing"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Python's special variables for this:

Suggested change
return "Clothing"
return self.__class__.__name__ # Python sets this to "Clothing"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, since you do this in Item, you don't need to put the get_category overriding functions in your derived classes at all!

Comment on lines +23 to +26
return False

if my_item in self.inventory or their_item in other_vendor.inventory:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you used a guard clause with early return, it's better to remove this second if statement.

Also, it's important to note that the second clause is not the exact opposite of the guard clause (if you used and it would be, see De Morgan's Law.) If you later changed this function, having both if statements increases the likelihood that you could implicitly return None without realizing.

Comment on lines +33 to +39
if not other_vendor.inventory or not self.inventory:
return False
else:
# Called method swap_items and used indexing to make sure first items are swapped
self.swap_items( other_vendor, self.inventory[0], other_vendor.inventory[0])
return True

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because your if clause returns, the else: isn't strictly necessary. It's also a good idea to capture the True/False that comes back from swap_items. You could write:

    def swap_first_item(self, other_vendor):
        if not other_vendor.inventory or not self.inventory:
            return False

        # Called method swap_items and used indexing to make sure first items are swapped
        return self.swap_items( other_vendor, self.inventory[0], other_vendor.inventory[0])

Comment on lines +41 to +48
category_list = []

for item in self.inventory:
# Called component method to get a category for comparison in conditional
if category == item.get_category():
category_list.append(item)
return category_list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice this pattern of:

result_list = []
for element in source_list:
    if some_condition(element):
        result_list.append(element)

can be rewritten using a list comprehension as:

result_list = [element for element in source_list if some_condition(element)]

Which here would look like:

category_list = [item for item in self.inventory if item.get_category() == category]
return category_list

At first, this may seem more difficult to read, but comprehensions are a very common python style, so I encourage y’all to try working with them!

Comment on lines +50 to +57

best_item = None
for item in self.get_by_category(category):
# Used component attribute for comparisons
if best_item == None or item.condition > best_item.condition:
best_item = item
return best_item

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Once you're comfortable with the version that uses loops, you can also write this with max:

by_category = self.get_by_category(category)
return max(by_category, key=lambda item: item.condition) if by_category else None

Comment on lines +66 to +69
self.swap_items(other_vendor, my_best_item, their_best_item)
return True
return False

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since swap_items can handle one of the items being None, you can write this as simply:

return self.swap_items(other_vendor, my_best_item, their_best_item)

@MelMott
Copy link
Author

MelMott commented Apr 13, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants