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)", )
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)')
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, )
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, )
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, )
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)")
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)')
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("]", "}")