update from atlas

This commit is contained in:
Luciano Ramalho
2015-03-22 17:01:13 -03:00
parent 8fb691d3c7
commit 290079ec9a
16 changed files with 803 additions and 2 deletions

View File

@@ -0,0 +1,22 @@
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')

View File

@@ -0,0 +1,92 @@
import atexit
from evaldecos import deco_alpha
from evaldecos import deco_beta
print('<<1>> start')
func_A = lambda: print('<<2>> func_A')
def func_B():
print('<<3>> func_B')
def func_C():
print('<<4>> func_C')
return func_C
@deco_alpha
def func_D():
print('<<6>> func_D')
def func_E():
print('<<7>> func_E')
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__'")
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)
one = ClassOne()
one.method1()
b_result()
print(func_D)
func_D()
three = ClassThree()
three.method3()
class ClassFour(object):
print('<<19>> ClasFour body')
print('<<20>> end __main__ block')
print('<<21>> The End')

View File

@@ -0,0 +1,50 @@
"""
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 weights {1}kg".format(*rex)
"Bob's dog weights 30kg"
>>> rex.weight = 32 # <4>
>>> rex
Dog(name='Rex', weight=32, owner='Bob')
>>> Dog.__mro__ # <5>
(<class 'factories.Dog'>, <class 'object'>)
# END RECORD_FACTORY_DEMO
"""
# BEGIN RECORD_FACTORY
def record_factory(cls_name, field_names):
if isinstance(field_names, str): # <1>
field_names = field_names.replace(',', ' ').split()
__slots__ = tuple(field_names)
def __init__(self, *args, **kwargs):
attrs = dict(zip(self.__slots__, args))
attrs.update(kwargs)
for name, value in attrs.items():
setattr(self, name, value)
def __iter__(self):
for name in self.__slots__:
yield getattr(self, name)
def __repr__(self):
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__)
return type(cls_name, (object,), cls_attrs)
# END RECORD_FACTORY