Beispiel #1
0
def parseUnit(units, conversions, tokens, overwrite=False):
    """
	Convert the next series of tokens into a unit
	@param units: a map of unit symbols to unit objects to be modified
	@param conversions: a map of unit symbols to scale factors to be modified
	@param tokens: a list of tokens
	"""
    baseUnitMap = {}

    # Handle base unit
    sym = UC_Utils.parseSymbol(tokens)
    if not overwrite and (sym in units):
        raise UC_Common.FileFormatError(
            f"Duplicate definition of unit '{sym}'")

    # Handle derived unit
    nextToken = UC_Utils.getNextToken(tokens)
    if nextToken == UC_Common.MAP_DELIMITER:
        scaleFactor = UC_Utils.parseFloat(tokens)
        UC_Utils.getNextToken(tokens, UC_Common.SEP_DELIMITER)
        baseUnitMap = parseBaseUnitMap(tokens)
        conversions[sym] = scaleFactor

    # Handle other tokens
    elif nextToken != UC_Common.END_DELIMITER:
        raise UC_Common.FileFormatError(
            f"Expected delimiter; received '{nextToken}'")

    # Create unit
    units[sym] = UC_Unit.Unit(sym, baseUnitMap)
Beispiel #2
0
def parsePrefixMapping(prefixes, tokens, base, overwrite=False):
    """
	Convert the next series of tokens into a prefix-exponent pair
	@param prefixes: the prefix-exponent map to modify
	@param tokens: a list of tokens
	@param base: the base for the exponent
	"""
    prefix = UC_Utils.getNextToken(tokens)
    if not overwrite and (prefix in prefixes):
        raise UC_Common.FileFormatError(
            f"Duplicate definition of prefix '{prefix}'")
    prefixes[prefix] = (base, Decimal(UC_Utils.parseInt(tokens)))