Who wants  to define *two *wrappers in the same decorator?

Just to be clear, this whole discussion is moot because the following works:
> @cmd('clone-find-all')
> @cmd('find-clone-all')
> And a good thing too, because I seen *no way* to define *two *wrappers in 
> the same decorator.  And even if there is a way, I don't particularly want 
> to know about it ;-)


Are you sure? 

The following solution gives you everything you want without functools and 
inner wrappers.
If works for both Python2 and Python3, and the synonyms are thrown in for 
free.

commands_dict = {}

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

    class Decorator:

        def __init__(self, func, *args):

            self.func = func

            try:                                            # Python3
                self.isMethod = '.' in func.__qualname__
            except AttributeError:                          # Python 2
                self.isMethod = 'instance' in str(func)

            for command_name in command_names:
                commands_dict[command_name] = self.__call__

        def __call__(self, *args, **kwargs):
            if self.isMethod:
                return self.func(args[0], *args, **kwargs)
            else:
                return self.func(*args, **kwargs)

    return Decorator

@cmd('command-first', 'first-command')
def pureFunction(event=None):
    """pureFunction docstring"""
    print("-- function:", event)


class MyClass:

    @cmd('command-second', 'second-command', 'another-second-command')
    def instanceMethod(self, event=None):
        """instanceMethod docstring"""
        print("** method:", event)

def tests():
    """Calling functions/methods from the command_dict"""
    for command in commands_dict:
        commands_dict[command]("I'm command <%s>." % command)

tests()


The commands are run from the dispatch dictionary (commands_dict).

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