How can we implement Python AOP correctly and efficiently to help our program development? First, we need to use metaclass to implement this technical application. The specific operation steps will be presented here, hoping to help you.
In fact, it is very easy to implement AOP in a very dynamic language such as Python, so we should first define a metaclass
Then we need to dynamically implant the method to the front and back of the method to be called when _ new _) metaclass.
The specific Python AOP implementation code is as follows:
- _ Author __= "alex"
- _ Date _ = "$23:54:11 $"
- _ Name __= "pyAOP"
- '''
- This metaclass is the basis for Implementing AOP.
- '''
- Class pyAOP (type ):
- '''
- This empty method is used to initialize beforeop and afterop to function reference.
- '''
- Def nop (self ):
- Pass
- '''
- The following two variables are class variables, that is, the variables that store the addresses of the two functions to be implanted.
- '''
- Beforeop = nop
- Afterop = nop
- '''
- Set the class functions of the first and second embedded Functions
- '''
- @ Classmethod
- Def setbefore (self, func ):
- PyAOP. beforeop = func
- @ Classmethod
- Def setafter (self, func ):
- PyAOP. afterop = func
- '''
- Initialize the metaclass function, which is the fourth parameter,
Dict through this parameter we can modify the class attribute method)
- '''
- Def _ new _ (mcl, name, bases, dict ):
- From types import FunctionType # The funpes type of the load type module
- Obj = object () # define a variable of an empty object
- '''
- This is the method to be implanted. the func parameter is the function we want to call.
- '''
- Def AOP (func ):
- '''
- We use this function to replace the function to be called.
- '''
- Def wrapper (* args, ** kwds ):
- PyAOP. beforeop (obj) # Call the prefix Function
- Value = func (* args, ** kwds) # Call the function to be called
- PyAOP. afterop (obj) # Call the post Function
- Return value # return
- Return wrapper
- # Search for the function to be called in the member list of the class
- For attr, value in dict. iteritems ():
- If isinstance (value, FunctionType ):
- Dict [attr] = AOP (value) # Replace it with the function AOP after finding it
- Obj = super (pyAOP, mcl). _ new _ (mcl, name, bases, dict)
# Call _ new _ () of the parent class to create self
- Return obj
If we want to intercept A method call for Class A, this is the case:
- class A(object):
- __metaclass__ = pyaop
- def foo(self):
- total = 0
- for i in range(100000):
- totaltotal = total+1
- print total
- def foo2(self):
- from time import sleep
- total = 0
- for i in range(100000):
- totaltotal = total+1
- sleep(0.0001)
- print total
Finally, we only need:
- 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()
In this way, the following results are obtained during code execution:
- before
- 100000
- after
- before
- 100000
- after
The above is our introduction to Python AOP.