example-code-2e/22-dyn-attr-prop/bulkfood/bulkfood_v2prop.py
2021-09-10 12:34:39 -03:00

70 lines
1.8 KiB
Python

"""
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::
# tag::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[]
"""
# tag::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[]
# tag::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[]