A week of practice in python is finally a result. Haha, an AOP implemented using metaclass.
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 before and after the method to be called in metaclass of _ new.
DetailsCodeAs follows:
1
2 _ Author __ = " Alex "
3 _ Date __ = " $2008-12-5 23:54:11 $ "
4 _ Name __ = " Pyaop "
5
6 '''
7 This metaclass is the basis for Implementing AOP.
8 '''
9 Class Pyaop (type ):
10 '''
11 This empty method is used to initialize beforeop and afterop to function reference.
12 '''
13 Def Nop (Self ):
14 Pass
15 '''
16 The following two variables are class variables, that is, the variables that store the addresses of the two functions to be implanted.
17 '''
18 Beforeop = NOP
19 Afterop = NOP
20 '''
21 Set the class functions of the first and second embedded Functions
22 '''
23 @ Classmethod
24 Def Setbefore (self, func ):
25 Pyaop. beforeop = Func
26 @ Classmethod
27 Def Setafter (self, func ):
28 Pyaop. afterop = Func
29 '''
30 Initialize the metaclass function. The most important parameter of this function is the fourth parameter. Through this parameter, dict can modify the attributes (methods) of the class)
31 '''
32 Def _ New __ (MCL, name, bases, dict ):
33 From Types Import Functiontype # Functiontype of the load type module
34 OBJ = Object () # Defines the variables of an empty object.
35 '''
36 This is the method to be implanted. the func parameter is the function we want to call.
37 '''
38 Def AOP (func ):
39 '''
40 We use this function to replace the function to be called.
41 '''
42 Def Wrapper ( * ARGs, ** Kwds ):
43 Pyaop. beforeop (OBJ) # Call the prefix Function
44 Value = Func ( * ARGs, ** Kwds) # Call the function to be called
45 Pyaop. afterop (OBJ) # Call the post Function
46 Return Value # Return
47 Return Wrapper
48 # Find the function to be called in the member list of the class
49 For ATTR, Value In Dict. iteritems ():
50 If Isinstance (value, functiontype ):
51 Dict [ATTR] = AOP (value) # Replace it with the function AOP.
52 OBJ = Super (pyaop, MCL ). _ New __ (MCL, name, bases, dict) # Call _ new _ () of the parent class to create self.
53 Return OBJ
54
If we want to intercept a method call for Class A, this is the case:
1 Class A (object ):
2 _ Metaclass __ = Pyaop
3 Def Foo (Self ):
4 Total = 0
5 For I In Range ( 100000 ):
6 Total = Total + 1
7 Print Total
8
9 Def Foo2 (Self ):
10 From Time Import Sleep
11 Total = 0
12 For I In Range ( 100000 ):
13 Total = Total + 1
14 Sleep ( 0.0001 )
15 Print Total
Finally, we only need:
1 Def Beforep (Self ):
2 Print ( ' Before ' )
3 Def Afterp (Self ):
4 Print ( ' After ' )
5
6 If _ Name __ = " _ Main __ " :
7 Pyaop. setbefore (beforep)
8 Pyaop. setafter (afterp)
9 A = A ()
10 A. Foo ()
11 A. foo2 ()
In this way, the following results are obtained during code execution:
Before
100000
After
Before
100000
After
This Code takes one day to call (shame), is based on another http://www.cnblogs.com/cavingdeep/archive/2006/08/22/483056.html posted in the garden
I would like to thank the prawn seewind (69828975) in the python discussion group 310380 for their selfless help.
You are welcome to give an axe to everyone in Python.