python aop (metaclass)
代码可以直接运行,看结果
如果命令调试 python -m pdb pyaop.py
(Pdb)b pyaop:10
(Pdb)c
(Pdb)n .....自己来把
调试参考 : python pdb 基础调试
源文件 : pyaop.py
#
!/usr/bin/python
# -*- coding: utf8 -*-
# 参考:http://www.cnblogs.com/Alexander-Lee/archive/2008/12/06/pythonaop.html
"""
py aop 代理类 ( metaclass 特性 )
由于使用 __metaclass__ = <type 'type'>
pyaop 继承 type
"""
class pyaop(type):
# before ; after 方法变量引用声明
beforeop = lambda e : None
afterop = lambda e : None
# class方法(静态方法)set
@classmethod
def setbefore(self,func):
pyaop.beforeop = func
@classmethod
def setafter(self,func):
pyaop.afterop = func
""" 使用调试
# python -m pdb pyaop.py
# 由下面 A类 < __metaclass__ = pyaop >
# 类初始 的 __new__ 指向 pyaop __new__
#
# (Pdb)b pyaop:36 (大概就是下面函数form types 的行号)
# (Pdb)a (可以看看调试中,各参数的值,注意dict为A的初始对象传过来了)
# mcl = <class '__main__.pyaop'>
# name = A
# bases = (<type 'object'>,)
# dict = {'__module__': '__main__', 'foo': <function foo at 0x7fddced4>, '__metaclass__': <class '__main__.pyaop'>, 'foo2': <function foo2 at 0x7fddcf0c>}
# 本函数目的: 使用 新的另个对象挂载 被aop后的 A对象 方法
"""
def __new__ (mcl,name,bases,dict):
from types import FunctionType
obj = object()
def aop(func):
def wrapper( * args, ** kwds):
pyaop.beforeop(obj)
value = func( * args, ** kwds)
pyaop.afterop(obj)
return value
return wrapper
# 添加代理
for attr, value in dict.iteritems():
if isinstance(value, FunctionType):
dict[attr] = aop(value)
# 挂载到 obj 上
obj = super(pyaop, mcl). __new__ (mcl, name, bases, dict)
return obj
class A(object):
# 被 aop 代理 声明!
__metaclass__ = pyaop
def foo(self):
total = 0
for i in range( 100000 ):
total = total + 1
print total
def foo2(self):
from time import sleep
total = 0
for i in range( 100000 ):
total = total + 1
# sleep(0.0001)
print total
""" #####################################################################################
# 测试
##################################################################################### """
def beforep(self):
print ( ' before ' )
def afterp(self):
print ( ' after ' )
if __name__ == " __main__ " :
pyaop.setbefore(beforep)
pyaop.setafter(afterp)
a = A()
a.foo()
a.foo2()
# -*- coding: utf8 -*-
# 参考:http://www.cnblogs.com/Alexander-Lee/archive/2008/12/06/pythonaop.html
"""
py aop 代理类 ( metaclass 特性 )
由于使用 __metaclass__ = <type 'type'>
pyaop 继承 type
"""
class pyaop(type):
# before ; after 方法变量引用声明
beforeop = lambda e : None
afterop = lambda e : None
# class方法(静态方法)set
@classmethod
def setbefore(self,func):
pyaop.beforeop = func
@classmethod
def setafter(self,func):
pyaop.afterop = func
""" 使用调试
# python -m pdb pyaop.py
# 由下面 A类 < __metaclass__ = pyaop >
# 类初始 的 __new__ 指向 pyaop __new__
#
# (Pdb)b pyaop:36 (大概就是下面函数form types 的行号)
# (Pdb)a (可以看看调试中,各参数的值,注意dict为A的初始对象传过来了)
# mcl = <class '__main__.pyaop'>
# name = A
# bases = (<type 'object'>,)
# dict = {'__module__': '__main__', 'foo': <function foo at 0x7fddced4>, '__metaclass__': <class '__main__.pyaop'>, 'foo2': <function foo2 at 0x7fddcf0c>}
# 本函数目的: 使用 新的另个对象挂载 被aop后的 A对象 方法
"""
def __new__ (mcl,name,bases,dict):
from types import FunctionType
obj = object()
def aop(func):
def wrapper( * args, ** kwds):
pyaop.beforeop(obj)
value = func( * args, ** kwds)
pyaop.afterop(obj)
return value
return wrapper
# 添加代理
for attr, value in dict.iteritems():
if isinstance(value, FunctionType):
dict[attr] = aop(value)
# 挂载到 obj 上
obj = super(pyaop, mcl). __new__ (mcl, name, bases, dict)
return obj
class A(object):
# 被 aop 代理 声明!
__metaclass__ = pyaop
def foo(self):
total = 0
for i in range( 100000 ):
total = total + 1
print total
def foo2(self):
from time import sleep
total = 0
for i in range( 100000 ):
total = total + 1
# sleep(0.0001)
print total
""" #####################################################################################
# 测试
##################################################################################### """
def beforep(self):
print ( ' before ' )
def afterp(self):
print ( ' after ' )
if __name__ == " __main__ " :
pyaop.setbefore(beforep)
pyaop.setafter(afterp)
a = A()
a.foo()
a.foo2()
其他aop:
使用 @
def
addspam(fn):
def new( * args):
print " spam, spam, spam "
return fn( * args)
return new
@addspam
def useful(a, b):
print a ** 2 + b ** 2
useful( 3 , 4 )
# 结果
# spam, spam, spam
# 25
def new( * args):
print " spam, spam, spam "
return fn( * args)
return new
@addspam
def useful(a, b):
print a ** 2 + b ** 2
useful( 3 , 4 )
# 结果
# spam, spam, spam
# 25
晚绑定!
def
attrs(
**
kwds):
def decorate(f):
for k in kwds:
setattr(f, k, kwds[k])
return f
return decorate
@attrs(versionadded = " 2.2 " ,author = " Guido van Rossum " )
def mymethod(f):
return mymethod
x = mymethod( 1 )
x.versionadded
# 2.2 !这是什么好东西!!
def decorate(f):
for k in kwds:
setattr(f, k, kwds[k])
return f
return decorate
@attrs(versionadded = " 2.2 " ,author = " Guido van Rossum " )
def mymethod(f):
return mymethod
x = mymethod( 1 )
x.versionadded
# 2.2 !这是什么好东西!!
整理 www.blogjava.net/Good-Game