예제 #1
0
    def _validate(self, value):
        """Validate that the value is of the correct type."""

        if type(value) is _Types.Length:
            return value._convert_to(self._unit)

        else:
            # Extract the value and unit from the argument string.
            value, unit = _validate_unit_requirement(value, "length")

            if unit is None:
                return _Types.Length(value, self._unit)
            else:
                return _Types.Length(value, unit)._convert_to(self._unit)
예제 #2
0
    def __init__(self,
                 help=None,
                 default=None,
                 unit=None,
                 minimum=None,
                 maximum=None,
                 allowed=None):
        """Constructor.

           Parameters
           ----------

           help : str
               The help string.

           default : :class:`Length <BioSimSpace.Types.Length>`
               The default value.

           unit : str
               The unit.

           minimum : :class:`Length <BioSimSpace.Types.Length>`
               The minimum allowed value.

           maximum : :class:`Length <BioSimSpace.Types.Length>`
               The maximum allowed value.

           allowed : [:class:`Length <BioSimSpace.Types.Length>`]
               A list of allowed values.
        """

        # Validate the unit.
        if unit is not None:
            length = _Types.Length("1 %s" % unit)
            self._unit = length.unit()
            self._print_unit = length._print_format[length.unit()]
        else:
            try:
                self._unit = default.unit()
            except:
                raise ValueError(
                    "No unit or default value has been specified!")

        # Call the base class constructor.
        super().__init__(help=help,
                         default=default,
                         unit=self._unit,
                         minimum=minimum,
                         maximum=maximum,
                         allowed=allowed)