Source code for jssapi.decorators

# -*- coding: utf-8 -*-
#
[docs]class alias(object): """ Alias class that can be used as a decorator for making methods callable through other names (or "aliases"). Note: This decorator must be used inside an @aliased -decorated class. For example, if you want to make the method shout() be also callable as yell() and scream(), you can use alias like this: @alias('yell', 'scream') def shout(message): # .... """ def __init__(self, *aliases): """Constructor.""" self.aliases = set(aliases) def __call__(self, f): """ Method call wrapper. As this decorator has arguments, this method will only be called once as a part of the decoration process, receiving only one argument: the decorated function ('f'). As a result of this kind of decorator, this method must return the callable that will wrap the decorated function. """ f._aliases = self.aliases return f
[docs]def aliased(aliased_class): original_methods = aliased_class.__dict__.copy() for name, method in original_methods.items(): if hasattr(method, '_aliases'): # Add the aliases for 'method', but don't override any # previously-defined attribute of 'aliased_class' for alias in method._aliases - set(original_methods): setattr(aliased_class, alias, method) return aliased_class