Initial commit
This commit is contained in:
14
Solutions/6_1/stock.py
Normal file
14
Solutions/6_1/stock.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# stock.py
|
||||
|
||||
from structure import Structure
|
||||
|
||||
class Stock(Structure):
|
||||
_fields = ('name', 'shares', 'price')
|
||||
|
||||
@property
|
||||
def cost(self):
|
||||
return self.shares * self.price
|
||||
|
||||
def sell(self, nshares):
|
||||
self.shares -= nshares
|
||||
|
||||
19
Solutions/6_1/structure.py
Normal file
19
Solutions/6_1/structure.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# structure.py
|
||||
|
||||
class Structure:
|
||||
_fields = ()
|
||||
def __init__(self, *args):
|
||||
if len(args) != len(self._fields):
|
||||
raise TypeError('Expected %d arguments' % len(self._fields))
|
||||
for name, val in zip(self._fields, args):
|
||||
setattr(self, name, val)
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if name.startswith('_') or name in self._fields:
|
||||
super().__setattr__(name, value)
|
||||
else:
|
||||
raise AttributeError('No attribute %s' % name)
|
||||
|
||||
def __repr__(self):
|
||||
return '%s(%s)' % (type(self).__name__,
|
||||
', '.join(repr(getattr(self, name)) for name in self._fields))
|
||||
70
Solutions/6_1/teststock.py
Normal file
70
Solutions/6_1/teststock.py
Normal file
@@ -0,0 +1,70 @@
|
||||
# teststock.py
|
||||
|
||||
import stock
|
||||
import unittest
|
||||
|
||||
class TestStock(unittest.TestCase):
|
||||
def test_create(self):
|
||||
s = stock.Stock('GOOG', 100, 490.1)
|
||||
self.assertEqual(s.name, 'GOOG')
|
||||
self.assertEqual(s.shares, 100)
|
||||
self.assertEqual(s.price, 490.1)
|
||||
|
||||
def test_create_keyword(self):
|
||||
s = stock.Stock(name='GOOG', shares=100, price=490.1)
|
||||
self.assertEqual(s.name, 'GOOG')
|
||||
self.assertEqual(s.shares, 100)
|
||||
self.assertEqual(s.price, 490.1)
|
||||
|
||||
def test_cost(self):
|
||||
s = stock.Stock('GOOG', 100, 490.1)
|
||||
self.assertEqual(s.cost, 49010.0)
|
||||
|
||||
def test_sell(self):
|
||||
s = stock.Stock('GOOG', 100, 490.1)
|
||||
s.sell(25)
|
||||
self.assertEqual(s.shares, 75)
|
||||
|
||||
def test_from_row(self):
|
||||
s = stock.Stock.from_row(['GOOG','100','490.1'])
|
||||
self.assertEqual(s.name, 'GOOG')
|
||||
self.assertEqual(s.shares, 100)
|
||||
self.assertEqual(s.price, 490.1)
|
||||
|
||||
def test_repr(self):
|
||||
s = stock.Stock('GOOG', 100, 490.1)
|
||||
self.assertEqual(repr(s), "Stock('GOOG', 100, 490.1)")
|
||||
|
||||
def test_eq(self):
|
||||
a = stock.Stock('GOOG', 100, 490.1)
|
||||
b = stock.Stock('GOOG', 100, 490.1)
|
||||
self.assertTrue(a==b)
|
||||
|
||||
# Tests for failure conditions
|
||||
def test_shares_badtype(self):
|
||||
s = stock.Stock('GOOG', 100, 490.1)
|
||||
with self.assertRaises(TypeError):
|
||||
s.shares = '50'
|
||||
|
||||
def test_shares_badvalue(self):
|
||||
s = stock.Stock('GOOG', 100, 490.1)
|
||||
with self.assertRaises(ValueError):
|
||||
s.shares = -50
|
||||
|
||||
def test_price_badtype(self):
|
||||
s = stock.Stock('GOOG', 100, 490.1)
|
||||
with self.assertRaises(TypeError):
|
||||
s.price = '45.23'
|
||||
|
||||
def test_price_badvalue(self):
|
||||
s = stock.Stock('GOOG', 100, 490.1)
|
||||
with self.assertRaises(ValueError):
|
||||
s.price = -45.23
|
||||
|
||||
def test_bad_attribute(self):
|
||||
s = stock.Stock('GOOG', 100, 490.1)
|
||||
with self.assertRaises(AttributeError):
|
||||
s.share = 100
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user