update from atlas

This commit is contained in:
Luciano Ramalho
2015-03-31 16:53:46 -03:00
parent 290079ec9a
commit 104b2c9d2d
24 changed files with 674 additions and 205 deletions

View File

@@ -16,7 +16,7 @@ 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.
ValueError: value must be > 0
No change was made::
@@ -27,49 +27,52 @@ The value of the attributes managed by the descriptors are stored in
alternate attributes, created by the descriptors in each ``LineItem``
instance::
# BEGIN LINEITEM_V6_DEMO
>>> 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]
>>> dir(raisins)[:3]
['_NonBlank#description', '_Quantity#price', '_Quantity#weight']
>>> LineItem.description.storage_name
'_NonBlank#description'
>>> raisins.description
'Golden raisins'
>>> getattr(raisins, '_NonBlank#description')
'Golden raisins'
# END LINEITEM_V6_DEMO
If the descriptor is accessed in the class, the descriptor object is
returned:
>>> LineItem.price # doctest: +ELLIPSIS
<model_v6.Check object at 0x...>
>>> br_nuts = LineItem('Brazil nuts', 10, 34.95)
>>> br_nuts.price
34.95
>>> LineItem.weight # doctest: +ELLIPSIS
<model_v6.Quantity object at 0x...>
>>> LineItem.weight.storage_name
'_Quantity#weight'
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.
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_v6 as model # <1>
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
# BEGIN LINEITEM_V6
import model_v6 as model
@model.entity # <1>
class LineItem:
description = model.Check(non_blank) # <2>
weight = model.Check(gt_zero)
price = model.Check(gt_zero)
description = model.NonBlank()
weight = model.Quantity()
price = model.Quantity()
def __init__(self, description, weight, price):
self.description = description
@@ -78,4 +81,4 @@ class LineItem:
def subtotal(self):
return self.weight * self.price
# END LINEITEM_V5
# END LINEITEM_V6