示例#1
0
def getPokemonWeight(pokemon):
    pokeWeight = pokemon['weight'] / 10
    if pokeWeight > 1:
        pokeWeight = str(
            round((massunits.MassUnit(pokeWeight, 'kg', 'lb').doconvert())))
        pokeWeight = f'{pokeWeight}.0 l b'
    else:
        pokeWeight = str(
            round((massunits.MassUnit(pokeWeight, 'kg', 'lb').doconvert()), 1))
        pokeWeight = f'{pokeWeight} l b'
    return pokeWeight
示例#2
0
async def mass(client, message):
    if len(message.command) <= 3:
        await self_destruct(message, "<code>Incorrect Syntax</code>")
        return

    value = message.command[1]
    curr1 = message.command[2].lower()
    curr2 = message.command[3].lower()
    try:
        conv = massunits.MassUnit(float(value), curr1, curr2).doconvert()
        await message.edit(
            f"<code>{value} {curr1} = {conv:,.2f} {curr2}</code>")
    except ValueError as err:
        await self_destruct(message, f"<code>{str(err)}</code>")
示例#3
0
    def determine_qty(self, product, purchase_amount, purchase_unit):
        '''
        determines the minimum quantity of product needed to fulfill
        purchase_amount in purchase_unit

        Note:
            when product["unit"] is in mass and purchase_unit is in volume
            product density is assumed to be that of water

        Args:
            product(dict): dict containing aspects of product to be added
            purchase_amount(float): amount of product needed
            purchase_unit(str): unit purchase_amount is measured in

        Returns:
            minimum quantity of product needed to fulfill purchase_amount
            in purchase_unit as an int
        '''
        purchase_unit = homogenize_unit(purchase_unit)
        vol_units = volumeunits.VolumeUnit(0, '_', '_').units
        mass_units = massunits.MassUnit(0, '_', '_').units
        item_unit = product['amount'][1]
        item_amount = product['amount'][0]
        if purchase_unit in mass_units:
            item_amount = massunits.MassUnit(item_amount, item_unit,
                                             purchase_unit)
        elif purchase_unit in vol_units:
            if item_unit in vol_units:
                item_amount = volumeunits.VolumeUnit(item_amount, item_unit,
                                                     purchase_unit)
            else:
                g = massunits.MassUnit(item_amount, item_unit, 'g').doconvert()
                item_amount = volumeunits.VolumeUnit(g, 'ml', purchase_unit)
        else:
            return purchase_amount
        return ceil(purchase_amount / item_amount.doconvert())
示例#4
0
def setSIUnits(result_json):
    results_units = {}
    for key, value in result_json.iteritems():
        if (isinstance(value[0], numbers.Number) == False) and (','
                                                                in value[0]):
            value[0] = value[0].replace(',', '')
        value[0] = float(value[0])
        unit = unit_translate[value[1]]
        if value[1] in unit_types['mass']:
            real_unit = massunits.MassUnit(value[0], unit, 'kg').doconvert()
        elif value[1] in unit_types['volume']:
            real_unit = volumeunits.VolumeUnit(value[0], unit, 'l').doconvert()
        else:
            if value[1] == 'ton':
                real_unit = value[0] * 907.18500036199
            elif value[1] == 'MT':
                real_unit = value[0] * 1000
            else:
                real_unit = value[0]

        results_units.update({key: real_unit})
    return results_units
示例#5
0
def conv3():
    a = massunits.MassUnit(userin.get(), f'{uf.get()}',
                           f'{us.get()}').doconvert()
    resultin.set(a)
def convertunits(x, output):
    """Converts a number into a different unit.\n
    Takes two string inputs where the first contains
    the number and unit, while the second contains the unit to convert to.\n
    This returns a list containing the new number, original number, initial unit, and converted unit."""

    # Keeps track of lower case units and lists for checking if units are valid
    digital = {
        'b': 'B',
        'kb': 'kB',
        'mb': 'MB',
        'gb': 'GB',
        'tb': 'TB',
        'pb': 'PB',
        'eb': 'EB',
        'zb': 'ZB',
        'yb': 'YB',
        'kib': 'KiB',
        'mib': 'MiB',
        'gib': 'GiB',
        'tib': 'TiB',
        'pib': 'PiB',
        'eib': 'EiB',
        'zib': 'ZiB',
        'yib': 'YiB'
    }
    length = {'mm', 'cm', 'in', 'ft', 'yd', 'm', 'km', 'mi'}
    time = {'ms', 'sec', 'min', 'hr', 'day', 'wk', 'yr'}
    volume = {
        'ml', 'l', 'tsp', 'tbsp', 'floz', 'cup', 'pt', 'qt', 'gal', 'lcup',
        'in3', 'ft3'
    }
    mass = {'mg', 'g', 'oz', 'lb', 'kg'}
    temperature = {'f': 'F', 'c': 'C', 'k': 'K'}

    # Splits the number and unit
    num = 0
    unit = ''
    try:
        if len(x.split()) > 1:
            num = int(x.split()[0])
            unit = x.split()[1]
        else:  # if its one word (e.g. 100kg)
            for i, c in enumerate(x):
                if not c.isdigit():
                    break
            num = int(x[:i])
            unit = x[i:]
    except:
        return [0, 0, '', '']

    # Uses unit convert and checks if the two units are in the same category
    if unit in digital and output in digital:
        return [
            digitalunits.DigitalUnit(num, digital[unit],
                                     digital[output]).doconvert(), num,
            digital[unit], digital[output]
        ]
    elif unit in length and output in length:
        return [
            lengthunits.LengthUnit(num, unit, output).doconvert(), num, unit,
            output
        ]
    elif unit in time and output in time:
        return [
            timeunits.TimeUnit(num, unit, output).doconvert(), num, unit,
            output
        ]
    elif unit in volume and output in volume:
        return [
            volumeunits.VolumeUnit(num, unit, output).doconvert(), num, unit,
            output
        ]
    elif unit in mass and output in mass:
        return [
            massunits.MassUnit(num, unit, output).doconvert(), num, unit,
            output
        ]
    elif unit in temperature and output in temperature:
        return [
            temperatureunits.TemperatureUnit(num, temperature[unit],
                                             temperature[output]).doconvert(),
            num, temperature[unit], temperature[output]
        ]
    else:
        return [0, 0, '', '']