metaprogramming examples
This commit is contained in:
@@ -60,4 +60,4 @@ class LineItem:
|
||||
self.__weight = value # <6>
|
||||
else:
|
||||
raise ValueError('value must be > 0') # <7>
|
||||
# END LINEITEM_V2
|
||||
# END LINEITEM_V2
|
||||
|
||||
@@ -33,6 +33,7 @@ No change was made::
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# BEGIN LINEITEM_V3
|
||||
class Quantity: # <1>
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ No change was made::
|
||||
|
||||
"""
|
||||
|
||||
|
||||
class Quantity:
|
||||
|
||||
def __init__(self, storage_name):
|
||||
|
||||
@@ -45,23 +45,23 @@ class Quantity:
|
||||
|
||||
def __init__(self):
|
||||
cls = self.__class__ # <2>
|
||||
prefix = cls.__name__ # <3>
|
||||
index = cls.__counter # <4>
|
||||
self.storage_name = '_{}_{}'.format(prefix, index) # <5>
|
||||
cls.__counter += 1 # <6>
|
||||
prefix = cls.__name__
|
||||
index = cls.__counter
|
||||
self.storage_name = '_{}_{}'.format(prefix, index) # <3>
|
||||
cls.__counter += 1 # <4>
|
||||
|
||||
def __get__(self, instance, owner): # <7>
|
||||
return getattr(instance, self.storage_name) # <8>
|
||||
def __get__(self, instance, owner): # <5>
|
||||
return getattr(instance, self.storage_name) # <6>
|
||||
|
||||
def __set__(self, instance, value): # <9>
|
||||
def __set__(self, instance, value):
|
||||
if value > 0:
|
||||
setattr(instance, self.storage_name, value) # <10>
|
||||
setattr(instance, self.storage_name, value) # <7>
|
||||
else:
|
||||
raise ValueError('value must be > 0')
|
||||
|
||||
|
||||
class LineItem:
|
||||
weight = Quantity() # <11>
|
||||
weight = Quantity() # <8>
|
||||
price = Quantity()
|
||||
|
||||
def __init__(self, description, weight, price):
|
||||
@@ -71,4 +71,4 @@ class LineItem:
|
||||
|
||||
def subtotal(self):
|
||||
return self.weight * self.price
|
||||
# END LINEITEM_V4
|
||||
# END LINEITEM_V4
|
||||
|
||||
86
descriptors/bulkfood_v4b.py
Normal file
86
descriptors/bulkfood_v4b.py
Normal file
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
|
||||
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']
|
||||
>>> raisins._Quantity_0
|
||||
10
|
||||
>>> raisins._Quantity_1
|
||||
6.95
|
||||
|
||||
If the descriptor is accessed in the class, the descriptor object is
|
||||
returned:
|
||||
|
||||
>>> LineItem.price # doctest: +ELLIPSIS
|
||||
<bulkfood_v4b.Quantity object at 0x...>
|
||||
>>> br_nuts = LineItem('Brazil nuts', 10, 34.95)
|
||||
>>> br_nuts.price
|
||||
34.95
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# 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
|
||||
65
descriptors/bulkfood_v4c.py
Normal file
65
descriptors/bulkfood_v4c.py
Normal file
@@ -0,0 +1,65 @@
|
||||
"""
|
||||
|
||||
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']
|
||||
>>> raisins._Quantity_0
|
||||
10
|
||||
>>> raisins._Quantity_1
|
||||
6.95
|
||||
|
||||
If the descriptor is accessed in the class, the descriptor object is
|
||||
returned:
|
||||
|
||||
>>> LineItem.price # doctest: +ELLIPSIS
|
||||
<model_v4c.Quantity object at 0x...>
|
||||
>>> br_nuts = LineItem('Brazil nuts', 10, 34.95)
|
||||
>>> br_nuts.price
|
||||
34.95
|
||||
|
||||
"""
|
||||
|
||||
# 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
|
||||
23
descriptors/model_v4c.py
Normal file
23
descriptors/model_v4c.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# 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
|
||||
Reference in New Issue
Block a user