diff options
author | Jules Chéron <43635101+jules-ch@users.noreply.github.com> | 2022-02-23 23:08:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-23 23:08:12 +0100 |
commit | 7ec296c94d98b965a8726f4bc3698545efe250d5 (patch) | |
tree | 5d6f1dd633e7dabac6193929b9d074360fcfb0d4 /docs | |
parent | ff15a1483b3b823bb35866436e084ba07a408377 (diff) | |
parent | 5f72edbcaa0f0f4ee2b3eadde566fdeb9845eb16 (diff) | |
download | pint-7ec296c94d98b965a8726f4bc3698545efe250d5.tar.gz |
Merge pull request #1422 from keewis/formatter-docs
add a example for a custom unit formatter
Diffstat (limited to 'docs')
-rw-r--r-- | docs/formatting.rst | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/docs/formatting.rst b/docs/formatting.rst index 5b4c91f..a261147 100644 --- a/docs/formatting.rst +++ b/docs/formatting.rst @@ -106,6 +106,24 @@ Spec Name Example ``C`` compact ``kilogram*meter/second**2`` ======= =============== ====================================================================== +Custom Unit Format Types +------------------------ +Using :py:func:`pint.register_unit_format`, it is possible to add custom +formats: + +.. ipython:: + + In [1]: u = ureg.Unit("m ** 3 / (s ** 2 * kg)") + + In [2]: @pint.register_unit_format("simple") + ...: def format_unit_simple(unit, registry, **options): + ...: return " * ".join(f"{u} ** {p}" for u, p in unit.items()) + + In [3]: f"{u:~simple}" + +where ``unit`` is a :py:class:`dict` subclass containing the unit names and +their exponents. + Quantity Format Specifications ------------------------------ The magnitude format is forwarded to the magnitude (for a unit-spec of ``H`` the |