def eachMethod(decorator, methodFilter=lambda fName: True):
    """
    Class decorator that wraps every single method in its own method decorator

    methodFilter: a function which accepts a function name and should return
    True if the method is one which we want to decorate, False if we want to
    leave this method alone.

    methodFilter can also be simply a string prefix. If it is a string, it is
    assumed to be the prefix we're looking for.
    """
    if isinstance(methodFilter, basestring):
        # Is it a string? If it is, change it into a function that takes a string.
        prefix = methodFilter
        methodFilter = lambda fName: fName.startswith(prefix)

    ismethod = lambda fn: inspect.ismethod(fn) or inspect.isfunction(fn)

    def innerDeco(cls):
        assert inspect.isclass(cls), "eachMethod is designed to be used only on classes"
        for fName, fn in inspect.getmembers(cls):
            if methodFilter(fName):
                if ismethod(fn):
                    # We attempt to avoid decorating staticmethods by looking for an arg named cls
                    # or self; this is a kludge, but there's no other way to tell, and
                    # staticmethods do not work correctly with eachMethod
                    if getargspec(fn).args[0] not in ['cls', 'self']:
                        continue

                    setattr(cls, fName, decorator(fn))

        return cls
    return innerDeco
def eachMethod ( decorator , methodFilter = lambda fName : True ) : if isinstance ( methodFilter , basestring ) : prefix = methodFilter methodFilter = lambda fName : fName . startswith ( prefix ) ismethod = lambda fn : inspect . ismethod ( fn ) or inspect . isfunction ( fn ) def innerDeco ( cls ) : assert inspect . isclass ( cls ) , "eachMethod is designed to be used only on classes" for fName , fn in inspect . getmembers ( cls ) : if methodFilter ( fName ) : if ismethod ( fn ) : if getargspec ( fn ) . args [ 0 ] not in [ 'cls' , 'self' ] : continue setattr ( cls , fName , decorator ( fn ) ) return cls return innerDeco
Class decorator that wraps every single method in its own method decorator

    methodFilter: a function which accepts a function name and should return
    True if the method is one which we want to decorate, False if we want to
    leave this method alone.

    methodFilter can also be simply a string prefix. If it is a string, it is
    assumed to be the prefix we're looking for.