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()

其他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  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 !这是什么好东西!!
本文转自博客园刘凯毅的博客,原文链接: python aop (metaclass),如需转载请自行联系原博主。
目录
相关文章
|
3月前
|
Python
Python 元类 metaclass 详解
Python 元类 metaclass 详解
22 0
|
2月前
|
Python
解释Python中的元类(Metaclass)是什么,如何使用元类?
解释Python中的元类(Metaclass)是什么,如何使用元类?
13 0
|
3月前
|
Python
Python 元类 metaclass 详解
- 元类是类的类,用于控制类实例的创建过程和初始化过程。 - `__new__` 用于创建类实例,`__init__` 用于初始化实例。 - 元类的主要作用是允许在类被创建之前修改它,如添加属性、修改行为。 - 元类适用于对类创建过程进行深度干预的特殊需求,对于大多数情况,普通类定义和继承已经足够,元类提供额外的灵活性。
26 0
|
4月前
|
Python
Python 高级主题:什么是元类(Metaclass)?在 Python 中如何使用元类?
Python 高级主题:什么是元类(Metaclass)?在 Python 中如何使用元类?
|
7月前
|
Rust Go Swift
Python metaclass 的原理和应用(下)
Python metaclass 的原理和应用
56 0
|
7月前
|
Python
Python metaclass 的原理和应用(上)
Python metaclass 的原理和应用
38 0
|
10月前
|
存储 Python
Python 元类(metaclass)
Python面向对象编程的高级知识点——元类(metaclass)
37 0
|
SQL 数据库 Python
Python编程:metaclass元类实现简单的ORM
Python编程:metaclass元类实现简单的ORM
180 0