removed 1st edition code
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
Sample code for Chapter 21 - "Class metaprogramming"
|
||||
|
||||
From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015)
|
||||
http://shop.oreilly.com/product/0636920032519.do
|
||||
@@ -1,84 +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::
|
||||
|
||||
# BEGIN LINEITEM_V6_DEMO
|
||||
>>> raisins = LineItem('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.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: value cannot be empty or blank
|
||||
>>> void = LineItem('', 1, 1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value cannot be empty or blank
|
||||
|
||||
|
||||
"""
|
||||
|
||||
# BEGIN LINEITEM_V6
|
||||
import model_v6 as model
|
||||
|
||||
@model.entity # <1>
|
||||
class LineItem:
|
||||
description = model.NonBlank()
|
||||
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_V6
|
||||
@@ -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)[:3]
|
||||
['_NonBlank#description', '_Quantity#price', '_Quantity#weight']
|
||||
>>> LineItem.description.storage_name
|
||||
'_NonBlank#description'
|
||||
>>> raisins.description
|
||||
'Golden raisins'
|
||||
>>> getattr(raisins, '_NonBlank#description')
|
||||
'Golden raisins'
|
||||
|
||||
If the descriptor is accessed in the class, the descriptor object is
|
||||
returned:
|
||||
|
||||
>>> LineItem.weight # doctest: +ELLIPSIS
|
||||
<model_v7.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: value cannot be empty or blank
|
||||
>>> void = LineItem('', 1, 1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value cannot be empty or blank
|
||||
|
||||
"""
|
||||
|
||||
# BEGIN LINEITEM_V7
|
||||
import model_v7 as model
|
||||
|
||||
class LineItem(model.Entity): # <1>
|
||||
description = model.NonBlank()
|
||||
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_V7
|
||||
@@ -1,86 +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
|
||||
|
||||
>>> raisins = LineItem('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'
|
||||
|
||||
If the descriptor is accessed in the class, the descriptor object is
|
||||
returned:
|
||||
|
||||
>>> LineItem.weight # doctest: +ELLIPSIS
|
||||
<model_v8.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: value cannot be empty or blank
|
||||
>>> void = LineItem('', 1, 1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: value cannot be empty or blank
|
||||
|
||||
|
||||
Fields can be retrieved in the order they were declared:
|
||||
|
||||
# BEGIN LINEITEM_V8_DEMO
|
||||
>>> for name in LineItem.field_names():
|
||||
... print(name)
|
||||
...
|
||||
description
|
||||
weight
|
||||
price
|
||||
|
||||
# END LINEITEM_V8_DEMO
|
||||
|
||||
"""
|
||||
|
||||
import model_v8 as model
|
||||
|
||||
class LineItem(model.Entity):
|
||||
description = model.NonBlank()
|
||||
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
|
||||
@@ -1,60 +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"""
|
||||
|
||||
|
||||
class Quantity(Validated):
|
||||
"""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
|
||||
|
||||
# BEGIN MODEL_V6
|
||||
def entity(cls): # <1>
|
||||
for key, attr in cls.__dict__.items(): # <2>
|
||||
if isinstance(attr, Validated): # <3>
|
||||
type_name = type(attr).__name__
|
||||
attr.storage_name = '_{}#{}'.format(type_name, key) # <4>
|
||||
return cls # <5>
|
||||
# END MODEL_V6
|
||||
@@ -1,66 +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"""
|
||||
|
||||
|
||||
class Quantity(Validated):
|
||||
"""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
|
||||
|
||||
# BEGIN MODEL_V7
|
||||
class EntityMeta(type):
|
||||
"""Metaclass for business entities with validated fields"""
|
||||
|
||||
def __init__(cls, name, bases, attr_dict):
|
||||
super().__init__(name, bases, attr_dict) # <1>
|
||||
for key, attr in attr_dict.items(): # <2>
|
||||
if isinstance(attr, Validated):
|
||||
type_name = type(attr).__name__
|
||||
attr.storage_name = '_{}#{}'.format(type_name, key)
|
||||
|
||||
class Entity(metaclass=EntityMeta): # <3>
|
||||
"""Business entity with validated fields"""
|
||||
# END MODEL_V7
|
||||
@@ -1,80 +0,0 @@
|
||||
import abc
|
||||
import collections
|
||||
|
||||
|
||||
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"""
|
||||
|
||||
|
||||
class Quantity(Validated):
|
||||
"""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
|
||||
|
||||
# BEGIN MODEL_V8
|
||||
class EntityMeta(type):
|
||||
"""Metaclass for business entities with validated fields"""
|
||||
|
||||
@classmethod
|
||||
def __prepare__(cls, name, bases):
|
||||
return collections.OrderedDict() # <1>
|
||||
|
||||
def __init__(cls, name, bases, attr_dict):
|
||||
super().__init__(name, bases, attr_dict)
|
||||
cls._field_names = [] # <2>
|
||||
for key, attr in attr_dict.items(): # <3>
|
||||
if isinstance(attr, Validated):
|
||||
type_name = type(attr).__name__
|
||||
attr.storage_name = '_{}#{}'.format(type_name, key)
|
||||
cls._field_names.append(key) # <4>
|
||||
|
||||
|
||||
class Entity(metaclass=EntityMeta):
|
||||
"""Business entity with validated fields"""
|
||||
|
||||
@classmethod
|
||||
def field_names(cls): # <5>
|
||||
for name in cls._field_names:
|
||||
yield name
|
||||
|
||||
# END MODEL_V8
|
||||
@@ -1,25 +0,0 @@
|
||||
print('<[100]> evalsupport module start')
|
||||
|
||||
def deco_alpha(cls):
|
||||
print('<[200]> deco_alpha')
|
||||
|
||||
def inner_1(self):
|
||||
print('<[300]> deco_alpha:inner_1')
|
||||
|
||||
cls.method_y = inner_1
|
||||
return cls
|
||||
|
||||
|
||||
class MetaAleph(type):
|
||||
print('<[400]> MetaAleph body')
|
||||
|
||||
def __init__(cls, name, bases, dic):
|
||||
print('<[500]> MetaAleph.__init__')
|
||||
|
||||
def inner_2(self):
|
||||
print('<[600]> MetaAleph.__init__:inner_2')
|
||||
|
||||
cls.method_z = inner_2
|
||||
|
||||
|
||||
print('<[700]> evalsupport module end')
|
||||
@@ -1,49 +0,0 @@
|
||||
from evalsupport import deco_alpha
|
||||
|
||||
print('<[1]> evaltime module start')
|
||||
|
||||
|
||||
class ClassOne():
|
||||
print('<[2]> ClassOne body')
|
||||
|
||||
def __init__(self):
|
||||
print('<[3]> ClassOne.__init__')
|
||||
|
||||
def __del__(self):
|
||||
print('<[4]> ClassOne.__del__')
|
||||
|
||||
def method_x(self):
|
||||
print('<[5]> ClassOne.method_x')
|
||||
|
||||
class ClassTwo(object):
|
||||
print('<[6]> ClassTwo body')
|
||||
|
||||
|
||||
@deco_alpha
|
||||
class ClassThree():
|
||||
print('<[7]> ClassThree body')
|
||||
|
||||
def method_y(self):
|
||||
print('<[8]> ClassThree.method_y')
|
||||
|
||||
|
||||
class ClassFour(ClassThree):
|
||||
print('<[9]> ClassFour body')
|
||||
|
||||
def method_y(self):
|
||||
print('<[10]> ClassFour.method_y')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('<[11]> ClassOne tests', 30 * '.')
|
||||
one = ClassOne()
|
||||
one.method_x()
|
||||
print('<[12]> ClassThree tests', 30 * '.')
|
||||
three = ClassThree()
|
||||
three.method_y()
|
||||
print('<[13]> ClassFour tests', 30 * '.')
|
||||
four = ClassFour()
|
||||
four.method_y()
|
||||
|
||||
|
||||
print('<[14]> evaltime module end')
|
||||
@@ -1,53 +0,0 @@
|
||||
from evalsupport import deco_alpha
|
||||
from evalsupport import MetaAleph
|
||||
|
||||
print('<[1]> evaltime_meta module start')
|
||||
|
||||
|
||||
@deco_alpha
|
||||
class ClassThree():
|
||||
print('<[2]> ClassThree body')
|
||||
|
||||
def method_y(self):
|
||||
print('<[3]> ClassThree.method_y')
|
||||
|
||||
|
||||
class ClassFour(ClassThree):
|
||||
print('<[4]> ClassFour body')
|
||||
|
||||
def method_y(self):
|
||||
print('<[5]> ClassFour.method_y')
|
||||
|
||||
|
||||
class ClassFive(metaclass=MetaAleph):
|
||||
print('<[6]> ClassFive body')
|
||||
|
||||
def __init__(self):
|
||||
print('<[7]> ClassFive.__init__')
|
||||
|
||||
def method_z(self):
|
||||
print('<[8]> ClassFive.method_y')
|
||||
|
||||
|
||||
class ClassSix(ClassFive):
|
||||
print('<[9]> ClassSix body')
|
||||
|
||||
def method_z(self):
|
||||
print('<[10]> ClassSix.method_y')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('<[11]> ClassThree tests', 30 * '.')
|
||||
three = ClassThree()
|
||||
three.method_y()
|
||||
print('<[12]> ClassFour tests', 30 * '.')
|
||||
four = ClassFour()
|
||||
four.method_y()
|
||||
print('<[13]> ClassFive tests', 30 * '.')
|
||||
five = ClassFive()
|
||||
five.method_z()
|
||||
print('<[14]> ClassSix tests', 30 * '.')
|
||||
six = ClassSix()
|
||||
six.method_z()
|
||||
|
||||
print('<[15]> evaltime_meta module end')
|
||||
@@ -1,59 +0,0 @@
|
||||
"""
|
||||
record_factory: create simple classes just for holding data fields
|
||||
|
||||
# BEGIN RECORD_FACTORY_DEMO
|
||||
>>> Dog = record_factory('Dog', 'name weight owner') # <1>
|
||||
>>> rex = Dog('Rex', 30, 'Bob')
|
||||
>>> rex # <2>
|
||||
Dog(name='Rex', weight=30, owner='Bob')
|
||||
>>> name, weight, _ = rex # <3>
|
||||
>>> name, weight
|
||||
('Rex', 30)
|
||||
>>> "{2}'s dog weighs {1}kg".format(*rex) # <4>
|
||||
"Bob's dog weighs 30kg"
|
||||
>>> rex.weight = 32 # <5>
|
||||
>>> rex
|
||||
Dog(name='Rex', weight=32, owner='Bob')
|
||||
>>> Dog.__mro__ # <6>
|
||||
(<class 'factories.Dog'>, <class 'object'>)
|
||||
|
||||
# END RECORD_FACTORY_DEMO
|
||||
|
||||
The factory also accepts a list or tuple of identifiers:
|
||||
|
||||
>>> Dog = record_factory('Dog', ['name', 'weight', 'owner'])
|
||||
>>> Dog.__slots__
|
||||
('name', 'weight', 'owner')
|
||||
|
||||
"""
|
||||
|
||||
# BEGIN RECORD_FACTORY
|
||||
def record_factory(cls_name, field_names):
|
||||
try:
|
||||
field_names = field_names.replace(',', ' ').split() # <1>
|
||||
except AttributeError: # no .replace or .split
|
||||
pass # assume it's already a sequence of identifiers
|
||||
field_names = tuple(field_names) # <2>
|
||||
|
||||
def __init__(self, *args, **kwargs): # <3>
|
||||
attrs = dict(zip(self.__slots__, args))
|
||||
attrs.update(kwargs)
|
||||
for name, value in attrs.items():
|
||||
setattr(self, name, value)
|
||||
|
||||
def __iter__(self): # <4>
|
||||
for name in self.__slots__:
|
||||
yield getattr(self, name)
|
||||
|
||||
def __repr__(self): # <5>
|
||||
values = ', '.join('{}={!r}'.format(*i) for i
|
||||
in zip(self.__slots__, self))
|
||||
return '{}({})'.format(self.__class__.__name__, values)
|
||||
|
||||
cls_attrs = dict(__slots__ = field_names, # <6>
|
||||
__init__ = __init__,
|
||||
__iter__ = __iter__,
|
||||
__repr__ = __repr__)
|
||||
|
||||
return type(cls_name, (object,), cls_attrs) # <7>
|
||||
# END RECORD_FACTORY
|
||||
Reference in New Issue
Block a user