Skip to content

Commit 6d6c052

Browse files
committed
monkey-patch Unit class to make "clean" the default format option
1 parent 6df6538 commit 6d6c052

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

gemd/units/impl.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,16 @@ def _format_clean(unit, registry, **options):
271271
return " * ".join(numerator)
272272

273273

274+
"""
275+
We can make "clean" the default formatting option by monkey-patching the
276+
__format__ method of the Unit class
277+
"""
278+
_super_format_unit = UnitRegistry.Unit.__format__
279+
def _format_unit(self, spec) -> str:
280+
return _super_format_unit(self, spec or 'clean')
281+
UnitRegistry.Unit.__format__ = _format_unit
282+
283+
274284
@functools.lru_cache(maxsize=1024)
275285
def parse_units(units: Union[str, Unit, None],
276286
*,

0 commit comments

Comments
 (0)