Skip to content

Commit 0618105

Browse files
committed
update from Atlas
1 parent 1edb0cd commit 0618105

18 files changed

+820
-121
lines changed

descriptors/bulkfood_v2b.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
3+
A line item for a bulk food order has description, weight and price fields::
4+
5+
>>> raisins = LineItem('Golden raisins', 10, 6.95)
6+
>>> raisins.weight, raisins.description, raisins.price
7+
(10, 'Golden raisins', 6.95)
8+
9+
A ``subtotal`` method gives the total price for that line item::
10+
11+
>>> raisins.subtotal()
12+
69.5
13+
14+
The weight of a ``LineItem`` must be greater than 0::
15+
16+
>>> raisins.weight = -20
17+
Traceback (most recent call last):
18+
...
19+
ValueError: value must be > 0
20+
21+
No change was made::
22+
23+
>>> raisins.weight
24+
10
25+
26+
The check is also performed on instantiation::
27+
28+
>>> walnuts = LineItem('walnuts', 0, 10.00)
29+
Traceback (most recent call last):
30+
...
31+
ValueError: value must be > 0
32+
33+
The proteced attribute can still be accessed if needed for some reason, such as
34+
white box testing)::
35+
36+
>>> raisins._LineItem__weight
37+
10
38+
39+
"""
40+
41+
42+
# BEGIN LINEITEM_V2B
43+
class LineItem:
44+
45+
def __init__(self, description, weight, price):
46+
self.description = description
47+
self.weight = weight
48+
self.price = price
49+
50+
def subtotal(self):
51+
return self.weight * self.price
52+
53+
def get_weight(self): # <1>
54+
return self.__weight
55+
56+
def set_weight(self, value): # <2>
57+
if value > 0:
58+
self.__weight = value
59+
else:
60+
raise ValueError('value must be > 0')
61+
62+
weight = property(get_weight, set_weight) # <3>
63+
64+
# END LINEITEM_V2B

descriptors/bulkfood_v2prop.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
3+
A line item for a bulk food order has description, weight and price fields::
4+
5+
>>> raisins = LineItem('Golden raisins', 10, 6.95)
6+
>>> raisins.weight, raisins.description, raisins.price
7+
(10, 'Golden raisins', 6.95)
8+
9+
A ``subtotal`` method gives the total price for that line item::
10+
11+
>>> raisins.subtotal()
12+
69.5
13+
14+
The weight of a ``LineItem`` must be greater than 0::
15+
16+
>>> raisins.weight = -20
17+
Traceback (most recent call last):
18+
...
19+
ValueError: value must be > 0
20+
21+
No change was made::
22+
23+
>>> raisins.weight
24+
10
25+
26+
27+
"""
28+
29+
30+
# BEGIN LINEITEM_V2_PROP
31+
def quantity(storage_name): # <1>
32+
33+
@property # <2>
34+
def new_prop(self):
35+
return self.__dict__[storage_name] # <3>
36+
37+
@new_prop.setter
38+
def new_prop(self, value):
39+
if value > 0:
40+
self.__dict__[storage_name] = value # <4>
41+
else:
42+
raise ValueError('value must be > 0')
43+
44+
return new_prop # <5>
45+
46+
47+
class LineItem:
48+
weight = quantity('weight') # <6>
49+
price = quantity('price') # <7>
50+
51+
def __init__(self, description, weight, price):
52+
self.description = description
53+
self.weight = weight
54+
self.price = price
55+
56+
def subtotal(self):
57+
return self.weight * self.price
58+
# END LINEITEM_V2_PROP

descriptors/bulkfood_v4prop.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
3+
A line item for a bulk food order has description, weight and price fields::
4+
5+
>>> raisins = LineItem('Golden raisins', 10, 6.95)
6+
>>> raisins.weight, raisins.description, raisins.price
7+
(10, 'Golden raisins', 6.95)
8+
9+
A ``subtotal`` method gives the total price for that line item::
10+
11+
>>> raisins.subtotal()
12+
69.5
13+
14+
The weight of a ``LineItem`` must be greater than 0::
15+
16+
>>> raisins.weight = -20
17+
Traceback (most recent call last):
18+
...
19+
ValueError: value must be > 0
20+
21+
No change was made::
22+
23+
>>> raisins.weight
24+
10
25+
26+
The value of the attributes managed by the descriptors are stored in
27+
alternate attributes, created by the descriptors in each ``LineItem``
28+
instance::
29+
30+
>>> raisins = LineItem('Golden raisins', 10, 6.95)
31+
>>> sorted(dir(raisins)) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
32+
[..., '_quantity_0', '_quantity_1', 'description',
33+
'price', 'subtotal', 'weight']
34+
>>> raisins._quantity_0
35+
10
36+
>>> raisins._quantity_1
37+
6.95
38+
39+
"""
40+
41+
42+
# BEGIN LINEITEM_V4_PROP
43+
def quantity(): # <1>
44+
try:
45+
quantity.counter += 1 # <2>
46+
except AttributeError:
47+
quantity.counter = 0 # <3>
48+
49+
storage_name = '_{}_{}'.format('quantity', quantity.counter) # <4>
50+
51+
@property # <5>
52+
def prop(self):
53+
return getattr(self, storage_name)
54+
55+
@prop.setter
56+
def prop(self, value):
57+
if value > 0:
58+
setattr(self, storage_name, value)
59+
else:
60+
raise ValueError('value must be > 0')
61+
62+
return prop # <6>
63+
64+
65+
class LineItem:
66+
weight = quantity() # <7>
67+
price = quantity()
68+
69+
def __init__(self, description, weight, price):
70+
self.description = description
71+
self.weight = weight
72+
self.price = price
73+
74+
def subtotal(self):
75+
return self.weight * self.price
76+
# END LINEITEM_V4_PROP

descriptors/bulkfood_v5.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
3+
A line item for a bulk food order has description, weight and price fields::
4+
5+
>>> raisins = LineItem('Golden raisins', 10, 6.95)
6+
>>> raisins.weight, raisins.description, raisins.price
7+
(10, 'Golden raisins', 6.95)
8+
9+
A ``subtotal`` method gives the total price for that line item::
10+
11+
>>> raisins.subtotal()
12+
69.5
13+
14+
The weight of a ``LineItem`` must be greater than 0::
15+
16+
>>> raisins.weight = -20
17+
Traceback (most recent call last):
18+
...
19+
ValueError: value must be > 0
20+
21+
No change was made::
22+
23+
>>> raisins.weight
24+
10
25+
26+
The value of the attributes managed by the descriptors are stored in
27+
alternate attributes, created by the descriptors in each ``LineItem``
28+
instance::
29+
30+
>>> raisins = LineItem('Golden raisins', 10, 6.95)
31+
>>> dir(raisins) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
32+
['_NonBlank_0', '_Quantity_0', '_Quantity_1', '__class__', ...
33+
'description', 'price', 'subtotal', 'weight']
34+
>>> raisins._Quantity_0
35+
10
36+
>>> raisins._Quantity_1
37+
6.95
38+
39+
If the descriptor is accessed in the class, the descriptor object is
40+
returned:
41+
42+
>>> LineItem.price # doctest: +ELLIPSIS
43+
<model_v5.Quantity object at 0x...>
44+
>>> br_nuts = LineItem('Brazil nuts', 10, 34.95)
45+
>>> br_nuts.price
46+
34.95
47+
48+
The `NonBlank` descriptor prevents empty or blank strings to be used
49+
for the description:
50+
51+
>>> br_nuts.description = ' '
52+
Traceback (most recent call last):
53+
...
54+
ValueError: value cannot be empty or blank
55+
56+
57+
"""
58+
59+
# BEGIN LINEITEM_V5
60+
import model_v5 as model # <1>
61+
62+
63+
class LineItem:
64+
description = model.NonBlank() # <2>
65+
weight = model.Quantity()
66+
price = model.Quantity()
67+
68+
def __init__(self, description, weight, price):
69+
self.description = description
70+
self.weight = weight
71+
self.price = price
72+
73+
def subtotal(self):
74+
return self.weight * self.price
75+
# END LINEITEM_V5

0 commit comments

Comments
 (0)