예제 #1
0
 def test_allow_derivation(self):
     u = unitconv.determine_compatible_units('b', 'datasize', 1, 's', 'time', allow_integration=False)
     self.assertEqual(u['b'], (1.0, 'derive'))
     self.assertEqual(u['B'], (8.0, 'derive'))
     self.assertEqual(u['b/s'], (1.0, None))
     self.assertAlmostEqual(u['B/d'][0], 9.26e-05)
     self.assertIsNone(u['B/d'][1])
     self.assertNotIn('h', u)
예제 #2
0
 def test_allow_derivation(self):
     u = unitconv.determine_compatible_units('b', 'datasize', 1, 's', 'time')
     self.assertEqual(u['b'], (1.0, 'derive'))
     self.assertEqual(u['B'], (8.0, 'derive'))
     self.assertEqual(u['b/s'], (1.0, None))
     self.assertAlmostEqual(u['B/d'][0], 9.26e-05)
     self.assertIsNone(u['B/d'][1])
     self.assertNotIn('h', u)
예제 #3
0
    def test_compatible_to_simple_primary_type(self):
        all_time_units = [pair[0] for pair in unitconv.unit_classes_by_name['time']]
        u = unitconv.determine_compatible_units('s', 'time')
        compatunits = u.keys()

        for timeunit in all_time_units:
            self.assertIn(timeunit, compatunits)

        self.assertEqual(u['MM'], (60000000.0, None))
        self.assertEqual(u['h'], (3600.0, None))

        self.assertEqual([extra_op for (_multiplier, extra_op) in u.values()],
                         [None] * len(u))
예제 #4
0
    def test_compatible_to_simple_primary_type(self):
        all_time_units = [pair[0] for pair in unitconv.unit_classes_by_name['time']]
        u = unitconv.determine_compatible_units('s', 'time', allow_integration=False)
        compatunits = u.keys()

        for timeunit in all_time_units:
            self.assertIn(timeunit, compatunits)

        self.assertEqual(u['MM'], (60000000.0, None))
        self.assertEqual(u['h'], (3600.0, None))

        self.assertEqual([extra_op for (_multiplier, extra_op) in u.values()],
                         [None] * len(u))
def run_scenario(user_asked_for, data_exists_as, allow_derivation=True,
                 allow_integration=False, allow_prefixes_in_denominator=False,
                 round_result=6):
    userunit = unitconv.parse_unitname(user_asked_for)
    compatibles = unitconv.determine_compatible_units(
            allow_derivation=allow_derivation,
            allow_integration=allow_integration,
            allow_prefixes_in_denominator=allow_prefixes_in_denominator,
            **userunit)
    try:
        scale, extra_op = compatibles[data_exists_as]
    except KeyError:
        return
    if round_result is not None:
        scale = round(scale, round_result)
    return (data_exists_as, scale, extra_op)
예제 #6
0
def run_scenario(user_asked_for, data_exists_as, allow_derivation=True,
                 allow_integration=False, allow_prefixes_in_denominator=False,
                 round_result=6):
    userunit = unitconv.parse_unitname(user_asked_for, fold_scale_prefix=False)
    prefixclass = unitconv.prefix_class_for(userunit['scale_multiplier'])
    use_unit = userunit['base_unit']
    compatibles = unitconv.determine_compatible_units(
            allow_derivation=allow_derivation,
            allow_integration=allow_integration,
            allow_prefixes_in_denominator=allow_prefixes_in_denominator,
            **userunit)
    try:
        scale, extra_op = compatibles[data_exists_as]
    except KeyError:
        return
    if round_result is not None:
        scale = round(scale, round_result)
    return (data_exists_as, use_unit, scale, extra_op, prefixclass)
예제 #7
0
def run_scenario(user_asked_for, data_exists_as, allow_derivation=True,
                 allow_integration=False, allow_prefixes_in_denominator=False,
                 round_result=6):
    userunit = unitconv.parse_unitname(user_asked_for, fold_scale_prefix=False)
    prefixclass = unitconv.prefix_class_for(userunit['scale_multiplier'])
    use_unit = userunit['base_unit']
    compatibles = unitconv.determine_compatible_units(
            allow_derivation=allow_derivation,
            allow_integration=allow_integration,
            allow_prefixes_in_denominator=allow_prefixes_in_denominator,
            **userunit)
    try:
        scale, extra_op = compatibles[data_exists_as]
    except KeyError:
        return
    if round_result is not None:
        scale = round(scale, round_result)
    return (data_exists_as, use_unit, scale, extra_op, prefixclass)
예제 #8
0
파일: query.py 프로젝트: debedb/kupuestra2
    def transform_ast_for_compatible_units(cls, ast):
        if ast[0] == 'match_tag_equality' and ast[1] == 'unit':
            requested_unit = ast[2]
            unitinfo = unitconv.parse_unitname(requested_unit,
                                               fold_scale_prefix=False)
            prefixclass = unitconv.prefix_class_for(
                unitinfo['scale_multiplier'])
            use_unit = unitinfo['base_unit']
            compatibles = unitconv.determine_compatible_units(**unitinfo)

            # rewrite the search term to include all the alternates
            ast = ('match_or', ) + tuple([('match_tag_equality', 'unit', u)
                                          for u in compatibles.keys()])

            modifiers = [
                cls.convert_to_requested_unit_applier(compatibles),
                cls.variable_applier(unit=use_unit)
            ]
            if prefixclass == 'binary':
                modifiers.append(
                    cls.graph_config_applier(suffixes=prefixclass))
            return ast, modifiers
        elif ast[0] in ('match_and', 'match_or'):
            # recurse into subexpressions, in case they have unit=* terms
            # underneath. this won't be totally correct in case there's a way
            # to have multiple "unit=*" terms inside varying structures of
            # 'and' and 'or', but that's not exposed to the user yet anyway,
            # and auto-unit-conversion in that case probably isn't worth
            # supporting.
            new_target_modifiers = []
            newargs = []
            for sub_ast in ast[1:]:
                if isinstance(sub_ast, tuple):
                    sub_ast, mods = cls.transform_ast_for_compatible_units(
                        sub_ast)
                    new_target_modifiers.extend(mods)
                newargs.append(sub_ast)
            ast = (ast[0], ) + tuple(newargs)
            return ast, new_target_modifiers
        return ast, []
예제 #9
0
    def transform_ast_for_compatible_units(cls, ast):
        if ast[0] == 'match_tag_equality' and ast[1] == 'unit':
            requested_unit = ast[2]
            unitinfo = unitconv.parse_unitname(requested_unit, fold_scale_prefix=False)
            prefixclass = unitconv.prefix_class_for(unitinfo['scale_multiplier'])
            use_unit = unitinfo['base_unit']
            compatibles = unitconv.determine_compatible_units(**unitinfo)

            # rewrite the search term to include all the alternates
            ast = ('match_or',) + tuple(
                [('match_tag_equality', 'unit', u) for u in compatibles.keys()])

            modifiers = [
                cls.convert_to_requested_unit_applier(compatibles),
                cls.variable_applier(unit=use_unit)
            ]
            if prefixclass == 'binary':
                modifiers.append(cls.graph_config_applier(suffixes=prefixclass))
            return ast, modifiers
        elif ast[0] in ('match_and', 'match_or'):
            # recurse into subexpressions, in case they have unit=* terms
            # underneath. this won't be totally correct in case there's a way
            # to have multiple "unit=*" terms inside varying structures of
            # 'and' and 'or', but that's not exposed to the user yet anyway,
            # and auto-unit-conversion in that case probably isn't worth
            # supporting.
            new_target_modifiers = []
            newargs = []
            for sub_ast in ast[1:]:
                if isinstance(sub_ast, tuple):
                    sub_ast, mods = cls.transform_ast_for_compatible_units(sub_ast)
                    new_target_modifiers.extend(mods)
                newargs.append(sub_ast)
            ast = (ast[0],) + tuple(newargs)
            return ast, new_target_modifiers
        return ast, []
예제 #10
0
 def test_allow_integration(self):
     u = unitconv.determine_compatible_units('Eggnog', None, 0.125, allow_integration=True)
     self.assertEqual(u['Eggnog'], (8.0, None))
     self.assertAlmostEqual(u['Eggnog/h'][0], 0.0022222)
     self.assertEqual(u['Eggnog/h'][1], 'integrate')
     self.assertNotIn('derive', [extra_op for (_multiplier, extra_op) in u.values()])
예제 #11
0
 def test_allow_integration(self):
     u = unitconv.determine_compatible_units('Eggnog', None, 0.125, allow_integration=True)
     self.assertEqual(u['Eggnog'], (8.0, None))
     self.assertAlmostEqual(u['Eggnog/h'][0], 0.0022222)
     self.assertEqual(u['Eggnog/h'][1], 'integrate')
     self.assertNotIn('derive', [extra_op for (_multiplier, extra_op) in u.values()])