def _validate(self, value): """Validate that the value is of the correct type.""" if type(value) is _Types.Charge: return value._convert_to(self._unit) else: # Extract the value and unit from the argument string. value, unit = _validate_unit_requirement(value, "energy") if unit is None: return _Types.Charge(value, self._unit) else: return _Types.Charge(value, unit)._convert_to(self._unit)
def __init__(self, help=None, default=None, unit=None, minimum=None, maximum=None, allowed=None): """Constructor. Parameters ---------- help : str The help string. default : :class:`Charge <BioSimSpace.Types.Charge>` The default value. unit : str The unit. minimum : :class:`Charge <BioSimSpace.Types.Charge>` The minimum allowed value. maximum : :class:`Charge <BioSimSpace.Types.Charge>` The maximum allowed value. allowed : [:class:`Charge <BioSimSpace.Types.Charge>`] A list of allowed values. """ # Validate the unit. if unit is not None: nrg = _Types.Charge("1 %s" % unit) self._unit = nrg.unit() self._print_unit = nrg._print_format[nrg.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)