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

Ruby - Thea V. #76

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d7c44ea
testing git
chickenoregg Apr 5, 2023
bb5b6e7
initialize Vendor class and add method
chickenoregg Apr 5, 2023
a8b42af
initialize Vendor class and add method
chickenoregg Apr 5, 2023
78b7c6c
pass remove method
chickenoregg Apr 5, 2023
0b07314
complete test_removing_not_found_is_false()
chickenoregg Apr 5, 2023
74bf6f3
import uuid to generate random unique numbers
chickenoregg Apr 5, 2023
13bf5b7
import uuid to generate random unique numbers
chickenoregg Apr 5, 2023
a904e60
change to int for uuid and add get_by_id method in vendor.py
chickenoregg Apr 5, 2023
3e81263
overrides to string
chickenoregg Apr 6, 2023
f7ac77b
create swap_items method and perform swap items
chickenoregg Apr 6, 2023
1840c38
add swap_first_item method
chickenoregg Apr 7, 2023
b2e30dd
create class Clothing and attributes, create get_category method
chickenoregg Apr 7, 2023
b907fba
add stringfy method and use __str__to call stringify method
chickenoregg Apr 7, 2023
bf2565b
create class Decor and add attributes
chickenoregg Apr 7, 2023
b8e15d4
create get_category method
chickenoregg Apr 7, 2023
b096e63
create stringify method
chickenoregg Apr 7, 2023
2464956
create class Electronics and add attributes
chickenoregg Apr 7, 2023
8da77bf
create get_category method for Electronics
chickenoregg Apr 7, 2023
e077af7
add stringify method in Electronics
chickenoregg Apr 7, 2023
aef41a7
add condition as an attribute to clothing, decor, electronic, and item
chickenoregg Apr 7, 2023
8067003
removed conditions, other tests on wave 5 started failing
chickenoregg Apr 7, 2023
a21952a
add get_by_category method
chickenoregg Apr 7, 2023
a31ad6f
add condition_description to all, this is not the most optimal way bu…
chickenoregg Apr 7, 2023
9f27198
add get_best_by_category method in Vendor class
chickenoregg Apr 7, 2023
e6d5868
add swap_best_by_category method in Vendor class and fixed test
chickenoregg Apr 7, 2023
0e543cf
complete test_swap_best_by_category_reordered()
chickenoregg Apr 7, 2023
b2a3c2a
complete test_swap_best_by_category_no_match_is_false()
chickenoregg Apr 7, 2023
d5410ed
complete test_swap_best_by_category_no_match_is_false()
chickenoregg Apr 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,40 @@

import uuid


class Clothing:

Choose a reason for hiding this comment

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

The requirements for the project stated that the Clothing, Decor, and Electronics classes should inherit from the Item class. Since this was not done, this project is a yellow.

Inheritance helps with keeping the code DRY. There is a lot of functionality that is the same between Clothing, Decor, Electronics, and Item that can be eliminated if we use inheritance.

pass
def __init__(self, fabric="Unknown", condition=0, id=None):
if id:
self.id = id
else:
# using uuid to generate random id
self.id = int(uuid.uuid4())

self.fabric = fabric
self.condition = condition

def get_category(self):
return "Clothing"

def stringify(self):
return f"An object of type {self.get_category()} with id {self.id}. It is made from {self.fabric} fabric."

# calling the stringfy method
def __str__(self):
return self.stringify()

def condition_description(self):
if self.condition == 0:
return "very bad"
elif self.condition == 1:
return "bad"
elif self.condition == 2:
return "fair"
elif self.condition == 3:
return "good"
elif self.condition == 4:
return "very good"
elif self.condition == 5:
return "great"

return "not acceptable"
39 changes: 38 additions & 1 deletion swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
import uuid


class Decor:
pass
def __init__(self, width=0, length=0, condition=0, id=None):
if id:
self.id = id
else:
# using uuid to generate random id
self.id = int(uuid.uuid4())

self.width = width
self.length = length
self.condition = condition

def get_category(self):
return "Decor"

def stringify(self):
return f"An object of type {self.get_category()} with id {self.id}. It takes up a {self.width} by {self.length} sized space."
Comment on lines +19 to +20

Choose a reason for hiding this comment

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

Instead of creating a helper function that's only one line, this line of code can go into the __str__ method.


def __str__(self):
return self.stringify()

def condition_description(self):
if self.condition == 0:
return "very bad"
elif self.condition == 1:
return "bad"
elif self.condition == 2:
return "fair"
elif self.condition == 3:
return "good"
elif self.condition == 4:
return "very good"
elif self.condition == 5:
return "great"

return "not acceptable"
38 changes: 37 additions & 1 deletion swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
import uuid


class Electronics:
pass
def __init__(self, type="Unknown", condition=0, id=None):
if id:
self.id = id
else:
# using uuid to generate random id
self.id = int(uuid.uuid4())

self.type = type
self.condition = condition

def get_category(self):
return "Electronics"

def stringify(self):
return f"An object of type {self.get_category()} with id {self.id}. This is a {self.type} device."

def __str__(self):
return self.stringify()

def condition_description(self):
if self.condition == 0:
return "very bad"
elif self.condition == 1:
return "bad"
elif self.condition == 2:
return "fair"
elif self.condition == 3:
return "good"
elif self.condition == 4:
return "very good"
elif self.condition == 5:
return "great"

return "not acceptable"
34 changes: 33 additions & 1 deletion swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
import uuid
# from swap_meet import Vendor


class Item:

Choose a reason for hiding this comment

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

Excellent!

pass
def __init__(self, id=None, condition=0):
if id:
self.id = id
else:
# using uuid to generate random id
self.id = int(uuid.uuid4())
self.conditon = condition

def get_category(self):
return "Item"

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

def condition_description(self):
if self.condition == 0:
return "very bad"
elif self.condition == 1:
return "bad"
elif self.condition == 2:
return "fair"
elif self.condition == 3:
return "good"
elif self.condition == 4:
return "very good"
elif self.condition == 5:
return "great"

return "not acceptable"
103 changes: 102 additions & 1 deletion swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,103 @@


class Vendor:
pass
#
def __init__(self, inventory=None):

Choose a reason for hiding this comment

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

Excellent!

# if inventory is truthy, values will be assigned to inventory
if inventory:
self.inventory = inventory
# otherwise, []
else:
self.inventory = []

def add(self, item):

Choose a reason for hiding this comment

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

Excellent!

self.inventory.append(item)
return item

# if theres a match, item gets removed
def remove(self, item):

Choose a reason for hiding this comment

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

Excellent!


if item in self.inventory:
self.inventory.remove(item)
return item

else:
return False

# chhecking if id matches any in the invetory
#
def get_by_id(self, id):

Choose a reason for hiding this comment

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

Excellent!

for each_item in self.inventory:
if each_item.id == id:
return each_item

return None

def swap_items(self, other_vendor, my_item, their_item):
# if either of these are not found, returns False
if my_item not in self.inventory:
return False

if their_item not in other_vendor.inventory:
return False
# if found
# performs the swap
self_item_idx = self.inventory.index(my_item)
their_item_idx = other_vendor.inventory.index(their_item)
# using a temp variable to hold the value then swap values
temp = self.inventory[self_item_idx]
self.inventory[self_item_idx] = their_item
other_vendor.inventory[their_item_idx] = temp
Comment on lines +45 to +50

Choose a reason for hiding this comment

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

Instead of using index to add/remove items, it would be better to reuse the add and remove methods you created earlier.


return True

def swap_first_item(self, other_vendor):

# check if any is empty
if self.inventory == []:
return False

if other_vendor.inventory == []:
return False
# swap the first items
self.inventory[0], other_vendor.inventory[0] = other_vendor.inventory[0], self.inventory[0]

Choose a reason for hiding this comment

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

This would be a great place to reuse the swap_items method you wrote earlier!


return True

# loops through the inventory to sotre items in the list
def get_by_category(self, category):

Choose a reason for hiding this comment

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

Excellent!


category_list = []
for each_item in self.inventory:
if each_item.get_category() == category:
category_list.append(each_item)
return category_list

def get_best_by_category(self, category):

Choose a reason for hiding this comment

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

Excellent!


items = self.get_by_category(category)
if not items:
return None

best_item = items[0]
# get best item in items list
for item in items:
if item.condition > best_item.condition:
best_item = item
return best_item

def swap_best_by_category(self, other_vendor, my_priority, their_priority):
# get best - their_priority
my_best = self.get_best_by_category(their_priority)
# get best - my_priority
their_best = other_vendor.get_best_by_category(my_priority)

if not my_best or not their_best:
return False
# removing my_best and theirs in my inventory and vice versa
self.remove(my_best)
other_vendor.remove(their_best)
self.add(their_best)
other_vendor.add(my_best)
Comment on lines +98 to +101

Choose a reason for hiding this comment

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

Another great place to use the swap_items method you created!


return True
21 changes: 12 additions & 9 deletions tests/unit_tests/test_wave_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
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)
Expand All @@ -16,7 +19,8 @@ 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"
Expand All @@ -27,7 +31,8 @@ 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(
Expand All @@ -40,7 +45,8 @@ 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(
Expand All @@ -49,7 +55,4 @@ def test_removing_not_found_is_false():

result = vendor.remove(item)

raise Exception("Complete this test according to comments below.")
# *********************************************************************
# ****** Complete Assert Portion of this test **********
# *********************************************************************
assert result == False

Choose a reason for hiding this comment

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

Great start! It would be good to also have an assert to make sure the number of items in the inventory hasn't changed and that nothing was accidentally removed.

20 changes: 14 additions & 6 deletions tests/unit_tests/test_wave_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,37 @@
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)
Expand All @@ -36,7 +43,8 @@ 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()
Expand Down
Loading