Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed Apr 19, 2024
1 parent 78df901 commit 1c1ebdd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
28 changes: 28 additions & 0 deletions pint/delegates/formatter/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
PlainQuantity,
PlainUnit,
)
from ...facets.kind import KindKind, QuantityKind
from ...registry import UnitRegistry


Expand Down Expand Up @@ -130,6 +131,19 @@ def format_unit(
unit, uspec, sort_func=sort_func, **babel_kwds
)

def format_kind(
self,
kind: KindKind | Iterable[tuple[str, Any]],
uspec: str = "",
sort_func: SortFunc | None = None,
**babel_kwds: Unpack[BabelKwds],
) -> str:
uspec = uspec or self.default_format
sort_func = sort_func or self.default_sort_func
return self.get_formatter(uspec).format_kind(
kind, uspec, sort_func=sort_func, **babel_kwds
)

def format_quantity(
self,
quantity: PlainQuantity[MagnitudeT],
Expand Down Expand Up @@ -166,6 +180,20 @@ def format_quantity(
length=babel_kwds.get("length", None),
locale=locale,
)

def format_quantitykind(
self,
quantitykind: QuantityKind | Iterable[tuple[str, Any]],
uspec: str = "",
sort_func: SortFunc | None = None,
**babel_kwds: Unpack[BabelKwds],
) -> str:
uspec = uspec or self.default_format
sort_func = sort_func or self.default_sort_func
return self.get_formatter(uspec).format_quantitykind(
quantitykind, uspec, sort_func=sort_func, **babel_kwds
)


def format_measurement(
self,
Expand Down
38 changes: 38 additions & 0 deletions pint/delegates/formatter/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
)

if TYPE_CHECKING:
from ...facets.kind import KindKind
from ...facets.measurement import Measurement
from ...facets.plain import MagnitudeT, PlainQuantity, PlainUnit
from ...registry import UnitRegistry
Expand All @@ -51,6 +52,42 @@ class BaseFormatter:
def __init__(self, registry: UnitRegistry | None = None):
self._registry = registry

def format_kind(
self,
kind: KindKind | Iterable[tuple[str, Any]],
uspec: str = "",
sort_func: SortFunc | None = None,
**babel_kwds: Unpack[BabelKwds],
) -> str:
"""Format a unit (can be compound) into string
given a string formatting specification and locale related arguments.
"""
kind = kind._kinds
return self.format_unit(kind, uspec, sort_func, **babel_kwds)

def format_quantitykind(
self,
quantitykind: QuantityKind,
qspec: str = "",
sort_func: SortFunc | None = None,
**babel_kwds: Unpack[BabelKwds],
) -> str:
"""Format a quantitykind into string
given a string formatting specification and locale related arguments.
"""
quantity = quantitykind.quantity

registry = self._registry

mspec, uspec = split_format(
qspec, registry.formatter.default_format, registry.separate_format_defaults
)

mu = self.format_quantity(quantity, qspec, sort_func, **babel_kwds)
k = self.format_kind(quantitykind.kinds, uspec, sort_func, **babel_kwds)
return mu + " " + k



class DefaultFormatter(BaseFormatter):
"""Simple, localizable plain text formatter.
Expand Down Expand Up @@ -112,6 +149,7 @@ def format_unit(
parentheses_fmt=r"({})",
)


def format_quantity(
self,
quantity: PlainQuantity[MagnitudeT],
Expand Down
14 changes: 6 additions & 8 deletions pint/facets/kind/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def __new__(cls, value, kinds, units=None):
"units must be of type str, PlainQuantity or "
"UnitsContainer; not {}.".format(type(units))
)
if isinstance(value, cls):
if isinstance(value, PlainQuantity):
magnitude = value.to(units)._magnitude
else:
magnitude = _to_magnitude(
Expand All @@ -121,17 +121,15 @@ def __new__(cls, value, kinds, units=None):

return inst

def __repr__(self):
return "<QuantityKind({}, {}, {})>".format(
self.magnitude, self._kinds, self.units
)
def __repr__(self) -> str:
return f"<QuantityKind({self._magnitude}, {self._units}, {self._kinds})>"

def __str__(self):
return f"{self}"

# def __format__(self, spec):
# spec = spec or self._REGISTRY.default_format
# return self._REGISTRY.formatter.format_measurement(self, spec)
def __format__(self, spec):
spec = spec or self._REGISTRY.default_format
return self._REGISTRY.formatter.format_quantitykind(self, spec)

@property
def kinds(self) -> KindKind:
Expand Down
4 changes: 2 additions & 2 deletions pint/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,8 @@ def _is_dim(name: str) -> bool:


class SharedRegistryObject:
"""
"""Base class for object keeping a reference to the registree.
Such object are for now Quantity and Unit, in a number of places it is
that an object from this class has a '_units' attribute.
Expand Down Expand Up @@ -1016,7 +1017,6 @@ def _check(self, other: Any) -> bool:
return False



class PrettyIPython:
"""Mixin to add pretty-printers for IPython"""

Expand Down

0 comments on commit 1c1ebdd

Please sign in to comment.