Source code for jitxlib.jlcpcb.export.values

"""Rendering a component value the way JLCPCB's own examples spell it.

Their BOM page uses `10k` and `470uF`, so that is the target: compact SI, no
space, ASCII only. This needs a hand-written formatter. `jitx.units` builds a
bare pint registry with no formatter, so `str()` gives `1e-07 farad`,
`to_compact()` alone gives `99.99999999999999 nF`, and pint's short formats
render ohm and micro as non-ASCII signs.
"""

from __future__ import annotations

from typing import Any

MICRO = "µ"
OHM = "Ω"


[docs] def render(value: Any) -> str | None: """A BOM `Comment` for a component value, or None when there is none. A value that is already a string passes through untouched: `Component.value` is `str | PlainQuantity | None`. """ if value is None: return None if isinstance(value, str): return value try: compact = value.to_compact() magnitude = f"{compact.magnitude:.6g}" unit = f"{compact.units:~}" except (AttributeError, TypeError, ValueError): return str(value) return magnitude + _unit(unit)
def _unit(unit: str) -> str: """The SI prefix and symbol, ASCII, with resistance spelled the short way. A resistance drops the unit symbol, which is what `10k` means; with no prefix to carry it, an `R` stands in so the number is not bare. """ unit = unit.replace(MICRO, "u") if unit.endswith(OHM): prefix = unit[: -len(OHM)] return prefix if prefix else "R" return unit