def convert_to(units, on_dimensionless='warn', on_incompatible='raise',
               return_float=False):
    """Return a function that convert a Quantity to to another units.

    :param units: string or Quantity specifying the target units
    :param on_dimensionless: how to proceed when a dimensionless number
                             number is given.
                             'raise' to raise an exception,
                             'warn' to log a warning and proceed,
                             'ignore' to silently proceed
    :param on_incompatible: how to proceed when source and target units are
                            incompatible. Same options as `on_dimensionless`

    :raises: :class:`ValueError` if the incoming value cannot be
             properly converted

        >>> convert_to('mV')(Q_(1, 'V'))
        <Quantity(1000.0, 'millivolt')>
        >>> convert_to('mV', return_float=True)(Q_(1, 'V'))
        1000.0
    """
    if on_dimensionless not in ('ignore', 'warn', 'raise'):
        raise ValueError("{} is not a valid value for 'on_dimensionless'. "
                         "It should be either 'ignore', 'warn' or 'raise'".format(on_dimensionless))
    if on_incompatible not in ('ignore', 'warn', 'raise'):
        raise ValueError("{} is not a valid value for 'on_incompatible'. "
                         "It should be either 'ignore', 'warn' or 'raise'".format(on_dimensionless))

    if isinstance(units, str):
        units = Q_(1, units)
    elif not isinstance(units, Q_):
        raise ValueError("{} is not a valid value for 'units'. "
                         "It should be either str or Quantity")

    if return_float:
        def _inner(value):
            if isinstance(value, Q_):
                try:
                    return value.to(units).magnitude
                except ValueError as e:
                    if on_incompatible == 'raise':
                        raise ValueError(e)
                    elif on_incompatible == 'warn':
                        msg = 'Unable to convert {} to {}. Ignoring source units.'.format(value, units)
                        warnings.warn(msg, DimensionalityWarning)
                        _LOG.warn(msg)

                # on_incompatible == 'ignore'
                return value.magnitude
            else:
                if not units.dimensionless:
                    if on_dimensionless == 'raise':
                        raise ValueError('Unable to convert {} to {}'.format(value, units))
                    elif on_dimensionless == 'warn':
                        msg = 'Assuming units `{1.units}` for {0}'.format(value, units)
                        warnings.warn(msg, DimensionalityWarning)
                        _LOG.warn(msg)

                # on_incompatible == 'ignore'
                return float(value)
        return _inner
    else:
        def _inner(value):
            if isinstance(value, Q_):
                try:
                    return value.to(units)
                except ValueError as e:
                    if on_incompatible == 'raise':
                        raise ValueError(e)
                    elif on_incompatible == 'warn':
                        msg = 'Assuming units `{1.units}` for {0}'.format(value, units)
                        warnings.warn(msg, DimensionalityWarning)
                        _LOG.warn(msg)

                # on_incompatible == 'ignore'
                return float(value.magnitude) * units
            else:
                if not units.dimensionless:
                    if on_dimensionless == 'raise':
                        raise ValueError('Unable to convert {} to {}'.format(value, units))
                    elif on_dimensionless == 'warn':
                        msg = 'Assuming units `{1.units}` for {0}'.format(value, units)
                        warnings.warn(msg, DimensionalityWarning)
                        _LOG.warn(msg)

                # on_incompatible == 'ignore'
                return float(value) * units
        return _inner
dataset/ETHPy150Open LabPy/lantz/lantz/processors.py/convert_to/original