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

Scissors_Gloria_Villa #66

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $ source venv/bin/activate
Verify that you're in a python3 virtual environment by running:

- `$ python --version` should output a Python 3 version
- `$ pip --version` should output that it is working with Python 3
- `$ python --version` should output that it is working with Python 3

6. Install dependencies once at the beginning of this project with

Expand All @@ -84,7 +84,7 @@ Summary of one-time project setup:
1. When you want to begin work on this project, ensure that your virtual environment is activated:

```bash
$ source venv/bin/activate
$ e
```

2. Find the test file that contains the test you want to run.
Expand Down
12 changes: 12 additions & 0 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from swap_meet.item import Item
class Clothing(Item):

def __init__(self,category = "", condition=0):
super().__init__("Clothing", condition)

def __str__(self):
return "The finest clothing you could wear."




10 changes: 10 additions & 0 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from swap_meet.item import Item
class Decor(Item):

def __init__(self,category = "", condition = 0):
super().__init__("Decor", condition)

def __str__(self):
return "Something to decorate your space."


12 changes: 12 additions & 0 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from swap_meet.item import Item
class Electronics(Item):

def __init__(self,category = "", condition=0):
super().__init__("Electronics", condition)

def __str__(self):
return "A gadget full of buttons and secrets."




26 changes: 26 additions & 0 deletions swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Item:
def __init__(self, category = "", condition = 0):
self.category = category
self.condition = float(condition)

def __str__(self):
return 'Hello World!'
def __repr__(self):
return "Item('{}')".format(self.category)

def condition_description(self):
if self.condition == 5:
desc = "mint"
elif self.condition == 4:
desc = "excellent"
elif self.condition == 3:
desc = "good"
elif self.condition == 2:
desc = "well used"
elif self.condition == 1:
desc = "Might use a glove"
elif self.condition == 0:
desc = "Make sure you really want to do this!"
Comment on lines +20 to +23

Choose a reason for hiding this comment

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

Haha

return desc


133 changes: 133 additions & 0 deletions swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
class Vendor:
def __init__(self, inventory = None):
if inventory == None:
self.inventory = []
else:
self.inventory = inventory

def add(self, item):
"""
function: adds Item to inventory
input: Item to add
output: Item (list)
"""
self.inventory.append(item)
return item

def remove(self, item):
"""
function: removes Item from inventory
input: Item to remove
output: Item list if True, or False
"""

try:
self.inventory.remove(item)
return item
except ValueError:
return False

def get_by_category(self, category):
"""
function: groups the items by category passed in
"""

by_category_list = []
for item in self.inventory:
if item.category == category:
by_category_list.append(item)
return by_category_list


def swap_items(self, vendor,my_item,vendor_item ):
"""
function: checks Items are in inventory
input: Self, Vendor and Items to swap
output: Booleon (True for success)
"""

if my_item in self.inventory and vendor_item in vendor.inventory:
self.add(vendor_item)
self.remove(my_item)
vendor.remove(vendor_item)
vendor.add(my_item)
return True
return False


def swap_first_item(self,vendor):
"""
function: swaps and removes item
input: vendor
output: True if successful, or False if empty list for vendor encountered
"""
if self.inventory == [] or vendor.inventory == []:
return False

self_first_item = self.inventory[0]
vendor_first_item = vendor.inventory[0]
self.remove(self_first_item)
self.add(vendor_first_item)
vendor.remove(vendor_first_item)
vendor.add(self_first_item)
return True
Comment on lines +67 to +73

Choose a reason for hiding this comment

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

This looks a whole lot like the code in the method above! I wonder if there's a way we could make use of that other method here...



def get_best_by_category(self,category):
"""
function: Gets all the items by category
input: category
output: True if successful, or False if empty list for vendor encountered
"""
if self.get_by_category == None:
return None

best_item = None
for item in self.get_by_category(category):

Choose a reason for hiding this comment

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

Love that you made use of the existing method here!

if best_item == None or item.condition > best_item.condition:
best_item = item
return best_item

def swap_best_by_category(self,other,my_priority,their_priority):
# my_priority is category that the Vendor wants to receive
# their_priority represents the category the other Vendor wants
"""
function: Best item in inventory that matches category is swapped with the best item in `other`'s inventory that matches `my_priority`
input: `their_priority`, my priority, other (the other vendor)
output: Boolean.

False -- If the `Vendor` has no item that matches `their_priority` category, swapping does not happen, and it returns `False. - If `other` has no item that matches `my_priority` category, swapping does not happen, and it returns `False`
"""

if other.get_by_category(my_priority) == None or self.get_by_category(their_priority)== None:
return False

my_trade_to_other = self.get_best_by_category(their_priority)
their_trade_to_me = other.get_best_by_category(my_priority)
Comment on lines +102 to +106

Choose a reason for hiding this comment

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

One minor optimization here would be to store the results of self.get_best_by_category(their_priority) and other.get_best_by_category(my_priority) in variables.
When you call the methods twice, they need to do all the work twice.


result = self.swap_items(other,my_trade_to_other,their_trade_to_me)
return result