ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Wrapping method calls (meta-class example)
Submitter: Stephan Diehl (other recipes)
Last Updated: 2003/05/04
Version no: 1.3
Category: OOP

 

5 stars 3 vote(s)


Description:

A metaclass is used to wrap all (or just some) methods for logging purposes. The underlying mechanism can be used as well to check pre/post conditions, attribute access,...
The basic point is, that the actual class must not be changed in any way to achive the desired effect.

Source: Text Source

import re

log = open('log','w')
indent = 0
indStr = '  '

def logmethod(methodname):
    def _method(self,*argl,**argd):
        global indent

        #parse the arguments and create a string representation
        args = []
        for item in argl:
            args.append('%s' % str(item))
        for key,item in argd.items():
            args.append('%s=%s' % (key,str(item)))
        argstr = ','.join(args)   
        print >> log,"%s%s.%s(%s) " % (indStr*indent,str(self),methodname,argstr)
        indent += 1
        # do the actual method call
        returnval = getattr(self,'_H_%s' % methodname)(*argl,**argd)
        indent -= 1
        print >> log,'%s:'% (indStr*indent), str(returnval)
        return returnval

    return _method


class LogTheMethods(type):
    def __new__(cls,classname,bases,classdict):
        logmatch = re.compile(classdict.get('logMatch','.*'))
        
        for attr,item in classdict.items():
            if callable(item) and logmatch.match(attr):
                classdict['_H_%s'%attr] = item    # rebind the method
                classdict[attr] = logmethod(attr) # replace method by wrapper

        return type.__new__(cls,classname,bases,classdict)

class Test(object):
    __metaclass__ = LogTheMethods
    logMatch = '.*'

    def __init__(self):
        self.a = 10

    def meth1(self):pass
    def add(self,a,b):return a+b
    def fac(self,val): # faculty calculation
        if val == 1:
            return 1
        else:
            return val * self.fac(val-1)

if __name__ == '__main__':
    l = Test()
    l.meth1()
    print l.add(1,2)
    print l.fac(10)

Discussion:

This recipe shows, how a metaclass can help various programmers needs. In this example, all method calls (and there return value) are written into a log file.
Running the code will result in the following log file:

------------------------------------------------------
<__main__.Test object at 0x8158c24>.__init__()
: None
<__main__.Test object at 0x8158c24>.meth1()
: None
<__main__.Test object at 0x8158c24>.add(1,2)
: 3
<__main__.Test object at 0x8158c24>.fac(10)
||<__main__.Test object at 0x8158c24>.fac(9)
||||<__main__.Test object at 0x8158c24>.fac(8)
||||||<__main__.Test object at 0x8158c24>.fac(7)
||||||||<__main__.Test object at 0x8158c24>.fac(6)
||||||||||<__main__.Test object at 0x8158c24>.fac(5)
||||||||||||<__main__.Test object at 0x8158c24>.fac(4)
||||||||||||||<__main__.Test object at 0x8158c24>.fac(3)
||||||||||||||||<__main__.Test object at 0x8158c24>.fac(2)
||||||||||||||||||<__main__.Test object at 0x8158c24>.fac(1)
||||||||||||||||||: 1
||||||||||||||||: 2
||||||||||||||: 6
||||||||||||: 24
||||||||||: 120
||||||||: 720
||||||: 5040
||||: 40320
||: 362880
: 3628800
--------------------------------------------------------------

The only interesting bit is the rebinding of the methods and their replacement with a wrapper method.
To make this example a little less dull, only methods are wrapped whose method name match some regular expression.
This might be actually close to Aspect Oriented Programming (but I don't know enough about this to say for sure)



Add comment

Number of comments: 1

Non-metaclass solution, Robert Brewer, 2003/10/08
You can perform the same task on the fly, on any object, without metaclasses. This means you can reroute object methods from outside the object itself. Just reproduce the body of LogTheMethods in a "normal" function. The only bit you need to change is, instead of assigning classdict[attr] = logmethod(attr), you use new.instancemethod(function, instance, class)

if callable(item):
    anInstance = item.im_self
    attr = item.__name__
    anInstance.__dict__['_H_%s' % attr] = item
    anInstance.__dict__[attr] = \
                    new.instancemethod(logmethod(attr),
                                       anInstance,
                                       anInstance.__class__)
In this example, I rewrote the block so that one needs only pass in a reference to any method, and the other properties are introspected. I use this technique to "lock" object methods at runtime: accessing the original method requires a corresponding key. None of the locked objects need a metaclass defined, or __metaclass__ declared.
Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. Wrapping template engine ...

4. Assignment in expression

5. SOLVING THE METACLASS ...

6. Povray for python

7. Calling Windows API ...

8. Generic filter logic ...

9. Function Decorators by ...

10. MS SQL Server log monitor




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.