Example #1
0
 def func_regexsub(self, vals, units, refs, flags):
     if len(vals) == 4:
         return OperationResult(re.subn(StringsFeature.string(self, vals[1]), StringsFeature.string(self, vals[2]), StringsFeature.string(self, vals[0]), int(vals[3]))[0])
     elif len(vals) == 3:
         return OperationResult(re.sub(StringsFeature.string(self, vals[1]), StringsFeature.string(self, vals[2]), StringsFeature.string(self, vals[0])))
     else:
         raise CalculatorException("regexsub requires 3 or 4 arguments, found {0}".format(len(vals)))
Example #2
0
 def func_regexget(self, vals, units, refs, flags):
     found = re.findall(StringsFeature.string(self, vals[1]), StringsFeature.string(self, vals[0]))
     if len(vals) == 3:
         group = vals[2] - 1
         return OperationResult(found[int(group)])
     found = [OperandResult(f, None, None) for f in found]
     return OperationResult(found)
    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
Example #4
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
 def func_substr(self, vals, units, refs, flags):
     start = int(vals[1])
     if len(vals) == 2:
         return OperationResult(
             StringsFeature.string(self, vals[0])[start:])
     elif len(vals) == 3:
         end = int(vals[2]) + 1
         if end <= 0:
             end += len(vals[0])
         return OperationResult(
             StringsFeature.string(self, vals[0])[start:end])
     else:
         raise CalculatorException(
             "substr needs either 2 or 3 arguments, found {0}".format(
                 len(vals)))
 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
Example #7
0
 def func_split(self, vals, units, refs, flags):
     string = StringsFeature.string(self, vals[0])
     splitter = StringsFeature.string(self, vals[1])
     results = [
         OperandResult(r, None, None) for r in string.split(splitter)
     ]
     return OperationResult(results)
 def func_delete(self, vals, units, refs, flags):
     if isinstance(refs[0], VariableItem):
         varname = refs[0].var
         del self.vars[varname]
         return OperationResult(None)
     else:
         raise ExecuteException("Expecting variable, received".format(refs[0].desc()), [], None)
    def op_bitwise_not(self, vals, units, refs, flags):
        dec_num, num_type = self.number(vals[0])

        if dec_num < 0 or dec_num % 1 != 0:
            raise CalculatorException('Operator requires positive integers')

        int_val = int(dec_num)
        if num_type and 'width' in num_type.opts:
            mask_val = 2**num_type.opts['width'] - 1
        else:
            mask_val = 2**math.ceil(math.log(int_val, 2)) - 1
        flipped_val = ~int_val
        masked_flipped_val = flipped_val & mask_val

        if num_type:
            return OperationResult(num_type.restore(self, masked_flipped_val))
        return OperationResult(Decimal(masked_flipped_val))
Example #10
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
 def func_dateformat(self, vals, units, refs, flags):
     dt = DatesFeature.string_to_date(self, vals[0])
     dt_format, dt_padding = DatesFeature.correct_format_for_date(self, dt)
     return OperationResult(
         format(
             dt.strftime(self.feature_options['dates.datefunctions'][
                 'Human Readable ' + dt_format]),
             self.feature_options['dates.datefunctions']['Human Readable ' +
                                                         dt_padding]))
Example #12
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
Example #14
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)
Example #15
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 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
Example #17
0
 def op_number_morethanequal(self, vals, units, refs, flags):
     return OperationResult(vals[0] >= vals[1])
 def func_upper(self, vals, units, refs, flags):
     return OperationResult(StringsFeature.string(self, vals[0]).upper())
 def func_length(self, vals, units, refs, flags):
     return OperationResult(
         Decimal(len(StringsFeature.string(self, vals[0]))))
 def func_strip(self, vals, units, refs, flags):
     return OperationResult(
         StringsFeature.string(self, vals[0]).strip(
             StringsFeature.string(self, vals[1])))
Example #21
0
 def op_number_modulus(self, vals, units, refs, flags):
     return OperationResult(vals[0] % vals[1])
Example #22
0
 def func_regexcount(self, vals, units, refs, flags):
     return OperationResult(Decimal(len(re.findall(StringsFeature.string(self, vals[1]), StringsFeature.string(self, vals[0])))))
Example #23
0
 def op_regexnot(self, vals, units, refs, flags):
     return OperationResult((re.search(StringsFeature.string(self, vals[1]), StringsFeature.string(self, vals[0])) is None))
Example #24
0
 def func_regexsplit(self, vals, units, refs, flags):
     found = re.split(StringsFeature.string(self, vals[1]), StringsFeature.string(self, vals[0]))
     found = [OperandResult(f, None, None) for f in found]
     return OperationResult(found)
 def op_bitwise_rshift(self, vals, units, refs, flags):
     return OperationResult(Decimal(int(vals[0]) >> int(vals[1])))
Example #26
0
 def func_join(self, vals, units, refs, flags):
     delimiter = ''
     if len(vals) > 1:
         delimiter = vals[1]
     values = [StringsFeature.string(self, v.value) for v in vals[0]]
     return OperationResult(delimiter.join(values))
Example #27
0
 def op_boolean_not(self, vals, units, refs, flags):
     return OperationResult(not BooleansFeature.boolean(self, vals[0]))
Example #28
0
 def op_number_notequals(self, vals, units, refs, flags):
     return OperationResult(vals[0] != vals[1])
Example #29
0
 def op_boolean_xor(self, vals, units, refs, flags):
     return OperationResult(BooleansFeature.boolean(self, vals[0]) != BooleansFeature.boolean(self, vals[1]))
Example #30
0
 def op_number_lessthan(self, vals, units, refs, flags):
     return OperationResult(vals[0] < vals[1])