コード例 #1
0
ファイル: scalar_conversion.py プロジェクト: yapengshi/drake
    def _on_add(self, param, cls):
        TemplateClass._on_add(self, param, cls)
        T, = param

        # Check that the user has not defined `__init__`, and has defined
        # `_construct` and `_construct_copy`.
        if not issubclass(cls, LeafSystem_[T]):
            raise RuntimeError("{} must inherit from {}".format(
                cls, LeafSystem_[T]))

        # Use the immediate `__dict__`, rather than querying the attributes, so
        # that we don't get spillover from inheritance.
        d = cls.__dict__
        no_init = "__init__" not in d
        has_construct = "_construct" in d
        has_copy = "_construct_copy" in d
        if not no_init:
            raise RuntimeError(
                "{} defines `__init__`, but should not. Please implement "
                "`_construct` and `_construct_copy` instead.".format(
                    cls.__name__))
        if not has_construct:
            raise RuntimeError(
                "{} does not define `_construct`. Please ensure this is "
                "defined.".format(cls.__name__))
        if not has_copy:
            raise RuntimeError(
                "{} does not define `_construct_copy`. Please ensure this "
                "is defined.".format(cls.__name__))

        # Patch `__init__`.
        template = self

        def system_init(self, *args, **kwargs):
            converter = None
            if "converter" in kwargs:
                converter = kwargs.pop("converter")
            if converter is None:
                # Use default converter.
                converter = template._converter
            if template._check_if_copying(self, *args, **kwargs):
                other = args[0]
                cls._construct_copy(self, other, converter=converter)
            else:
                cls._construct(self, *args, converter=converter, **kwargs)

        cls.__init__ = system_init
        return cls
コード例 #2
0
ファイル: scalar_conversion.py プロジェクト: kuindersma/drake
    def _on_add(self, param, cls):
        TemplateClass._on_add(self, param, cls)
        T, = param

        # Check that the user has not defined `__init__`, and has defined
        # `_construct` and `_construct_copy`.
        if not issubclass(cls, LeafSystem_[T]):
            raise RuntimeError(
                "{} must inherit from {}".format(cls, LeafSystem_[T]))

        # Use the immediate `__dict__`, rather than querying the attributes, so
        # that we don't get spillover from inheritance.
        d = cls.__dict__
        no_init = "__init__" not in d
        has_construct = "_construct" in d
        has_copy = "_construct_copy" in d
        if not no_init:
            raise RuntimeError(
                "{} defines `__init__`, but should not. Please implement "
                "`_construct` and `_construct_copy` instead."
                .format(cls.__name__))
        if not has_construct:
            raise RuntimeError(
                "{} does not define `_construct`. Pleaes ensure this is "
                "defined.".format(cls.__name__))
        if not has_copy:
            raise RuntimeError(
                "{} does not define `_construct_copy`. Please ensure this "
                "is defined.".format(cls.__name__))

        # Patch `__init__`.
        template = self

        def system_init(self, *args, **kwargs):
            converter = None
            if "converter" in kwargs:
                converter = kwargs.pop("converter")
            if converter is None:
                # Use default converter.
                converter = template._converter
            if template._check_if_copying(self, *args, **kwargs):
                other = args[0]
                cls._construct_copy(self, other, converter=converter)
            else:
                cls._construct(
                    self, *args, converter=converter, **kwargs)

        cls.__init__ = system_init
コード例 #3
0
    def __init__(self, name, T_list=None, T_pairs=None, module_name=None):
        """Constructs `TemplateSystem`.

        Args:
            T_list: List of T's that the given system supports. By default, it
                is all types supported by `LeafSystem`.
            T_pairs: List of pairs, (T, U), defining a conversion from a
                scalar type of U to T. If None, this will use all possible
                pairs that the Python bindings of `SystemScalarConverter`
                support.
            module_name: Defining `module_name`, per `TemplateClass`'s
                constructor.
        """
        if module_name is None:
            module_name = _get_module_name_from_stack()
        TemplateClass.__init__(self, name, module_name=module_name)

        # Check scalar types and conversions, using defaults if unspecified.
        if T_list is None:
            T_list = SystemScalarConverter.SupportedScalars
        for T in T_list:
            assert T in SystemScalarConverter.SupportedScalars, (
                "Type {} is not a supported scalar type".format(T))
        if T_pairs is None:
            T_pairs = _get_conversion_pairs(T_list)
        for T_pair in T_pairs:
            T, U = T_pair
            assert T in T_list and U in T_list, (
                "Conversion {} is not in the original parameter list".format(
                    T_pair))
            assert T_pair in \
                SystemScalarConverter.SupportedConversionPairs, (
                    "Conversion {} is not supported".format(T_pair))

        self._T_list = list(T_list)
        self._T_pairs = list(T_pairs)
        self._converter = self._make_converter()
コード例 #4
0
ファイル: scalar_conversion.py プロジェクト: kuindersma/drake
    def __init__(self, name, T_list=None, T_pairs=None, module_name=None):
        """Constructs `TemplateSystem`.

        @param T_list
            List of T's that the given system supports. By default, it is all
            types supported by `LeafSystem`.
        @param T_pairs List of pairs, (T, U), defining a conversion from a
            scalar type of U to T.
            If None, this will use all possible pairs that the Python bindings
            of `SystemScalarConverter` support.
        @param module_name
            Defining `module_name`, per `TemplateClass`'s constructor.
        """
        if module_name is None:
            module_name = _get_module_name_from_stack()
        TemplateClass.__init__(self, name, module_name=module_name)

        # Check scalar types and conversions, using defaults if unspecified.
        if T_list is None:
            T_list = SystemScalarConverter.SupportedScalars
        for T in T_list:
            assert T in SystemScalarConverter.SupportedScalars, (
                "Type {} is not a supported scalar type".format(T))
        if T_pairs is None:
            T_pairs = _get_conversion_pairs(T_list)
        for T_pair in T_pairs:
            T, U = T_pair
            assert T in T_list and U in T_list, (
                "Conversion {} is not in the original parameter list"
                .format(T_pair))
            assert T_pair in \
                SystemScalarConverter.SupportedConversionPairs, (
                    "Conversion {} is not supported".format(T_pair))

        self._T_list = list(T_list)
        self._T_pairs = list(T_pairs)
        self._converter = self._make_converter()