def scilab_command(*args, **kwargs): """ Scilab command """ kwargs['nout'] = get_nout() if name == 'getd': kwargs['nout'] = 0 kwargs['verbose'] = kwargs.get('verbose', False) return self._call(name, *args, **kwargs)
def _call(self, func, *inputs, **kwargs): """ Scilab2Py Parameters ---------- inputs : array_like Variables to pass to the function. nout : int, optional Number of output arguments. This is set automatically based on the number of return values requested. You can override this behavior by passing a different value. verbose : bool, optional Log Scilab output at info level. plot_dir: str, optional If specificed, save the session's plot figures to the plot directory instead of displaying the plot window. plot_name : str, optional Saved plots will start with `plot_name` and end with "_%%.xxx' where %% is the plot number and xxx is the `plot_format`. plot_format: str, optional The format in which to save the plot (PNG by default). kwargs : dictionary, optional Key - value pairs to be passed as prop - value inputs to the function. The values must be strings or numbers. Returns ------- out : value Value returned by the function. Raises ------ Scilab2PyError If the function call is unsucessful. """ nout = kwargs.pop('nout', get_nout()) argout_list = ['ans'] if '=' in func: nout = 0 # these three lines will form the commands sent to Scilab # load("-v6", "infile", "invar1", ...) # [a, b, c] = foo(A, B, C) # save("-v6", "outfile", "outvar1", ...) load_line = call_line = save_line = '' prop_vals = [] eval_kwargs = {} for (key, value) in kwargs.items(): if key in ['verbose', 'timeout'] or key.startswith('plot_'): eval_kwargs[key] = value continue if isinstance(value, (str, unicode, int, float)): prop_vals.append('"%s", %s' % (key, repr(value))) else: msg = 'Keyword arguments must be a string or number: ' msg += '%s = %s' % (key, value) raise Scilab2PyError(msg) prop_vals = ', '.join(prop_vals) if nout: # create a dummy list of var names ("a", "b", "c", ...) # use ascii char codes so we can increment argout_list, save_line = self._reader.setup(nout) call_line = '[{0}] = '.format(', '.join(argout_list)) call_line += func + '(' if inputs: argin_list, load_line = self._writer.create_file(inputs) call_line += ', '.join(argin_list) if prop_vals: if inputs: call_line += ', ' call_line += prop_vals call_line += ')' # create the command and execute in octave cmd = [load_line, call_line, save_line] data = self.eval(cmd, **eval_kwargs) if isinstance(data, dict) and not isinstance(data, Struct): data = [data.get(v, None) for v in argout_list] if len(data) == 1 and data[0] is None: data = None return data
def _call(self, func, *inputs, **kwargs): """ Scilab2Py Parameters ---------- inputs : array_like Variables to pass to the function. nout : int, optional Number of output arguments. This is set automatically based on the number of return values requested. You can override this behavior by passing a different value. verbose : bool, optional Log Scilab output at info level. plot_dir: str, optional If specificed, save the session's plot figures to the plot directory instead of displaying the plot window. plot_name : str, optional Saved plots will start with `plot_name` and end with "_%%.xxx' where %% is the plot number and xxx is the `plot_format`. plot_format: str, optional The format in which to save the plot (PNG by default). kwargs : dictionary, optional Key - value pairs to be passed as prop - value inputs to the function. The values must be strings or numbers. Returns ------- out : value Value returned by the function. Raises ------ Scilab2PyError If the function call is unsucessful. Notes ----- Integer type arguments will be converted to floating point unless `convert_to_float=False`. """ nout = kwargs.pop('nout', get_nout()) argout_list = ['ans'] if '=' in func: nout = 0 # these three lines will form the commands sent to Scilab # load("-v6", "infile", "invar1", ...) # [a, b, c] = foo(A, B, C) # save("-v6", "outfile", "outvar1", ...) load_line = call_line = save_line = '' prop_vals = [] eval_kwargs = {} for (key, value) in kwargs.items(): if key in ['verbose', 'timeout'] or key.startswith('plot_'): eval_kwargs[key] = value continue if isinstance(value, (str, unicode, int, float)): prop_vals.append('"%s", %s' % (key, repr(value))) else: msg = 'Keyword arguments must be a string or number: ' msg += '%s = %s' % (key, value) raise Scilab2PyError(msg) prop_vals = ', '.join(prop_vals) if nout: # create a dummy list of var names ("a", "b", "c", ...) # use ascii char codes so we can increment argout_list, save_line = self._reader.setup(nout) call_line = '[{0}] = '.format(', '.join(argout_list)) call_line += func + '(' if inputs: argin_list, load_line = self._writer.create_file(inputs) call_line += ', '.join(argin_list) if prop_vals: if inputs: call_line += ', ' call_line += prop_vals call_line += ');' # create the command and execute in octave cmd = [load_line, call_line, save_line] data = self.eval(cmd, **eval_kwargs) if isinstance(data, dict) and not isinstance(data, Struct): data = [data.get(v, None) for v in argout_list] if len(data) == 1 and data[0] is None: data = None return data