def attach_func_as_method(name, func_obj, self_param_name, workspace_types=None): """ Adds a method to the given type that calls an algorithm using the calling object as the input workspace :param name: The name of the new method as it should appear on the type :param func_obj: A free function object that defines the implementation of the call :param self_param_name: The name of the parameter in the free function that the method's self maps to :param workspace_types: A list of string names of a workspace types. If None, then it is attached to the general Workspace type. Default=None """ def _method_impl(self, *args, **kwargs): # Map the calling object to the requested parameter kwargs[self_param_name] = self # Define the frame containing the final variable assignment # used to figure out the workspace name kwargs["__LHS_FRAME_OBJECT__"] = _inspect.currentframe().f_back # Call main function return func_obj(*args, **kwargs) # ------------------------------------------------------------------ # Add correct meta-properties for the method signature = ['self'] signature.extend(get_function_code(func_obj).co_varnames) customise_func(_method_impl, func_obj.__name__, tuple(signature), func_obj.__doc__) if workspace_types or len(workspace_types) > 0: from mantid import api for typename in workspace_types: cls = getattr(api, typename) setattr(cls, name, _method_impl) else: setattr(Workspace, name, _method_impl)
def attach_func_as_method(name, func_obj, self_param_name, algm_name, workspace_types=None): """ Adds a method to the given type that calls an algorithm using the calling object as the input workspace :param name: The name of the new method as it should appear on the type :param func_obj: A free function object that defines the implementation of the call :param self_param_name: The name of the parameter in the free function that the method's self maps to :param algm_name: The name of the algorithm being attached. :param workspace_types: A list of string names of a workspace types. If None, then it is attached to the general Workspace type. Default=None """ def _method_impl(self, *args, **kwargs): # Map the calling object to the requested parameter kwargs[self_param_name] = self # Define the frame containing the final variable assignment # used to figure out the workspace name kwargs["__LHS_FRAME_OBJECT__"] = _inspect.currentframe().f_back # Call main function return func_obj(*args, **kwargs) # ------------------------------------------------------------------ customise_func(_method_impl, func_obj.__name__, LazyMethodSignature(alg_name=algm_name), func_obj.__doc__) if workspace_types or len(workspace_types) > 0: from mantid import api for typename in workspace_types: cls = getattr(api, typename) setattr(cls, name, _method_impl) else: setattr(Workspace, name, _method_impl)
def attach_func_as_method(name, func_obj, self_param_name, workspace_types=None): """ Adds a method to the given type that calls an algorithm using the calling object as the input workspace :param name: The name of the new method as it should appear on the type :param func_obj: A free function object that defines the implementation of the call :param self_param_name: The name of the parameter in the free function that the method's self maps to :param workspace_types: A list of string names of a workspace types. If None, then it is attached to the general Workspace type. Default=None """ def _method_impl(self, *args, **kwargs): # Map the calling object to the requested parameter kwargs[self_param_name] = self # Define the frame containing the final variable assignment # used to figure out the workspace name kwargs["__LHS_FRAME_OBJECT__"] = _inspect.currentframe().f_back # Call main function return func_obj(*args, **kwargs) # ------------------------------------------------------------------ # Add correct meta-properties for the method if hasattr(func_obj, '__signature__'): from inspect import Parameter func_parameters = list(func_obj.__signature__.parameters.values()) func_parameters.insert(0, Parameter("self", Parameter.POSITIONAL_ONLY)) signature = func_obj.__signature__.replace(parameters=func_parameters) else: signature = ['self'] signature.extend(getsource(func_obj).co_varnames) signature = tuple(signature) customise_func(_method_impl, func_obj.__name__, signature, func_obj.__doc__) if workspace_types or len(workspace_types) > 0: from mantid import api for typename in workspace_types: cls = getattr(api, typename) setattr(cls, name, _method_impl) else: setattr(Workspace, name, _method_impl)