Skip to content

Sphinx C22 - Aleida Vieyra and Tami Gaertner#24

Open
aleidavi wants to merge 37 commits into
Ada-C22:mainfrom
aleidavi:main
Open

Sphinx C22 - Aleida Vieyra and Tami Gaertner#24
aleidavi wants to merge 37 commits into
Ada-C22:mainfrom
aleidavi:main

Conversation

@aleidavi

@aleidavi aleidavi commented Oct 4, 2024

Copy link
Copy Markdown

No description provided.

tagaertner and others added 30 commits September 30, 2024 15:08
…onditional_dict file for the condition_description in item, cleaned up code.
Comment thread swap_meet/clothing.py
Comment on lines +15 to +16
def get_category(self):
return "Clothing"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For our children classes of Item, we have a get_category method that essentially returns the name of the class. Though we are returning the different names (Clothing, Decor, Electronic) we could actually move this functionality into the parent class, Item. This is how it would look:

class Item:
...
def  get_category(self):
	return  self.__class__.__name__

self refers to the class instance that called it. Meaning if we have the following:

shirt = Clothing()

shirt.get_category() # returns "Clothing"

self in this case refers to shirt. The class of clothing is Clothing and since Clothing is inheriting from the Item class it has access to the get_category method. This method is inherently being passed shirt and then accesses the __class__ attribute and then the __name__ attribute which gives us Clothing. Doing this will allow to make our code more DRY since we do this for Clothing, Decor, and Electronic.

Comment thread swap_meet/clothing.py
Comment on lines +12 to +13
def __str__(self):
return f"An object of type Clothing with id {self.id}. It is made from {self.fabric} fabric."

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Notice that each __str__ method for Clothing, Decor, and Electronic all start with "An object of type [CLASSNAME] with id [ID].". Inside of Item we can have a __str__ method that print could return the part mentioned above and then pair that with the __str__ inside the child classes (Clothing, Decor, and Electronic). It could something like this:

class Item: 
...
	def  __str__(self):
		return  f"An object of type {self.__class__.__name__)}" \
		f" with id {self.id}."

class Clothing(Item):
...
	def  __str__(self):
		return  f"{super().__str__()} It is made " \
		f"from {self.fabric} fabric."

Comment on lines +7 to +14
condition_table = {
5: "Brand New",
4: "Used - Like New/ Open Box/ Without Tags",
3: "Used - Good",
2: "Used - Fair",
1: "Used - Poor",
0: "For Parts - Not Working"
} No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How love you used a dictionary here! This could also be a time to use match case!

    def condition_description(self):
        match self.condition:
            case 0:
                return "Throw Away"
            case 1:
                return "Eh"
            case 2:
                return "Better..."
            case 3:
                return "..."
            case 4: 
                return "Okay?!"
            case 5:
                return "Yes!"

Comment thread swap_meet/item.py
'''

def __init__(self, id=None, condition=0):
self.id = uuid.uuid4().int if id == None else id

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread swap_meet/item.py
Returns a description of the items condition based on its condition value.
'''
if self.condition in condition_table.keys():
return condition_table[int(self.condition)]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice typecasting here since floats are passed in inside of the tests!

Comment thread swap_meet/vendor.py
self.inventory.remove(item)
return item

def get_by_id(self, vendor_id):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would maybe change this to item_id since we are looking for a specific item by id not necessarily a vendor.

Comment thread swap_meet/vendor.py
Returns None if no matching item is found.
'''

for object in self.inventory:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe the name item would be more declarative here?

Comment thread swap_meet/vendor.py
Returns: bool: True if the swap was successful, False otherwise.
'''

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We could also use get_by_id to check if an item is in a given inventory and then check to see if that function returns None or the item.

Comment thread swap_meet/vendor.py
Comment on lines +45 to +48
self.remove(my_item)
other_vendor.remove(their_item)
self.add(their_item)
other_vendor.add(my_item)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⭐️

Comment thread swap_meet/vendor.py
Returns a list of items in the inventory matching the given category.
'''

category_objects = [object for object in self.inventory if category == object.get_category()]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread swap_meet/vendor.py
Comment on lines +83 to +98
def get_best_by_category(self, category_best):
'''
Returns the item with the highest condition in the given category.
'''

category_inventory = self.get_by_category(category_best)

if not category_inventory:
return None

best_condition_object = category_inventory[0]

for object in category_inventory:
if object.condition > best_condition_object.condition:
best_condition_object = object
return best_condition_object

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great work with this function!

Comment thread swap_meet/vendor.py
Comment on lines +68 to +72
if not self.inventory or not other_vendor.inventory:
return False

my_best_item = self.get_best_by_category(their_priority)
their_best_item = other_vendor.get_best_by_category(my_priority)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We could also write this in the following manner:

        my_best = self.get_best_by_category(their_priority)
        their_best = other_vendor.get_best_by_category(my_priority)

        if not my_best or not their_best:
            return False
        
        self.swap_items(other_vendor, my_best, their_best)

        return True

Comment on lines +55 to +60
assert result == False
assert len(vendor.inventory) == 3
assert item not in vendor.inventory
assert "a" in vendor.inventory
assert "b" in vendor.inventory
assert "c" in vendor.inventory

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great work!

# *********************************************************************
# ****** Complete Assert Portion of this test **********
assert result == False
assert len(jolie.inventory) == 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

assert jolie.inventory == [] would ensure that the inventory is an empty list.

Comment on lines +139 to +141
assert item_a in fatimah.inventory
assert item_b in fatimah.inventory
assert item_c in fatimah.inventory

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We could also use a loop here!

Comment on lines +169 to +170
assert len(tai.inventory)==3
assert len(jesse.inventory)==3

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Don't forget your spacing between operators

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.

3 participants