Skip to content

Round numbers

round_numbers

Round-numbers strategy — reduce float precision.

RoundNumbersStrategy

Bases: SmeltStrategy

Recursively round floats to precision decimal places.

Source code in packages/axm-smelt/src/axm_smelt/strategies/round_numbers.py
Python
class RoundNumbersStrategy(SmeltStrategy):
    """Recursively round floats to *precision* decimal places."""

    def __init__(self, precision: int = 2) -> None:
        self._precision = precision

    @property
    def name(self) -> str:
        """Strategy identifier used in the registry."""
        return "round_numbers"

    @property
    def category(self) -> str:
        """Strategy category (``cosmetic``)."""
        return "cosmetic"

    def apply(self, ctx: SmeltContext) -> SmeltContext:
        """Round float values to the configured precision.

        Uses ``ctx.parsed`` for the JSON path; otherwise re-parses text
        that looks like JSON. Non-JSON text is returned unchanged (no
        regex rounding, which would corrupt floats embedded in strings).
        Propagates the rounded object as ``parsed`` on the returned
        context.
        """
        parsed = ctx.parsed
        if parsed is not None:
            rounded = _round_walk(parsed, self._precision)
            result = json.dumps(
                rounded, sort_keys=True, separators=(",", ":"), ensure_ascii=False
            )
            return SmeltContext(text=result, format=ctx.format, parsed=rounded)

        text = ctx.text
        stripped = text.strip()
        if not stripped:
            return ctx

        if stripped[0] in ("{", "["):
            try:
                data = json.loads(stripped)
                rounded = _round_walk(data, self._precision)
                result = json.dumps(
                    rounded, sort_keys=True, separators=(",", ":"), ensure_ascii=False
                )
                return SmeltContext(text=result, format=ctx.format, parsed=rounded)
            except (json.JSONDecodeError, ValueError):
                pass

        return ctx
category property

Strategy category (cosmetic).

name property

Strategy identifier used in the registry.

apply(ctx)

Round float values to the configured precision.

Uses ctx.parsed for the JSON path; otherwise re-parses text that looks like JSON. Non-JSON text is returned unchanged (no regex rounding, which would corrupt floats embedded in strings). Propagates the rounded object as parsed on the returned context.

Source code in packages/axm-smelt/src/axm_smelt/strategies/round_numbers.py
Python
def apply(self, ctx: SmeltContext) -> SmeltContext:
    """Round float values to the configured precision.

    Uses ``ctx.parsed`` for the JSON path; otherwise re-parses text
    that looks like JSON. Non-JSON text is returned unchanged (no
    regex rounding, which would corrupt floats embedded in strings).
    Propagates the rounded object as ``parsed`` on the returned
    context.
    """
    parsed = ctx.parsed
    if parsed is not None:
        rounded = _round_walk(parsed, self._precision)
        result = json.dumps(
            rounded, sort_keys=True, separators=(",", ":"), ensure_ascii=False
        )
        return SmeltContext(text=result, format=ctx.format, parsed=rounded)

    text = ctx.text
    stripped = text.strip()
    if not stripped:
        return ctx

    if stripped[0] in ("{", "["):
        try:
            data = json.loads(stripped)
            rounded = _round_walk(data, self._precision)
            result = json.dumps(
                rounded, sort_keys=True, separators=(",", ":"), ensure_ascii=False
            )
            return SmeltContext(text=result, format=ctx.format, parsed=rounded)
        except (json.JSONDecodeError, ValueError):
            pass

    return ctx