def test_formatter(self):
        self.assertEqual(fmt.formatter(dict().items()), "")
        self.assertEqual(fmt.formatter(dict(meter=1).items()), "meter")
        self.assertEqual(fmt.formatter(dict(meter=-1).items()), "1 / meter")
        self.assertEqual(fmt.formatter(dict(meter=-1).items(), as_ratio=False),
                         "meter ** -1")

        self.assertEqual(
            fmt.formatter(dict(meter=-1, second=-1).items(), as_ratio=False),
            "meter ** -1 * second ** -1",
        )
        self.assertEqual(fmt.formatter(dict(meter=-1, second=-1).items()),
                         "1 / meter / second")
        self.assertEqual(
            fmt.formatter(dict(meter=-1, second=-1).items(),
                          single_denominator=True),
            "1 / (meter * second)",
        )
        self.assertEqual(fmt.formatter(dict(meter=-1, second=-2).items()),
                         "1 / meter / second ** 2")
        self.assertEqual(
            fmt.formatter(dict(meter=-1, second=-2).items(),
                          single_denominator=True),
            "1 / (meter * second ** 2)",
        )
Пример #2
0
    def test_formatter(self):
        self.assertEqual(fmt.formatter(dict().items()), '')
        self.assertEqual(fmt.formatter(dict(meter=1).items()), 'meter')
        self.assertEqual(fmt.formatter(dict(meter=-1).items()), '1 / meter')
        self.assertEqual(fmt.formatter(dict(meter=-1).items(), as_ratio=False), 'meter ** -1')

        self.assertEqual(fmt.formatter(dict(meter=-1, second=-1).items(), as_ratio=False),
                         'meter ** -1 * second ** -1')
        self.assertEqual(fmt.formatter(dict(meter=-1, second=-1).items()),
                         '1 / meter / second')
        self.assertEqual(fmt.formatter(dict(meter=-1, second=-1).items(), single_denominator=True),
                         '1 / (meter * second)')
        self.assertEqual(fmt.formatter(dict(meter=-1, second=-2).items()),
                         '1 / meter / second ** 2')
        self.assertEqual(fmt.formatter(dict(meter=-1, second=-2).items(), single_denominator=True),
                         '1 / (meter * second ** 2)')
Пример #3
0
def format_compact(unit, registry, **options):
    return formatting.formatter(
        unit.items(),
        as_ratio=False,
        single_denominator=False,
        product_fmt="*",
        division_fmt="/",
        power_fmt="{}**{}",
        parentheses_fmt=r"({})",
        **options,
    )
Пример #4
0
def format_html(unit, registry, **options):
    return formatting.formatter(
        unit.items(),
        as_ratio=False,
        single_denominator=True,
        product_fmt=r".",
        division_fmt=r"{}/{}",
        power_fmt=r"{}<sup>{}</sup>",
        parentheses_fmt=r"({})",
        **options,
    )
Пример #5
0
def format_pretty(unit, registry, **options):
    return formatting.formatter(
        unit.items(),
        as_ratio=False,
        single_denominator=False,
        product_fmt=".",
        division_fmt="/",
        power_fmt="{}{}",
        parentheses_fmt="({})",
        exp_call=formatting._pretty_fmt_exponent,
        **options,
    )
Пример #6
0
    def test_formatter(self):
        assert fmt.formatter(dict().items()) == ""
        assert fmt.formatter(dict(meter=1).items()) == "meter"
        assert fmt.formatter(dict(meter=-1).items()) == "1 / meter"
        assert fmt.formatter(dict(meter=-1).items(),
                             as_ratio=False) == "meter ** -1"

        assert (fmt.formatter(dict(meter=-1, second=-1).items(),
                              as_ratio=False) == "meter ** -1 * second ** -1")
        assert fmt.formatter(dict(meter=-1,
                                  second=-1).items()) == "1 / meter / second"
        assert (fmt.formatter(
            dict(meter=-1, second=-1).items(),
            single_denominator=True) == "1 / (meter * second)")
        assert (fmt.formatter(dict(
            meter=-1, second=-2).items()) == "1 / meter / second ** 2")
        assert (fmt.formatter(
            dict(meter=-1, second=-2).items(),
            single_denominator=True) == "1 / (meter * second ** 2)")
Пример #7
0
    def test_formatter(self):
        self.assertEqual(fmt.formatter(dict().items()), '')
        self.assertEqual(fmt.formatter(dict(meter=1).items()), 'meter')
        self.assertEqual(fmt.formatter(dict(meter=-1).items()), '1 / meter')
        self.assertEqual(fmt.formatter(dict(meter=-1).items(), as_ratio=False),
                         'meter ** -1')

        self.assertEqual(
            fmt.formatter(dict(meter=-1, second=-1).items(), as_ratio=False),
            'meter ** -1 * second ** -1')
        self.assertEqual(fmt.formatter(dict(meter=-1, second=-1).items()),
                         '1 / meter / second')
        self.assertEqual(
            fmt.formatter(dict(meter=-1, second=-1).items(),
                          single_denominator=True), '1 / (meter * second)')
        self.assertEqual(fmt.formatter(dict(meter=-1, second=-2).items()),
                         '1 / meter / second ** 2')
        self.assertEqual(
            fmt.formatter(dict(meter=-1, second=-2).items(),
                          single_denominator=True),
            '1 / (meter * second ** 2)')
Пример #8
0
def format_latex(unit, registry, **options):
    preprocessed = {
        r"\mathrm{{{}}}".format(u.replace("_", r"\_")): p
        for u, p in unit.items()
    }
    formatted = formatting.formatter(
        preprocessed.items(),
        as_ratio=False,
        single_denominator=True,
        product_fmt=r" \cdot ",
        division_fmt=r"\frac[{}][{}]",
        power_fmt="{}^[{}]",
        parentheses_fmt=r"\left({}\right)",
        **options,
    )
    return formatted.replace("[", "{").replace("]", "}")