示例#1
0
class MatlabFunc(object):
    """
    Reference to a MATLAB function, where "matlabfunc" is replaced by the
    function called by the user. *args are passed to MATLAB. **kwargs are
    only passed to the engine.
    """
    def __init__(self, eng, name):
        self.__dict__["_engine"] = weakref.ref(eng)
        self.__dict__["_name"] = name

    def __getattr__(self, name):
        return MatlabFunc(self._engine(), "%s.%s" % (self._name, name))

    def __setattr__(self, kw, value):
        raise AttributeError("attribute cannot be added to MatlabEngine")

    def __call__(self, *args, **kwargs):
        self.__validate_engine()

        nargs = kwargs.pop('nargout', 1)

        if not isinstance(nargs, int):
            raise TypeError('nargout must be an int, not %s' %
                            type(nargs).__name__)

        if nargs < 0:
            raise ValueError('nargout must be greater or equal to 0')

        async = kwargs.pop('async', False)
        if not isinstance(async, bool):
            raise TypeError('async must be a bool, not %s' %
                            type(async).__name__)

        _stdout = kwargs.pop('stdout', None)
        _stderr = kwargs.pop('stderr', None)

        if (_stdout is not None) and (not isinstance(_stdout, sIO.StringIO)):
            raise TypeError(
                'stdout must be %s.%s, not %s.%s ' %
                (sIO.__name__, sIO.StringIO.__name__,
                 _stdout.__class__.__module__, _stdout.__class__.__name__))

        if (_stderr is not None) and (not isinstance(_stderr, sIO.StringIO)):
            raise TypeError(
                'stderr must be %s.%s, not %s.%s ' %
                (sIO.__name__, sIO.StringIO.__name__,
                 _stderr.__class__.__module__, _stderr.__class__.__name__))

        future = pythonengine.evaluateFunction(self._engine()._matlab,
                                               self._name,
                                               nargs,
                                               args,
                                               out=_stdout,
                                               err=_stderr)
        if async:
            return FutureResult(self._engine(), future, nargs, _stdout,
                                _stderr)
        else:
            return FutureResult(self._engine(), future, nargs, _stdout,
                                _stderr).result()
    def __call__(self, *args, **kwargs):
        self.__validate_engine()

        nargs = kwargs.pop('nargout', 1)

        if not isinstance(nargs, int):
            raise TypeError(
                pythonengine.getMessage('NargoutMustBeInt',
                                        type(nargs).__name__))

        if nargs < 0:
            raise ValueError(
                pythonengine.getMessage('NargoutCannotBeLessThanZero'))

        _stdout = kwargs.pop('stdout', None)
        _stderr = kwargs.pop('stderr', None)

        background = enginehelper._get_async_or_background_argument(kwargs)

        _sIO_info = '{0}.{1}'.format(sIO.__name__, sIO.StringIO.__name__)
        if (_stdout is not None) and (not isinstance(_stdout, sIO.StringIO)):
            _stdout_info = '{0}.{1}'.format(_stdout.__class__.__module__,
                                            _stdout.__class__.__name__)
            raise TypeError(
                pythonengine.getMessage('StdoutMustBeStringIO', _sIO_info,
                                        _stdout_info))

        if (_stderr is not None) and (not isinstance(_stderr, sIO.StringIO)):
            _stderr_info = '{0}.{1}'.format(_stderr.__class__.__module__,
                                            _stderr.__class__.__name__)
            raise TypeError(
                pythonengine.getMessage('StderrMustBeStringIO', _sIO_info,
                                        _stderr_info))

        future = pythonengine.evaluateFunction(self._engine()._matlab,
                                               self._name,
                                               nargs,
                                               args,
                                               out=_stdout,
                                               err=_stderr)
        if background:
            return FutureResult(self._engine(),
                                future,
                                nargs,
                                _stdout,
                                _stderr,
                                feval=True)
        else:
            return FutureResult(self._engine(),
                                future,
                                nargs,
                                _stdout,
                                _stderr,
                                feval=True).result()
class MatlabFunc(object):
    """
    Reference to a MATLAB function, where "matlabfunc" is replaced by the
    function called by the user. *args are passed to MATLAB. **kwargs are
    only passed to the engine.
    """
    def __init__(self, eng, name):
        self.__dict__["_engine"] = weakref.ref(eng)
        self.__dict__["_name"] = name

    def __getattr__(self, name):
        return MatlabFunc(self._engine(), "%s.%s" % (self._name, name))

    def __setattr__(self, kw, value):
        raise AttributeError(pythonengine.getMessage('AttrCannotBeAddedToM'))

    def __call__(self, *args, **kwargs):
        self.__validate_engine()

        nargs = kwargs.pop('nargout', 1)

        if not isinstance(nargs, int):
            raise TypeError(
                pythonengine.getMessage('NargoutMustBeInt') +
                ' %s' % type(nargs).__name__)

        if nargs < 0:
            raise ValueError(
                pythonengine.getMessage('NargoutCannotBeLessThanZero'))

        async = kwargs.pop('async', False)
        if not isinstance(async, bool):
            raise TypeError(
                pythonengine.getMessage('AsyncMustBeBool') +
                ' %s' % type(async).__name__)

        _stdout = kwargs.pop('stdout', None)
        _stderr = kwargs.pop('stderr', None)

        if kwargs:
            raise TypeError(
                (pythonengine.getMessage('InvalidKwargs') + ' %r') %
                (list(kwargs.keys())[0]))

        if (_stdout is not None) and (not isinstance(_stdout, sIO.StringIO)):
            raise TypeError(
                (pythonengine.getMessage('StdoutMustBe') + ' %s.%s, ' +
                 pythonengine.getMessage('NOT') + ' %s.%s ') %
                (sIO.__name__, sIO.StringIO.__name__,
                 _stdout.__class__.__module__, _stdout.__class__.__name__))

        if (_stderr is not None) and (not isinstance(_stderr, sIO.StringIO)):
            raise TypeError(
                (pythonengine.getMessage('StderrMustBe') + ' %s.%s, ' +
                 pythonengine.getMessage('NOT') + ' %s.%s ') %
                (sIO.__name__, sIO.StringIO.__name__,
                 _stderr.__class__.__module__, _stderr.__class__.__name__))

        future = pythonengine.evaluateFunction(self._engine()._matlab,
                                               self._name,
                                               nargs,
                                               args,
                                               out=_stdout,
                                               err=_stderr)
        if async:
            return FutureResult(self._engine(),
                                future,
                                nargs,
                                _stdout,
                                _stderr,
                                feval=True)
        else:
            return FutureResult(self._engine(),
                                future,
                                nargs,
                                _stdout,
                                _stderr,
                                feval=True).result()