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())
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
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
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
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
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
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
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())
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())
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())
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())
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