Instead of using per plass decorators you might use a class decorator that 
enhances the function/method decorators with the class name:

commands_dict = {}
ivars_dict = {}

# Use as function/method decorator
def cmd(command_name):

    class Decorator:

        def __init__(self, func):
            self.func = func
            try:    # for Python 2
                self.isMethod = 'self' in func.func_code__.co_varnames
            except: # for Python 3
                self.isMethod = 'self' in func.__code__.co_varnames

            commands_dict[command_name] = self.__call__
            ivars_dict[command_name] = 'testClass'

        def __call__(self, *args, **kwargs):

            if self.isMethod:
                print("*** className at runtime call:", self.className)
                return self.func(args[0], *args, **kwargs)
                    # args[0]: implicit 'self' of class method
            else:
                return self.func(*args, **kwargs)

    return Decorator

# Use as class decorator
def addClassnames(aClass):

    for key, value in aClass.__dict__.items():
        isDecorator = 'Decorator' in str(value)
        if isDecorator:
            value.__dict__['className'] = aClass.__name__

    def onCall(*args, **kwargs):
        return aClass(*args, **kwargs)

    return onCall


@cmd('command1')
def pureFunction(event=None):
    """pureFunction docstring"""
    print("pureFunction with argument:", event)
    return ("...exit pureFunction")


@addClassnames
class MyClass:

    @cmd('command2')
    def instanceMethod(self, event=None):
        """instanceMethod docstring"""
        print("MyClass.instanceMethod with argument:", event)
        return ("...exit instanceMethod")

for command in commands_dict:
    func = commands_dict[command]
    result = func('anArgument')
    print("result:", result)


Reinhard



-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to