Пример #1
0
    def _parse_unit(cls, unit, detailed_exception=True):
        if unit not in cls._units:
            if detailed_exception:
                raise ValueError("Unit '{}' not supported by the CDS SAC "
                                 "standard. {}".format(
                                     unit, did_you_mean(unit, cls._units)))
            else:
                raise ValueError()

        return cls._units[unit]
Пример #2
0
    def _parse_unit(cls, unit, detailed_exception=True):
        if unit not in cls._units:
            if detailed_exception:
                raise ValueError(
                    "Unit '{0}' not supported by the CDS SAC "
                    "standard. {1}".format(
                        unit, did_you_mean(
                            unit, cls._units)))
            else:
                raise ValueError()

        return cls._units[unit]
Пример #3
0
    def _parse_unit(cls, s, detailed_exception=True):
        registry = core.get_current_unit_registry().registry
        if s == '%':
            return registry['percent']
        elif s in registry:
            return registry[s]

        if detailed_exception:
            raise ValueError('{} is not a valid unit. {}'.format(
                s, did_you_mean(s, registry)))
        else:
            raise ValueError()
Пример #4
0
    def _parse_unit(cls, s, detailed_exception=True):
        registry = core.get_current_unit_registry().registry
        if s == '%':
            return registry['percent']
        elif s in registry:
            return registry[s]

        if detailed_exception:
            raise ValueError(
                '{0} is not a valid unit. {1}'.format(
                    s, did_you_mean(s, registry)))
        else:
            raise ValueError()
Пример #5
0
    def _parse_unit(cls, s, detailed_exception=True):
        registry = core.get_current_unit_registry().registry
        if s == '%':
            return registry['percent']

        if not _is_ascii(s):
            if s[0] == '\N{MICRO SIGN}':
                s = 'u' + s[1:]
            if s[-1] == '\N{GREEK CAPITAL LETTER OMEGA}':
                s = s[:-1] + 'Ohm'
            elif s[-1] == '\N{LATIN CAPITAL LETTER A WITH RING ABOVE}':
                s = s[:-1] + 'Angstrom'

        if s in registry:
            return registry[s]

        if detailed_exception:
            raise ValueError('{} is not a valid unit. {}'.format(
                s, did_you_mean(s, registry)))
        else:
            raise ValueError()
Пример #6
0
def did_you_mean_units(s, all_units, deprecated_units, format_decomposed):
    """
    A wrapper around `astropy.utils.misc.did_you_mean` that deals with
    the display of deprecated units.

    Parameters
    ----------
    s : str
        The invalid unit string

    all_units : dict
        A mapping from valid unit names to unit objects.

    deprecated_units : sequence
        The deprecated unit names

    format_decomposed : callable
        A function to turn a decomposed version of the unit into a
        string.  Should return `None` if not possible

    Returns
    -------
    msg : str
        A string message with a list of alternatives, or the empty
        string.
    """
    def fix_deprecated(x):
        if x in deprecated_units:
            results = [x + ' (deprecated)']
            decomposed = _try_decomposed(
                all_units[x], format_decomposed)
            if decomposed is not None:
                results.append(decomposed)
            return results
        return (x,)

    return did_you_mean(s, all_units, fix=fix_deprecated)
Пример #7
0
def did_you_mean_units(s, all_units, deprecated_units, format_decomposed):
    """
    A wrapper around `astropy.utils.misc.did_you_mean` that deals with
    the display of deprecated units.

    Parameters
    ----------
    s : str
        The invalid unit string

    all_units : dict
        A mapping from valid unit names to unit objects.

    deprecated_units : sequence
        The deprecated unit names

    format_decomposed : callable
        A function to turn a decomposed version of the unit into a
        string.  Should return `None` if not possible

    Returns
    -------
    msg : str
        A string message with a list of alternatives, or the empty
        string.
    """
    def fix_deprecated(x):
        if x in deprecated_units:
            results = [x + ' (deprecated)']
            decomposed = _try_decomposed(
                all_units[x], format_decomposed)
            if decomposed is not None:
                results.append(decomposed)
            return results
        return (x,)

    return did_you_mean(s, all_units, fix=fix_deprecated)