Exemplo n.º 1
0
def get_are_units_equal_by_string_or_cfunits(source,target,try_cfunits=True):
    '''
    Test if unit definitions are equal.
    
    :param str source: String definition of the units to compare against.
    :param str target: Target units to test for equality.
    :param bool try_cfunits: If ``True`` attempt to import and use
     :class:`cfunits.Units` for equality operation.
     
    >>> get_are_units_equal_by_string_or_cfunits('K','K',try_cfunits=True)
    True
    >>> get_are_units_equal_by_string_or_cfunits('K','kelvin',try_cfunits=False)
    False
    >>> get_are_units_equal_by_string_or_cfunits('K','kelvin',try_cfunits=True)
    True
    '''
    try:
        if try_cfunits:
            from cfunits import Units
            source_cfunits = Units(source)
            match = source_cfunits.equals(Units(target))
        else:
            raise(ImportError)
    except ImportError:
        match = source.lower() == target.lower()
    return(match)
Exemplo n.º 2
0
 def wrap(self, units=None):
     time = val_or_raise(func, (self._base, ))
     if units is not None:
         try:
             from_units = Units(self.get_time_units())
             to_units = Units(units)
         except AttributeError, NotImplementedError:
             pass
         else:
             if not from_units.equals(to_units):
                 time = Units.conform(time, from_units, to_units)
Exemplo n.º 3
0
    def time_from(self, time, units):
        if units is None:
            return time

        try:
            # units_str = self.time_units
            units_str = self.time_units
        except (AttributeError, NotImplementedError):
            pass
        else:
            to_units = Units(units_str)
            from_units = Units(units)

            if not from_units.equals(to_units):
                time = Units.conform(time, from_units, to_units)

        return time
Exemplo n.º 4
0
 def _convert(val, from_units, to_units):
     from_units, to_units = Units(from_units), Units(to_units)
     if from_units.equals(to_units):
         return val
     else:
         return Units.conform(val, from_units, to_units)