Ejemplo n.º 1
0
    def __init__(self, unit_1=None, unit_2=None, trivial=False, inverse=False,
                 scale=None, offset=None, base=None, power=None):
        """Initialize a converter.
        """
        if trivial:
            self.this = ut.cv_get_trivial()
        elif inverse:
            self.this = ut.cv_get_inverse()
        elif scale is not None and offset is not None:
            self.this = ut.cv_get_galilean(scale, offset)
        elif scale is not None:
            self.this = ut.cv_get_scale(scale)
        elif offset is not None:
            self.this = ut.cv_get_offset(offset)
        elif base is not None:
            self.this = ut.cv_get_log(base)
        elif power is not None:
            self.this = ut.cv_get_pow(power)
        else:
            if unit_1 is None or unit_2 is None:
                raise TypeError('\'unit_1\' and \'unit_2\' cannot be None')

            if isinstance(unit_1, str):
                unit_1 = Unit(unit_1)
            if isinstance(unit_2, str):
                unit_2 = Unit(unit_2)
            self.this = ut.get_converter(unit_1.this, unit_2.this)

        if self.this is None:
            raise UdunitsError(Converter.__init__.__name__, ut.get_status())
Ejemplo n.º 2
0
    def get_dimensionless_unit_one(self):
        ret = Unit(system=self.this)
        ret.this = ut.get_dimensionless_unit_one(self.this)
        if not ret.this:
            raise UdunitsError(System.get_dimensionless_unit_one.__name__, ut.get_status())

        return ret
Ejemplo n.º 3
0
    def get_unit_by_name(self, name):
        ret = Unit(system=self.this)
        ret.this = ut.get_unit_by_name(self.this, name)
        if not ret.this:
            raise UdunitsError(System.get_unit_by_name.__name__, ut.get_status(), 'No unit with name \'{0}\' in system'.format(name))

        return ret
Ejemplo n.º 4
0
    def get_unit_by_symbol(self, symbol):
        ret = Unit(system=self.this)
        ret.this = ut.get_unit_by_symbol(self.this, symbol)
        if not ret.this:
            raise UdunitsError(System.get_unit_by_symbol.__name__, ut.get_status(), 'No unit with symbol \'{0}\' in system'.format(symbol))

        return ret
Ejemplo n.º 5
0
    def get_dimensionless_unit_one(self):
        ret = Unit(system=self.this)
        ret.this = ut.get_dimensionless_unit_one(self.this)
        if not ret.this:
            raise UdunitsError(System.get_dimensionless_unit_one.__name__,
                               ut.get_status())

        return ret
Ejemplo n.º 6
0
    def get_unit_by_symbol(self, symbol):
        ret = Unit(system=self.this)
        ret.this = ut.get_unit_by_symbol(self.this, symbol)
        if not ret.this:
            raise UdunitsError(
                System.get_unit_by_symbol.__name__, ut.get_status(),
                'No unit with symbol \'{0}\' in system'.format(symbol))

        return ret
Ejemplo n.º 7
0
    def get_unit_by_name(self, name):
        ret = Unit(system=self.this)
        ret.this = ut.get_unit_by_name(self.this, name)
        if not ret.this:
            raise UdunitsError(
                System.get_unit_by_name.__name__, ut.get_status(),
                'No unit with name \'{0}\' in system'.format(name))

        return ret
Ejemplo n.º 8
0
    def combine(self, other):
        if not isinstance(other, Converter):
            raise TypeError('\'other\' must be of type Converter')

        result = Converter(trivial=True)
        result.this = ut.cv_combine(other.this, self.this)
        if not result.this:
            raise UdunitsError(Converter.__call__.__name__, ut.get_status(), 'ut_cv_combine failure')

        return result
Ejemplo n.º 9
0
    def __init__(self, spec="", system=None, encoding=None):
        """Initialize a unit. Calls ut_parse().
        """
        if not system and isinstance(system, (str, unicode)):
            system = System(path=system)
        self.system = system or DEFAULT_SYSTEM
        self.this = ut.parse(self.system.this, spec, encoding or UT_ASCII)

        if not self.this:
            raise UdunitsError(Unit.__init__.__name__, ut.get_status())
Ejemplo n.º 10
0
    def __init__(self, spec = "", system=None, encoding=None):
        """Initialize a unit. Calls ut_parse().
        """
        if not system and isinstance(system, (str, unicode)):
            system = System(path=system)
        self.system = system or DEFAULT_SYSTEM
        self.this = ut.parse(self.system.this, spec, encoding or UT_ASCII)

        if not self.this:
            raise UdunitsError(Unit.__init__.__name__, ut.get_status())
Ejemplo n.º 11
0
    def combine(self, other):
        if not isinstance(other, Converter):
            raise TypeError('\'other\' must be of type Converter')

        result = Converter(trivial=True)
        result.this = ut.cv_combine(other.this, self.this)
        if not result.this:
            raise UdunitsError(Converter.__call__.__name__, ut.get_status(),
                               'ut_cv_combine failure')

        return result
Ejemplo n.º 12
0
    def __init__(self, path=None, empty=False):
        """Creates a unit system
        """
        if empty:
            self.this = ut.new_system()
        elif path:
            self.this = ut.read_xml(path)
        else:
            self.this = ut.read_xml(None)

        if not self.this:
            raise UdunitsError(System.__init__.__name__, ut.get_status())
Ejemplo n.º 13
0
    def __init__(self, path=None, empty=False):
        """Creates a unit system
        """
        if empty:
            self.this = ut.new_system()
        elif path:
            self.this = ut.read_xml(path)
        else:
            self.this = ut.read_xml(None)

        if not self.this:
            raise UdunitsError(System.__init__.__name__, ut.get_status())
Ejemplo n.º 14
0
    def __init__(self,
                 unit_1=None,
                 unit_2=None,
                 trivial=False,
                 inverse=False,
                 scale=None,
                 offset=None,
                 base=None,
                 power=None):
        """Initialize a converter.
        """
        if trivial:
            self.this = ut.cv_get_trivial()
        elif inverse:
            self.this = ut.cv_get_inverse()
        elif scale is not None and offset is not None:
            self.this = ut.cv_get_galilean(scale, offset)
        elif scale is not None:
            self.this = ut.cv_get_scale(scale)
        elif offset is not None:
            self.this = ut.cv_get_offset(offset)
        elif base is not None:
            self.this = ut.cv_get_log(base)
        elif power is not None:
            self.this = ut.cv_get_pow(power)
        else:
            if unit_1 is None or unit_2 is None:
                raise TypeError('\'unit_1\' and \'unit_2\' cannot be None')

            if isinstance(unit_1, str):
                unit_1 = Unit(unit_1)
            if isinstance(unit_2, str):
                unit_2 = Unit(unit_2)
            self.this = ut.get_converter(unit_1.this, unit_2.this)

        if self.this is None:
            raise UdunitsError(Converter.__init__.__name__, ut.get_status())
Ejemplo n.º 15
0
 def add_symbol_prefix(self, symbol, value):
     res = ut.add_symbol_prefix(self.this, symbol, value)
     if res: # anything other than 0
         raise UdunitsError(System.add_symbol_prefix.__name__, ut.get_status())
Ejemplo n.º 16
0
    def new_base_unit(self):
        res = ut.new_base_unit(self.this)
        if not res:
            raise UdunitsError(System.new_base_unit.__name__, ut.get_status())

        return res
Ejemplo n.º 17
0
    def new_base_unit(self):
        res = ut.new_base_unit(self.this)
        if not res:
            raise UdunitsError(System.new_base_unit.__name__, ut.get_status())

        return res
Ejemplo n.º 18
0
 def add_symbol_prefix(self, symbol, value):
     res = ut.add_symbol_prefix(self.this, symbol, value)
     if res:  # anything other than 0
         raise UdunitsError(System.add_symbol_prefix.__name__,
                            ut.get_status())