updated from Atlas
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
"""
|
||||
A line item for a bulk food order has description, weight and price fields.
|
||||
A ``subtotal`` method gives the total price for that line item::
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> raisins.weight, raisins.description, raisins.price
|
||||
(10, 'Golden raisins', 6.95)
|
||||
>>> raisins.subtotal()
|
||||
69.5
|
||||
|
||||
But, without validation, these public attributes can cause trouble::
|
||||
|
||||
# BEGIN LINEITEM_PROBLEM_V1
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> raisins.subtotal()
|
||||
69.5
|
||||
>>> raisins.weight = -20 # garbage in...
|
||||
>>> raisins.subtotal() # garbage out...
|
||||
-139.0
|
||||
|
||||
# END LINEITEM_PROBLEM_V1
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# BEGIN LINEITEM_V1
|
||||
class LineItem:
|
||||
|
||||
def __init__(self, description, weight, price):
|
||||
self.description = description
|
||||
self.weight = weight
|
||||
self.price = price
|
||||
|
||||
def subtotal(self):
|
||||
return self.weight * self.price
|
||||
# END LINEITEM_V1
|
||||
@@ -1,63 +0,0 @@
|
||||
"""
|
||||
|
||||
A line item for a bulk food order has description, weight and price fields::
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> raisins.weight, raisins.description, raisins.price
|
||||
(10, 'Golden raisins', 6.95)
|
||||
|
||||
A ``subtotal`` method gives the total price for that line item::
|
||||
|
||||
>>> raisins.subtotal()
|
||||
69.5
|
||||
|
||||
The weight of a ``LineItem`` must be greater than 0::
|
||||
|
||||
>>> raisins.weight = -20
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value must be > 0
|
||||
|
||||
No change was made::
|
||||
|
||||
>>> raisins.weight
|
||||
10
|
||||
|
||||
The check is also performed on instantiation::
|
||||
|
||||
>>> walnuts = LineItem('walnuts', 0, 10.00)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value must be > 0
|
||||
|
||||
The proteced attribute can still be accessed if needed for some reason, such as
|
||||
white box testing)::
|
||||
|
||||
>>> raisins._LineItem__weight
|
||||
10
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# BEGIN LINEITEM_V2
|
||||
class LineItem:
|
||||
|
||||
def __init__(self, description, weight, price):
|
||||
self.description = description
|
||||
self.weight = weight # <1>
|
||||
self.price = price
|
||||
|
||||
def subtotal(self):
|
||||
return self.weight * self.price
|
||||
|
||||
@property # <2>
|
||||
def weight(self): # <3>
|
||||
return self.__weight # <4>
|
||||
|
||||
@weight.setter # <5>
|
||||
def weight(self, value):
|
||||
if value > 0:
|
||||
self.__weight = value # <6>
|
||||
else:
|
||||
raise ValueError('value must be > 0') # <7>
|
||||
# END LINEITEM_V2
|
||||
@@ -1,64 +0,0 @@
|
||||
"""
|
||||
|
||||
A line item for a bulk food order has description, weight and price fields::
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> raisins.weight, raisins.description, raisins.price
|
||||
(10, 'Golden raisins', 6.95)
|
||||
|
||||
A ``subtotal`` method gives the total price for that line item::
|
||||
|
||||
>>> raisins.subtotal()
|
||||
69.5
|
||||
|
||||
The weight of a ``LineItem`` must be greater than 0::
|
||||
|
||||
>>> raisins.weight = -20
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value must be > 0
|
||||
|
||||
No change was made::
|
||||
|
||||
>>> raisins.weight
|
||||
10
|
||||
|
||||
The check is also performed on instantiation::
|
||||
|
||||
>>> walnuts = LineItem('walnuts', 0, 10.00)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value must be > 0
|
||||
|
||||
The proteced attribute can still be accessed if needed for some reason, such as
|
||||
white box testing)::
|
||||
|
||||
>>> raisins._LineItem__weight
|
||||
10
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# BEGIN LINEITEM_V2B
|
||||
class LineItem:
|
||||
|
||||
def __init__(self, description, weight, price):
|
||||
self.description = description
|
||||
self.weight = weight
|
||||
self.price = price
|
||||
|
||||
def subtotal(self):
|
||||
return self.weight * self.price
|
||||
|
||||
def get_weight(self): # <1>
|
||||
return self.__weight
|
||||
|
||||
def set_weight(self, value): # <2>
|
||||
if value > 0:
|
||||
self.__weight = value
|
||||
else:
|
||||
raise ValueError('value must be > 0')
|
||||
|
||||
weight = property(get_weight, set_weight) # <3>
|
||||
|
||||
# END LINEITEM_V2B
|
||||
@@ -1,69 +0,0 @@
|
||||
"""
|
||||
|
||||
A line item for a bulk food order has description, weight and price fields::
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> raisins.weight, raisins.description, raisins.price
|
||||
(10, 'Golden raisins', 6.95)
|
||||
|
||||
A ``subtotal`` method gives the total price for that line item::
|
||||
|
||||
>>> raisins.subtotal()
|
||||
69.5
|
||||
|
||||
The weight of a ``LineItem`` must be greater than 0::
|
||||
|
||||
>>> raisins.weight = -20
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value must be > 0
|
||||
|
||||
No change was made::
|
||||
|
||||
>>> raisins.weight
|
||||
10
|
||||
|
||||
The value of the attributes managed by the properties are stored in
|
||||
instance attributes, created in each ``LineItem`` instance::
|
||||
|
||||
# BEGIN LINEITEM_V2_PROP_DEMO
|
||||
>>> nutmeg = LineItem('Moluccan nutmeg', 8, 13.95)
|
||||
>>> nutmeg.weight, nutmeg.price # <1>
|
||||
(8, 13.95)
|
||||
>>> sorted(vars(nutmeg).items()) # <2>
|
||||
[('description', 'Moluccan nutmeg'), ('price', 13.95), ('weight', 8)]
|
||||
|
||||
# END LINEITEM_V2_PROP_DEMO
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# BEGIN LINEITEM_V2_PROP_FACTORY_FUNCTION
|
||||
def quantity(storage_name): # <1>
|
||||
|
||||
def qty_getter(instance): # <2>
|
||||
return instance.__dict__[storage_name] # <3>
|
||||
|
||||
def qty_setter(instance, value): # <4>
|
||||
if value > 0:
|
||||
instance.__dict__[storage_name] = value # <5>
|
||||
else:
|
||||
raise ValueError('value must be > 0')
|
||||
|
||||
return property(qty_getter, qty_setter) # <6>
|
||||
# END LINEITEM_V2_PROP_FACTORY_FUNCTION
|
||||
|
||||
|
||||
# BEGIN LINEITEM_V2_PROP_CLASS
|
||||
class LineItem:
|
||||
weight = quantity('weight') # <1>
|
||||
price = quantity('price') # <2>
|
||||
|
||||
def __init__(self, description, weight, price):
|
||||
self.description = description
|
||||
self.weight = weight # <3>
|
||||
self.price = price
|
||||
|
||||
def subtotal(self):
|
||||
return self.weight * self.price # <4>
|
||||
# END LINEITEM_V2_PROP_CLASS
|
||||
Reference in New Issue
Block a user