def handle_unit(unit_type): """ Handles a handed over unit by creating the corresponding unit-type, storing it in the list of predefined units, creating a type symbol and returning it. :param unit_type: a single sympy unit symbol :type unit_type: Symbol (sympy) :return: a new type symbol :rtype: type_symbol """ # first ensure that it does not already exists, if not create it and register it in the set of predefined units # first clean up the unit of not required components, here it is the 1.0 in front of the unit # e.g., 1.0 * 1 / ms. This step is not mandatory for correctness, but makes reporting easier if isinstance(unit_type, units.Quantity) and unit_type.value == 1.0: to_process = unit_type.unit else: to_process = unit_type if str(to_process) not in PredefinedUnits.get_units().keys(): unit_type_t = UnitType(name=str(to_process), unit=to_process) PredefinedUnits.register_unit(unit_type_t) # now create the corresponding type symbol if it does not exists if PredefinedTypes.get_type(str(to_process)) is None: type_symbol = UnitTypeSymbol( unit=PredefinedUnits.get_unit(str(to_process))) PredefinedTypes.register_type(type_symbol) return PredefinedTypes.get_type(name=str(to_process))
def register_unit(cls, unit): """ Registers a new astropy unit into the system :param unit: an astropy Unit object :type unit: astropy.units.core.Unit """ unit_type = UnitType(str(unit), unit) PredefinedUnits.register_unit(unit_type) type_symbol = UnitTypeSymbol(unit=unit_type) cls.register_type(type_symbol) return
def register_unit(cls, unit): """ Registers a new sympy unit into the system :param unit: a sympy unit. :type unit: SympyUnit """ unit_type = UnitType(str(unit), unit) PredefinedUnits.register_unit(unit_type) type_symbol = UnitTypeSymbol(unit=unit_type) cls.register_type(type_symbol) return
def handle_unit(unit_type): """ Handles a handed over unit by creating the corresponding unit-type, storing it in the list of predefined units, creating a type symbol and returning it. :param unit_type: astropy unit object :type unit_type: astropy.units.core.Unit :return: a new type symbol :rtype: TypeSymbol """ # first ensure that it does not already exists, if not create it and register it in the set of predefined units # first clean up the unit of not required components, here it is the 1.0 in front of the unit # e.g., 1.0 * 1 / ms. This step is not mandatory for correctness, but makes reporting easier if isinstance(unit_type, units.Quantity) and unit_type.value == 1.0: to_process = unit_type.unit else: to_process = unit_type if str(to_process) not in PredefinedUnits.get_units().keys(): unit_type_t = UnitType(name=str(to_process), unit=to_process) PredefinedUnits.register_unit(unit_type_t) # now create the corresponding type symbol if it does not exists if PredefinedTypes.get_type(str(to_process)) is None: type_symbol = UnitTypeSymbol(unit=PredefinedUnits.get_unit(str(to_process))) PredefinedTypes.register_type(type_symbol) return PredefinedTypes.get_type(name=str(to_process))