本文整理汇总了Python中inventory.Inventory.view_price方法的典型用法代码示例。如果您正苦于以下问题:Python Inventory.view_price方法的具体用法?Python Inventory.view_price怎么用?Python Inventory.view_price使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inventory.Inventory
的用法示例。
在下文中一共展示了Inventory.view_price方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CartTest
# 需要导入模块: from inventory import Inventory [as 别名]
# 或者: from inventory.Inventory import view_price [as 别名]
class CartTest(unittest.TestCase):
def setUp(self):
self.cart = Cart()
self.inv = Inventory()
def tearDown(self):
self.inv.close()
def test_add_items_to_basket(self):
# Test adding item, make sure it's in cart and inventory updates to reflect that
old_qty= self.inv.view_qty('item050')
self.cart.add_item('item050', 10)
self.assertEqual(self.cart.basket('item050'), 10)
self.assertEqual(self.inv.view_qty('item050'), old_qty-10)
self.inv.update_qty('item050', 500)
# Test adding more to cart than inventory contains
self.cart._basket['item050'] = 0 # Temporary kluge
self.cart.add_item('item050', 600)
self.assertEqual(self.cart.basket('item050'), 500)
self.assertEqual(self.inv.view_qty('item050'), 0)
self.inv.update_qty('item050', 500)
def test_remove_items_from_basket(self):
self.cart.add_item('item050', 10)
self.cart.remove_item('item050',5)
self.assertEqual(self.cart.basket('item050'), 5)
self.assertEqual(self.inv.view_qty('item050'), 495)
def test_edit_items(self):
# Change to a quantity greater than you already have
self.cart.edit_item('item050', 50)
self.cart.edit_item('item050', 70)
self.assertEqual(self.cart.basket('item050'), 70)
self.assertEqual(self.inv.view_qty('item050'), 430)
self.cart.remove_item('item050', 70)
# Add more than are in inventory
self.cart.edit_item('item050', 1000)
self.assertEqual(self.inv.view_qty('item050'), 0)
self.assertEqual(self.cart.basket('item050'), 500)
self.cart.remove_item('item050', 500)
# Change to a quantity less than what you already have
self.cart.add_item('item050', 100)
self.cart.edit_item('item050', 50)
self.assertEqual(self.inv.view_qty('item050'), 450)
self.assertEqual(self.cart.basket('item050'), 50)
self.cart.remove_item('item050', 50)
def test_items_total(self):
self.cart.add_item('item100', 100)
cost = 100 * self.inv.view_price('item100')
self.assertEqual(self.cart.items_total(), cost)
self.cart.remove_item('item100', 100)
def test_list_items(self):
self.cart.add_item('item100', 100)
# Test listing sorted by item name
for _tuple in self.cart.list_items('item'):
self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100')))
# Test listing sorted by qty
for _tuple in self.cart.list_items('price'):
self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100')))
# Test listing sorted by price
self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100')))
self.cart.remove_item('item100', 100)
示例2: Cart
# 需要导入模块: from inventory import Inventory [as 别名]
# 或者: from inventory.Inventory import view_price [as 别名]
class Cart(object):
def __init__(self):
self.inv = Inventory()
self._basket = {}
def to_JSON(self):
# JSON serializability
del self.inv.c
del self.inv.conn
print (json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4))
self.inv = Inventory()
def basket(self, name):
try:
return self._basket[name.lower()]
except KeyError:
print 'Item not in basket.'
return 0
def add_item(self, name, qty):
if self.inv.view_qty(name.lower()) >= qty:
if name.lower() in self._basket:
self._basket[name.lower()] += qty
else:
self._basket[name.lower()] = qty
self.inv.update_qty(name, self.inv.view_qty(name.lower())-qty)
else:
print 'Inventory only has %s of this item' % self.inv.view_qty(name.lower())
if name.lower() in self._basket:
self._basket[name.lower()] += self.inv.view_qty(name.lower())
else:
self._basket[name.lower()] = self.inv.view_qty(name.lower())
self.inv.update_qty(name, 0)
def remove_item(self, name, qty):
if name.lower() in self._basket:
if self.basket(name.lower()) >= qty:
self._basket[name.lower()] -= qty
self.inv.update_qty(name, self.inv.view_qty(name.lower())+qty)
else:
self.inv.update_qty(name, self.inv.view_qty(name.lower())+self._basket[name.lower()])
self._basket[name.lower()] = 0
def edit_item(self, name, qty):
if qty < 0:
return
diff = qty - self.basket(name.lower())
if diff > 0:
# Need to add items from inventory
if self.inv.view_qty(name.lower()) >= diff:
self._basket[name.lower()] = qty
self.inv.update_qty(name, self.inv.view_qty(name.lower())-diff)
else:
print 'Only %s in inventory' % self.inv.view_qty(name.lower())
self._basket[name.lower()] = self.inv.view_qty(name.lower())
self.inv.update_qty(name, 0)
elif diff < 0:
# Remove items from cart
self._basket[name.lower()] = qty
self.inv.update_qty(name, self.inv.view_qty(name.lower())-diff)
else:
# Cart already has qty items
pass
def items_total(self):
"""Return total cost of all items in cart."""
total = 0
for item in self._basket:
total += self._basket[item] * self.inv.c.execute('SELECT price FROM inventory WHERE item=?', (item,)).fetchone()[0]
return total
def list_items(self, sortby='item'):
list = []
for key in self._basket:
list.append( (key, self._basket[key], self.inv.view_price(key)) )
if sortby.lower() == 'item':
self.sort_list_of_tuples(list,0)
elif sortby.lower() == 'qty':
self.sort_list_of_tuples(list,1)
elif sortby.lower() == 'price':
self.sort_list_of_tuples(list,2)
else:
print 'Cart can only be sorted by "item", "qty", or "price".'
return
for _tuple in list:
yield _tuple
def query_by_price(self, lowprice=0, highprice=None, sortby='item'):
list = []
if highprice is not None:
for key in self._basket:
this_price = self.inv.view_price(key)
if this_price >= lowprice and this_price <= highprice:
list.append( (key, self._basket[key], self.inv.view_price(key)) )
else:
for key in self._basket:
this_price = self.inv.view_price(key)
if this_price >= lowprice:
list.append( (key, self._basket[key], self.inv.view_price(key)) )
#.........这里部分代码省略.........