""" open/DurusWorks/qp/lib/delegation.py """ def delegate(klass, *names): """ The names are strings of the form .. For each one, this adds a method to klass, named , that delegates to the same method of self.. Arguments are passed to the method without change. """ def get_delegated(attribute_name, method_name): def delegated(self, *args, **kwargs): method = getattr(getattr(self, attribute_name), method_name) return method(*args, **kwargs) return delegated for name in names: attribute_name, method_name = name.split('.') setattr(klass, method_name, get_delegated(attribute_name, method_name)) def delegate_passing_self(klass, *names): """ The names are strings of the form .. For each one, this adds a method to klass, named , that delegates to the same method of self.. Arguments are passed to the method with "self" added as the first argument. """ def get_delegated(attribute_name, method_name): def delegated(self, *args, **kwargs): method = getattr(getattr(self, attribute_name), method_name) return method(self, *args, **kwargs) return delegated for name in names: attribute_name, method_name = name.split('.') setattr(klass, method_name, get_delegated(attribute_name, method_name))