>
> It's even easier: You don't need a class decorator but can retrieve the 
> class nam from th decorator's __dict__:
>

commands_dict = {}
ivars_dict = {}

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

    class Decorator:

        def __init__(self, func):
            self.func = func

            callable = str(self.__dict__['func']).split()[1]
            print("callable:", callable)
            self.isMethod = '.' in callable
            if self.isMethod:
                self.className = callable.split('.')[0]
            else:
                self.className = None

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

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

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

    return Decorator

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


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