removed 1st edition code
This commit is contained in:
@@ -1,61 +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
|
||||
|
||||
Negative or 0 price is not acceptable either::
|
||||
|
||||
>>> truffle = LineItem('White truffle', 100, 0)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value must be > 0
|
||||
|
||||
|
||||
No change was made::
|
||||
|
||||
>>> raisins.weight
|
||||
10
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# BEGIN LINEITEM_V3
|
||||
class Quantity: # <1>
|
||||
|
||||
def __init__(self, storage_name):
|
||||
self.storage_name = storage_name # <2>
|
||||
|
||||
def __set__(self, instance, value): # <3>
|
||||
if value > 0:
|
||||
instance.__dict__[self.storage_name] = value # <4>
|
||||
else:
|
||||
raise ValueError('value must be > 0')
|
||||
|
||||
|
||||
class LineItem:
|
||||
weight = Quantity('weight') # <5>
|
||||
price = Quantity('price') # <6>
|
||||
|
||||
def __init__(self, description, weight, price): # <7>
|
||||
self.description = description
|
||||
self.weight = weight
|
||||
self.price = price
|
||||
|
||||
def subtotal(self):
|
||||
return self.weight * self.price
|
||||
# END LINEITEM_V3
|
||||
@@ -1,74 +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 descriptors are stored in
|
||||
alternate attributes, created by the descriptors in each ``LineItem``
|
||||
instance::
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> dir(raisins) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
|
||||
['_Quantity#0', '_Quantity#1', '__class__', ...
|
||||
'description', 'price', 'subtotal', 'weight']
|
||||
>>> getattr(raisins, '_Quantity#0')
|
||||
10
|
||||
>>> getattr(raisins, '_Quantity#1')
|
||||
6.95
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# BEGIN LINEITEM_V4
|
||||
class Quantity:
|
||||
__counter = 0 # <1>
|
||||
|
||||
def __init__(self):
|
||||
cls = self.__class__ # <2>
|
||||
prefix = cls.__name__
|
||||
index = cls.__counter
|
||||
self.storage_name = '_{}#{}'.format(prefix, index) # <3>
|
||||
cls.__counter += 1 # <4>
|
||||
|
||||
def __get__(self, instance, owner): # <5>
|
||||
return getattr(instance, self.storage_name) # <6>
|
||||
|
||||
def __set__(self, instance, value):
|
||||
if value > 0:
|
||||
setattr(instance, self.storage_name, value) # <7>
|
||||
else:
|
||||
raise ValueError('value must be > 0')
|
||||
|
||||
|
||||
class LineItem:
|
||||
weight = Quantity() # <8>
|
||||
price = Quantity()
|
||||
|
||||
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_V4
|
||||
@@ -1,85 +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 descriptors are stored in
|
||||
alternate attributes, created by the descriptors in each ``LineItem``
|
||||
instance::
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> dir(raisins) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
|
||||
['_Quantity#0', '_Quantity#1', '__class__', ...
|
||||
'description', 'price', 'subtotal', 'weight']
|
||||
>>> getattr(raisins, '_Quantity#0')
|
||||
10
|
||||
>>> getattr(raisins, '_Quantity#1')
|
||||
6.95
|
||||
|
||||
If the descriptor is accessed in the class, the descriptor object is
|
||||
returned:
|
||||
|
||||
>>> LineItem.weight # doctest: +ELLIPSIS
|
||||
<bulkfood_v4b.Quantity object at 0x...>
|
||||
>>> LineItem.weight.storage_name
|
||||
'_Quantity#0'
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# BEGIN LINEITEM_V4B
|
||||
class Quantity:
|
||||
__counter = 0
|
||||
|
||||
def __init__(self):
|
||||
cls = self.__class__
|
||||
prefix = cls.__name__
|
||||
index = cls.__counter
|
||||
self.storage_name = '_{}#{}'.format(prefix, index)
|
||||
cls.__counter += 1
|
||||
|
||||
def __get__(self, instance, owner):
|
||||
if instance is None:
|
||||
return self # <1>
|
||||
else:
|
||||
return getattr(instance, self.storage_name) # <2>
|
||||
|
||||
def __set__(self, instance, value):
|
||||
if value > 0:
|
||||
setattr(instance, self.storage_name, value)
|
||||
else:
|
||||
raise ValueError('value must be > 0')
|
||||
# END LINEITEM_V4B
|
||||
|
||||
|
||||
class LineItem:
|
||||
weight = Quantity()
|
||||
price = Quantity()
|
||||
|
||||
def __init__(self, description, weight, price):
|
||||
self.description = description
|
||||
self.weight = weight
|
||||
self.price = price
|
||||
|
||||
def subtotal(self):
|
||||
return self.weight * self.price
|
||||
@@ -1,65 +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 descriptors are stored in
|
||||
alternate attributes, created by the descriptors in each ``LineItem``
|
||||
instance::
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> dir(raisins) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
|
||||
['_Quantity#0', '_Quantity#1', '__class__', ...
|
||||
'description', 'price', 'subtotal', 'weight']
|
||||
>>> getattr(raisins, '_Quantity#0')
|
||||
10
|
||||
>>> getattr(raisins, '_Quantity#1')
|
||||
6.95
|
||||
|
||||
If the descriptor is accessed in the class, the descriptor object is
|
||||
returned:
|
||||
|
||||
>>> LineItem.weight # doctest: +ELLIPSIS
|
||||
<model_v4c.Quantity object at 0x...>
|
||||
>>> LineItem.weight.storage_name
|
||||
'_Quantity#0'
|
||||
|
||||
|
||||
"""
|
||||
|
||||
# BEGIN LINEITEM_V4C
|
||||
import model_v4c as model # <1>
|
||||
|
||||
|
||||
class LineItem:
|
||||
weight = model.Quantity() # <2>
|
||||
price = model.Quantity()
|
||||
|
||||
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_V4C
|
||||
@@ -1,74 +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 descriptors are stored in
|
||||
alternate attributes, created by the descriptors in each ``LineItem``
|
||||
instance::
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> dir(raisins) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
|
||||
[... '_quantity:0', '_quantity:1', 'description',
|
||||
'price', 'subtotal', 'weight']
|
||||
>>> getattr(raisins, '_quantity:0')
|
||||
10
|
||||
>>> getattr(raisins, '_quantity:1')
|
||||
6.95
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# BEGIN LINEITEM_V4_PROP
|
||||
def quantity(): # <1>
|
||||
try:
|
||||
quantity.counter += 1 # <2>
|
||||
except AttributeError:
|
||||
quantity.counter = 0 # <3>
|
||||
|
||||
storage_name = '_{}:{}'.format('quantity', quantity.counter) # <4>
|
||||
|
||||
def qty_getter(instance): # <5>
|
||||
return getattr(instance, storage_name)
|
||||
|
||||
def qty_setter(instance, value):
|
||||
if value > 0:
|
||||
setattr(instance, storage_name, value)
|
||||
else:
|
||||
raise ValueError('value must be > 0')
|
||||
|
||||
return property(qty_getter, qty_setter)
|
||||
# END LINEITEM_V4_PROP
|
||||
|
||||
class LineItem:
|
||||
weight = quantity()
|
||||
price = quantity()
|
||||
|
||||
def __init__(self, description, weight, price):
|
||||
self.description = description
|
||||
self.weight = weight
|
||||
self.price = price
|
||||
|
||||
def subtotal(self):
|
||||
return self.weight * self.price
|
||||
|
||||
@@ -1,79 +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 descriptors are stored in
|
||||
alternate attributes, created by the descriptors in each ``LineItem``
|
||||
instance::
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> dir(raisins) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
|
||||
['_NonBlank#0', '_Quantity#0', '_Quantity#1', '__class__', ...
|
||||
'description', 'price', 'subtotal', 'weight']
|
||||
>>> getattr(raisins, '_Quantity#0')
|
||||
10
|
||||
>>> getattr(raisins, '_NonBlank#0')
|
||||
'Golden raisins'
|
||||
|
||||
If the descriptor is accessed in the class, the descriptor object is
|
||||
returned:
|
||||
|
||||
>>> LineItem.weight # doctest: +ELLIPSIS
|
||||
<model_v5.Quantity object at 0x...>
|
||||
>>> LineItem.weight.storage_name
|
||||
'_Quantity#0'
|
||||
|
||||
The `NonBlank` descriptor prevents empty or blank strings to be used
|
||||
for the description:
|
||||
|
||||
>>> br_nuts = LineItem('Brazil Nuts', 10, 34.95)
|
||||
>>> br_nuts.description = ' '
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value cannot be empty or blank
|
||||
>>> void = LineItem('', 1, 1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value cannot be empty or blank
|
||||
|
||||
|
||||
"""
|
||||
|
||||
# BEGIN LINEITEM_V5
|
||||
import model_v5 as model # <1>
|
||||
|
||||
|
||||
class LineItem:
|
||||
description = model.NonBlank() # <2>
|
||||
weight = model.Quantity()
|
||||
price = model.Quantity()
|
||||
|
||||
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_V5
|
||||
@@ -1,85 +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; -20 is not valid.
|
||||
|
||||
No change was made::
|
||||
|
||||
>>> raisins.weight
|
||||
10
|
||||
|
||||
The value of the attributes managed by the descriptors are stored in
|
||||
alternate attributes, created by the descriptors in each ``LineItem``
|
||||
instance::
|
||||
|
||||
>>> raisins = LineItem('Golden raisins', 10, 6.95)
|
||||
>>> dir(raisins) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
|
||||
['_Check#0', '_Check#1', '_Check#2', '__class__', ...
|
||||
'description', 'price', 'subtotal', 'weight']
|
||||
>>> [getattr(raisins, name) for name in dir(raisins) if name.startswith('_Check#')]
|
||||
['Golden raisins', 10, 6.95]
|
||||
|
||||
If the descriptor is accessed in the class, the descriptor object is
|
||||
returned:
|
||||
|
||||
>>> LineItem.weight # doctest: +ELLIPSIS
|
||||
<model_v5_check.Check object at 0x...>
|
||||
>>> LineItem.weight.storage_name
|
||||
'_Check#1'
|
||||
|
||||
The `NonBlank` descriptor prevents empty or blank strings to be used
|
||||
for the description:
|
||||
|
||||
>>> br_nuts = LineItem('Brazil Nuts', 10, 34.95)
|
||||
>>> br_nuts.description = ' '
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: ' ' is not valid.
|
||||
>>> void = LineItem('', 1, 1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: '' is not valid.
|
||||
|
||||
|
||||
|
||||
"""
|
||||
|
||||
import model_v5_check as model
|
||||
|
||||
def gt_zero(x):
|
||||
'''value must be > 0'''
|
||||
return x if x > 0 else model.INVALID
|
||||
|
||||
def non_blank(txt):
|
||||
txt = txt.strip()
|
||||
return txt if txt else model.INVALID
|
||||
|
||||
|
||||
class LineItem:
|
||||
description = model.Check(non_blank)
|
||||
weight = model.Check(gt_zero)
|
||||
price = model.Check(gt_zero)
|
||||
|
||||
def __init__(self, description, weight, price):
|
||||
self.description = description
|
||||
self.weight = weight
|
||||
self.price = price
|
||||
|
||||
def subtotal(self):
|
||||
return self.weight * self.price
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
# BEGIN MODEL_V4
|
||||
class Quantity:
|
||||
__counter = 0
|
||||
|
||||
def __init__(self):
|
||||
cls = self.__class__
|
||||
prefix = cls.__name__
|
||||
index = cls.__counter
|
||||
self.storage_name = '_{}#{}'.format(prefix, index)
|
||||
cls.__counter += 1
|
||||
|
||||
def __get__(self, instance, owner):
|
||||
if instance is None:
|
||||
return self
|
||||
else:
|
||||
return getattr(instance, self.storage_name)
|
||||
|
||||
def __set__(self, instance, value):
|
||||
if value > 0:
|
||||
setattr(instance, self.storage_name, value)
|
||||
else:
|
||||
raise ValueError('value must be > 0')
|
||||
# END MODEL_V4
|
||||
@@ -1,54 +0,0 @@
|
||||
# BEGIN MODEL_V5
|
||||
import abc
|
||||
|
||||
|
||||
class AutoStorage: # <1>
|
||||
__counter = 0
|
||||
|
||||
def __init__(self):
|
||||
cls = self.__class__
|
||||
prefix = cls.__name__
|
||||
index = cls.__counter
|
||||
self.storage_name = '_{}#{}'.format(prefix, index)
|
||||
cls.__counter += 1
|
||||
|
||||
def __get__(self, instance, owner):
|
||||
if instance is None:
|
||||
return self
|
||||
else:
|
||||
return getattr(instance, self.storage_name)
|
||||
|
||||
def __set__(self, instance, value):
|
||||
setattr(instance, self.storage_name, value) # <2>
|
||||
|
||||
|
||||
class Validated(abc.ABC, AutoStorage): # <3>
|
||||
|
||||
def __set__(self, instance, value):
|
||||
value = self.validate(instance, value) # <4>
|
||||
super().__set__(instance, value) # <5>
|
||||
|
||||
@abc.abstractmethod
|
||||
def validate(self, instance, value): # <6>
|
||||
"""return validated value or raise ValueError"""
|
||||
|
||||
|
||||
class Quantity(Validated): # <7>
|
||||
"""a number greater than zero"""
|
||||
|
||||
def validate(self, instance, value):
|
||||
if value <= 0:
|
||||
raise ValueError('value must be > 0')
|
||||
return value
|
||||
|
||||
|
||||
class NonBlank(Validated):
|
||||
"""a string with at least one non-space character"""
|
||||
|
||||
def validate(self, instance, value):
|
||||
value = value.strip()
|
||||
if len(value) == 0:
|
||||
raise ValueError('value cannot be empty or blank')
|
||||
return value # <8>
|
||||
|
||||
# END MODEL_V5
|
||||
@@ -1,52 +0,0 @@
|
||||
import abc
|
||||
|
||||
|
||||
class AutoStorage:
|
||||
__counter = 0
|
||||
|
||||
def __init__(self):
|
||||
cls = self.__class__
|
||||
prefix = cls.__name__
|
||||
index = cls.__counter
|
||||
self.storage_name = '_{}#{}'.format(prefix, index)
|
||||
cls.__counter += 1
|
||||
|
||||
def __get__(self, instance, owner):
|
||||
if instance is None:
|
||||
return self
|
||||
else:
|
||||
return getattr(instance, self.storage_name)
|
||||
|
||||
def __set__(self, instance, value):
|
||||
setattr(instance, self.storage_name, value)
|
||||
|
||||
|
||||
class Validated(abc.ABC, AutoStorage):
|
||||
|
||||
def __set__(self, instance, value):
|
||||
value = self.validate(instance, value)
|
||||
super().__set__(instance, value)
|
||||
|
||||
@abc.abstractmethod
|
||||
def validate(self, instance, value):
|
||||
"""return validated value or raise ValueError"""
|
||||
|
||||
INVALID = object()
|
||||
|
||||
class Check(Validated):
|
||||
|
||||
def __init__(self, checker):
|
||||
super().__init__()
|
||||
self.checker = checker
|
||||
if checker.__doc__ is None:
|
||||
doc = ''
|
||||
else:
|
||||
doc = checker.__doc__ + '; '
|
||||
self.message = doc + '{!r} is not valid.'
|
||||
|
||||
|
||||
def validate(self, instance, value):
|
||||
result = self.checker(value)
|
||||
if result is INVALID:
|
||||
raise ValueError(self.message.format(value))
|
||||
return result
|
||||
Reference in New Issue
Block a user