Esempio n. 1
0
 def op_number_integer_divide(self, vals, units, refs, flags):
     op = self.ops_list['/']
     division_result = op.call(self, [
         OperandResult(vals[0], units[0], None),
         OperandResult(vals[1], units[1], None)
     ], flags)
     res = OperationResult(Decimal(math.floor(division_result.value)))
     res.set_unit(division_result.unit)
     return res
Esempio n. 2
0
 def op_boolean_conditional(self, vals, units, refs, flags):
     if BooleansFeature.boolean(self, vals[0]):
         res = OperationResult(vals[1])
         res.set_unit(units[1])
         res.set_ref(refs[1])
         return res
     res = OperationResult(vals[2])
     res.set_unit(units[2])
     res.set_ref(refs[2])
     return res
Esempio n. 3
0
 def op_var_set(self, vals, units, refs, flags):
     if isinstance(refs[0], VariableItem):
         if 'stateless' not in flags.keys() or not flags['stateless']:
             self.vars[refs[0].var] = (vals[1], units[1])
         res = OperationResult(vals[1])
         res.set_unit(units[1])
         return res
     else:
         raise ExecuteException(
             "Expecting variable, received".format(refs[0].desc()), [],
             None)
    def func_dateadd(self, vals, units, refs, flags):
        dt = DatesFeature.string_to_date(self, vals[0])

        num, fromunit = vals[1], units[1]
        tounit = self.unit_normaliser.get_unit('seconds')
        seconds, tounit = self.unit_normaliser.unit_conversion(
            num, fromunit, tounit, False)
        td = timedelta(seconds=float(seconds))

        dt += td

        res = OperationResult(DatesFeature.date_to_string(self, dt))
        res.set_unit(None)
        return res
 def op_number_power(self, vals, units, refs, flags):
     power_numtype = self.number(vals[1])
     power = power_numtype[0]
     num_type = power_numtype[1]
     if isinstance(vals[0], UnitPowerList):
         unit = vals[0].power(power)
         return OperationResult(unit)
     num_res = vals[0]**power
     restored_number = self.restore_number_type(num_res, num_type)
     res = OperationResult(restored_number)
     if units[1] is not None:
         raise CalculatorException("Power operand must be simple number")
     if units[0] is not None:
         res.set_unit(units[0].power(power))
     return res
Esempio n. 6
0
 def assignment_operator(self, sym, vals, units, refs, flags):
     if isinstance(refs[0], VariableItem):
         if 'stateless' not in flags.keys() or not flags['stateless']:
             varname = refs[0].var
             var = self.vars[varname]
             op = self.ops_list[sym]
             op_result = op.call(self, [
                 OperandResult(var[0], var[1], None),
                 OperandResult(vals[1], units[1], refs[1])
             ], flags)
             self.vars[varname] = (op_result.value, op_result.unit)
         res = OperationResult(vals[1])
         res.set_unit(units[1])
         return res
     else:
         raise ExecuteException(
             "Expecting variable, received".format(refs[0].desc()), [],
             None)
Esempio n. 7
0
    def op_unit_conversion(self, vals, units, refs, flags):
        if isinstance(vals[0], UnitPowerList):
            units[0] = vals[0]
            vals[0] = Decimal('1')
        num = vals[0]
        if vals[1] is not None and not isinstance(vals[1], UnitPowerList) and vals[1] != Decimal('1'):
            raise CalculatorException("Second operand must be just a unit")
        fromunit = units[0]
        tounit = vals[1]
        if tounit is None:
            raise CalculatorException("Second operand is not set")
        if not isinstance(tounit, UnitPowerList):
            raise CalculatorException("Second operand is not a unit")

        num, tounit = self.unit_normaliser.unit_conversion(num, fromunit, tounit, False)
        tounit.no_simplify = True

        res = OperationResult(num)
        res.set_unit(tounit)
        return res
    def op_number_multiply(self, vals, units, refs, flags):
        if isinstance(vals[0], UnitPowerList) and isinstance(
                vals[1], UnitPowerList):
            unit = UnitPowerList.new([vals[0], 1, vals[1], 1])
            unit.no_simplify = True
            return OperationResult(unit)
        for i in range(0, len(vals)):
            if isinstance(vals[i], UnitPowerList):
                units[i] = vals[i]
                vals[i] = Decimal('1')

        num_type = self.number(vals[0])[1]
        val0 = self.number(vals[0])[0]
        val1 = self.number(vals[1])[0]
        restored_number = self.restore_number_type(val0 * val1, num_type)
        res = OperationResult(restored_number)

        if units[0] is not None and units[1] is not None:
            unit = UnitPowerList.new([units[0], 1, units[1], 1])
            res.set_unit(unit)
        elif units[0] is not None:
            res.set_unit(units[0])
        elif units[1] is not None:
            res.set_unit(units[1])
        return res
    def func_datedifference(self, vals, units, refs, flags):
        date1 = DatesFeature.string_to_date(self, vals[0])
        date2 = DatesFeature.string_to_date(self, vals[1])
        if ((date1.utcoffset() is None) != (date2.utcoffset() is None)):
            raise CalculatorException(
                "Cannot compare dates with timezones and dates without timezones"
            )
        td = abs(date1 - date2)

        seconds = Decimal(td.total_seconds())
        seconds = round(seconds, min(6, getcontext().prec))
        fromunit = UnitPowerList.new(
            [self.unit_normaliser.get_unit('seconds'), 1])
        tounit = UnitPowerList.new(
            [self.unit_normaliser.get_unit('seconds'), 1])
        value = seconds
        if len(vals) >= 3:
            tounit = vals[2]
            value, tounit = self.unit_normaliser.unit_conversion(
                seconds, fromunit, tounit, False)

        res = OperationResult(value)
        res.set_unit(tounit)
        return res
Esempio n. 10
0
    def func_format(self, vals, units, refs, flags):
        num, unit = vals[0], units[0]
        num, unit = self.unit_normaliser.simplify_units(num, unit)
        if unit is None:
            raise CalculatorException("Input must have a unit")
        if len(unit.list()) != 1:
            raise CalculatorException("Can only format single units")
        power = unit.list()[0].power
        if power != 1:
            raise CalculatorException("Unit must have power of 1")

        singleunit = unit.list()[0].unit
        if len(vals) >= 2:
            system = vals[1]
        else:
            if singleunit.systems is None or len(singleunit.systems) == 0:
                raise CalculatorException(
                    "Unit is not part of a measuring system")
            system = self.unit_normaliser.get_preferred_system(
                singleunit.systems)

        systemunits = self.unit_normaliser.get_system(system,
                                                      singleunit.dimension)
        if len(systemunits) == 0:
            raise CalculatorException(
                "No units in system {0} found for dimension {1}".format(
                    system, singleunit.dimension))
        nonprefixedsystemunits = [
            s for s in systemunits if s.nameprefix is None
        ]

        parts = []
        for systemunit in nonprefixedsystemunits:
            num = unit.convertfrom(num, False)
            num = systemunit.convertto(num, False)
            unit = systemunit
            last = (systemunit == nonprefixedsystemunits[-1])
            if num >= Decimal('1') or last:
                if last:
                    num_of_unit, unit = GeneralUnitFunctionsFeature.find_first_unit_prefix_at_least_one(
                        self, num, unit, systemunits)
                else:
                    num_of_unit = self.floor_number(self.round_number(num))
                    num_of_unit, new_unit = GeneralUnitFunctionsFeature.find_first_unit_prefix_at_least_one(
                        self, num_of_unit, unit, systemunits)
                    if new_unit == unit:
                        num -= num_of_unit
                    else:
                        num = unit.convertfrom(num, False)
                        num = new_unit.convertto(num, False)
                        num -= num_of_unit
                        unit = new_unit
                num_of_unit = self.round_number(num_of_unit)
                if num_of_unit != Decimal('0'):
                    parts.append("{0} {1}".format(
                        self.number_to_string(num_of_unit),
                        unit.get_name(num_of_unit)))

        res = OperationResult(str.join(', ', parts))
        res.set_unit(None)
        return res
Esempio n. 11
0
 def func_compact(self, vals, units, refs, flags):
     value, unit = self.unit_normaliser.simplify_units(vals[0], units[0])
     res = OperationResult(value)
     res.set_unit(unit)
     return res