Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed Jun 11, 2024
1 parent 4b931fa commit 6c89e09
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pint/delegates/formatter/_format_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def formatter(
power_fmt: str = "{} ** {}",
parentheses_fmt: str = "({0})",
exp_call: FORMATTER = "{:n}".format,
empty_numerator_fmt = "1",
) -> str:
"""Format a list of (name, exponent) pairs.
Expand All @@ -183,6 +184,8 @@ def formatter(
the format used for parenthesis. (Default value = "({0})")
exp_call : callable
(Default value = lambda x: f"{x:n}")
empty_numerator_fmt : str
the format used for an empty numerator. (Default value = "1")
Returns
-------
Expand Down Expand Up @@ -218,7 +221,7 @@ def formatter(
return join_u(product_fmt, pos_terms + neg_terms)

# Show as Ratio: positive terms / negative terms
pos_ret = join_u(product_fmt, pos_terms) or "1"
pos_ret = join_u(product_fmt, pos_terms) or empty_numerator_fmt

if not neg_terms:
return pos_ret
Expand Down
6 changes: 5 additions & 1 deletion pint/delegates/formatter/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ def format_unit(
) -> str:
uspec = uspec or self.default_format
sort_func = sort_func or self.default_sort_func
empty_numerator_fmt = "1"
if isinstance(unit._units, NonReducingUnitsContainer):
sort_func = _sort_func
empty_numerator_fmt = ""
return self.get_formatter(uspec).format_unit(
unit, uspec, sort_func=sort_func, **babel_kwds
unit, uspec, sort_func=sort_func,
empty_numerator_fmt = empty_numerator_fmt,
**babel_kwds
)

def format_quantity(
Expand Down
9 changes: 9 additions & 0 deletions pint/delegates/formatter/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def format_unit(
unit: PlainUnit | Iterable[tuple[str, Any]],
uspec: str = "",
sort_func: SortFunc | None = None,
empty_numerator_fmt = "1",
**babel_kwds: Unpack[BabelKwds],
) -> str:
"""Format a unit (can be compound) into string
Expand Down Expand Up @@ -203,6 +204,7 @@ def format_unit(
unit: PlainUnit | Iterable[tuple[str, Any]],
uspec: str = "",
sort_func: SortFunc | None = None,
empty_numerator_fmt = "1",
**babel_kwds: Unpack[BabelKwds],
) -> str:
numerator, denominator = prepare_compount_unit(
Expand All @@ -225,6 +227,7 @@ def format_unit(
division_fmt=division_fmt,
power_fmt="{}**{}",
parentheses_fmt=r"({})",
empty_numerator_fmt=empty_numerator_fmt,
)

def format_quantity(
Expand Down Expand Up @@ -313,12 +316,16 @@ def format_unit(
unit: PlainUnit | Iterable[tuple[str, Any]],
uspec: str = "",
sort_func: SortFunc | None = None,
empty_numerator_fmt="1",

**babel_kwds: Unpack[BabelKwds],

) -> str:
numerator, denominator = prepare_compount_unit(
unit,
uspec,
sort_func=sort_func,

**babel_kwds,
registry=self._registry,
)
Expand All @@ -339,6 +346,8 @@ def format_unit(
power_fmt="{}{}",
parentheses_fmt="({})",
exp_call=pretty_fmt_exponent,
empty_numerator_fmt=empty_numerator_fmt,

)

def format_quantity(
Expand Down

0 comments on commit 6c89e09

Please sign in to comment.