update from atlas
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
def deco_alpha(func):
|
||||
print('<<100>> deco_alpha')
|
||||
|
||||
def inner_alpha():
|
||||
print('<<200>> deco_alpha')
|
||||
func()
|
||||
|
||||
return inner_alpha
|
||||
|
||||
|
||||
def deco_beta(cls):
|
||||
print('<<300>> deco_beta')
|
||||
|
||||
def inner_beta(self):
|
||||
print('<<400>> inner_beta')
|
||||
print("result from 'deco_beta::inner_beta'")
|
||||
|
||||
cls.method3 = inner_beta
|
||||
return cls
|
||||
|
||||
|
||||
print('<<500>> evaldecos mudule body')
|
||||
25
21-class-metaprog/evalsupport.py
Normal file
25
21-class-metaprog/evalsupport.py
Normal file
@@ -0,0 +1,25 @@
|
||||
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
|
||||
|
||||
# BEGIN META_ALEPH
|
||||
class MetaAleph(type):
|
||||
print('<[400]> MetaAleph body')
|
||||
|
||||
def __init__(self, name, bases, dic):
|
||||
print('<[500]> MetaAleph.__init__')
|
||||
|
||||
def inner_2(self):
|
||||
print('<[600]> MetaAleph.__init__:inner_2')
|
||||
|
||||
self.method_z = inner_2
|
||||
# END META_ALEPH
|
||||
|
||||
print('<[700]> evalsupport module end')
|
||||
@@ -1,92 +1,49 @@
|
||||
import atexit
|
||||
from evalsupport import deco_alpha
|
||||
|
||||
from evaldecos import deco_alpha
|
||||
from evaldecos import deco_beta
|
||||
print('<[1]> evaltime module start')
|
||||
|
||||
|
||||
print('<<1>> start')
|
||||
class ClassOne():
|
||||
print('<[2]> ClassOne body')
|
||||
|
||||
def __init__(self):
|
||||
print('<[3]> ClassOne.__init__')
|
||||
|
||||
func_A = lambda: print('<<2>> func_A')
|
||||
def __del__(self):
|
||||
print('<[4]> ClassOne.__del__')
|
||||
|
||||
def method_x(self):
|
||||
print('<[5]> ClassOne.method_x')
|
||||
|
||||
def func_B():
|
||||
print('<<3>> func_B')
|
||||
|
||||
def func_C():
|
||||
print('<<4>> func_C')
|
||||
|
||||
return func_C
|
||||
class ClassTwo(object):
|
||||
print('<[6]> ClassTwo body')
|
||||
|
||||
|
||||
@deco_alpha
|
||||
def func_D():
|
||||
print('<<6>> func_D')
|
||||
class ClassThree():
|
||||
print('<[7]> ClassThree body')
|
||||
|
||||
def method_y(self):
|
||||
print('<[8]> ClassThree.method_y')
|
||||
|
||||
|
||||
def func_E():
|
||||
print('<<7>> func_E')
|
||||
class ClassFour(ClassThree):
|
||||
print('<[9]> ClassFour body')
|
||||
|
||||
|
||||
class ClassOne(object):
|
||||
print('<<7>> ClassOne body')
|
||||
|
||||
def __init__(self):
|
||||
print('<<8>> ClassOne.__init__')
|
||||
|
||||
def __del__(self):
|
||||
print('<<9>> ClassOne.__del__')
|
||||
|
||||
def method1(self):
|
||||
print('<<10>> ClassOne.method')
|
||||
return "result from 'ClassOne.method1'"
|
||||
|
||||
class ClassTwo(object):
|
||||
print('<<11>> ClassTwo body')
|
||||
|
||||
|
||||
@deco_beta
|
||||
class ClassThree(ClassOne):
|
||||
print('<<12>> ClassThree body')
|
||||
|
||||
def method3(self):
|
||||
print('<<13>> ClassOne.method')
|
||||
return "result from 'ClassThree.method3'"
|
||||
|
||||
|
||||
if True:
|
||||
print("<<14>> 'if True'")
|
||||
|
||||
|
||||
if False:
|
||||
print("<<15>> 'if False'")
|
||||
|
||||
|
||||
atexit.register(func_E)
|
||||
|
||||
print("<<16>> right before 'if ... __main__'")
|
||||
def method_y(self):
|
||||
print('<[10]> ClassFour.method_y')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('<<17>> start __main__ block')
|
||||
print(func_A)
|
||||
print(func_A())
|
||||
print('<<18>> continue __main__ block')
|
||||
print(func_B)
|
||||
b_result = func_B()
|
||||
print(b_result)
|
||||
print('<[11]> ClassOne tests', 30 * '.')
|
||||
one = ClassOne()
|
||||
one.method1()
|
||||
b_result()
|
||||
print(func_D)
|
||||
func_D()
|
||||
one.method_x()
|
||||
print('<[12]> ClassThree tests', 30 * '.')
|
||||
three = ClassThree()
|
||||
three.method3()
|
||||
|
||||
class ClassFour(object):
|
||||
print('<<19>> ClasFour body')
|
||||
|
||||
print('<<20>> end __main__ block')
|
||||
three.method_y()
|
||||
print('<[13]> ClassFour tests', 30 * '.')
|
||||
four = ClassFour()
|
||||
four.method_y()
|
||||
|
||||
|
||||
print('<<21>> The End')
|
||||
print('<[14]> evaltime module end')
|
||||
|
||||
53
21-class-metaprog/evaltime_meta.py
Normal file
53
21-class-metaprog/evaltime_meta.py
Normal file
@@ -0,0 +1,53 @@
|
||||
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')
|
||||
@@ -9,12 +9,12 @@ record_factory: create simple classes just for holding data fields
|
||||
>>> name, weight, _ = rex # <3>
|
||||
>>> name, weight
|
||||
('Rex', 30)
|
||||
>>> "{2}'s dog weights {1}kg".format(*rex)
|
||||
>>> "{2}'s dog weights {1}kg".format(*rex) # <4>
|
||||
"Bob's dog weights 30kg"
|
||||
>>> rex.weight = 32 # <4>
|
||||
>>> rex.weight = 32 # <5>
|
||||
>>> rex
|
||||
Dog(name='Rex', weight=32, owner='Bob')
|
||||
>>> Dog.__mro__ # <5>
|
||||
>>> Dog.__mro__ # <6>
|
||||
(<class 'factories.Dog'>, <class 'object'>)
|
||||
|
||||
# END RECORD_FACTORY_DEMO
|
||||
@@ -22,29 +22,29 @@ record_factory: create simple classes just for holding data fields
|
||||
|
||||
# BEGIN RECORD_FACTORY
|
||||
def record_factory(cls_name, field_names):
|
||||
if isinstance(field_names, str): # <1>
|
||||
if isinstance(field_names, str):
|
||||
field_names = field_names.replace(',', ' ').split()
|
||||
__slots__ = tuple(field_names)
|
||||
field_names = tuple(field_names) # <1>
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
def __init__(self, *args, **kwargs): # <2>
|
||||
attrs = dict(zip(self.__slots__, args))
|
||||
attrs.update(kwargs)
|
||||
for name, value in attrs.items():
|
||||
setattr(self, name, value)
|
||||
|
||||
def __iter__(self):
|
||||
def __iter__(self): # <3>
|
||||
for name in self.__slots__:
|
||||
yield getattr(self, name)
|
||||
|
||||
def __repr__(self):
|
||||
def __repr__(self): # <4>
|
||||
values = ', '.join('{}={!r}'.format(*i) for i
|
||||
in zip(self.__slots__, self))
|
||||
return '{}({})'.format(self.__class__.__name__, values)
|
||||
|
||||
cls_attrs = dict(__slots__ = __slots__,
|
||||
__init__ = __init__,
|
||||
__iter__ = __iter__,
|
||||
__repr__ = __repr__)
|
||||
cls_attrs = dict(__slots__ = field_names, # <5>
|
||||
__init__ = __init__,
|
||||
__iter__ = __iter__,
|
||||
__repr__ = __repr__)
|
||||
|
||||
return type(cls_name, (object,), cls_attrs)
|
||||
return type(cls_name, (object,), cls_attrs) # <6>
|
||||
# END RECORD_FACTORY
|
||||
|
||||
Reference in New Issue
Block a user