summaryrefslogtreecommitdiff
path: root/pint
diff options
context:
space:
mode:
authorKeewis <keewis@posteo.de>2021-08-14 15:07:29 +0200
committerKeewis <keewis@posteo.de>2021-08-14 15:07:29 +0200
commitcf56f4bef9c2d5564215aca428f8a474e744c1ed (patch)
tree56fec0f28732384e5d31c9adcf78863b1955b501 /pint
parent6250bff0cf9eb79671fecd45d46d6f9f9340e130 (diff)
downloadpint-cf56f4bef9c2d5564215aca428f8a474e744c1ed.tar.gz
add a register function and convert _FORMATS to a dict of callables
Diffstat (limited to 'pint')
-rw-r--r--pint/formatting.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/pint/formatting.py b/pint/formatting.py
index 5983e3c..99c73ca 100644
--- a/pint/formatting.py
+++ b/pint/formatting.py
@@ -9,7 +9,7 @@
"""
import re
-from typing import Dict
+from typing import Callable, Dict
from .babel_names import _babel_lengths, _babel_units
from .compat import babel_parse
@@ -71,7 +71,16 @@ def _pretty_fmt_exponent(num):
#: _FORMATS maps format names to callables doing the formatting
-_FORMATS: Dict[str, dict] = {}
+_FORMATS: Dict[str, Callable] = {}
+
+
+def register_unit_format(name):
+ def wrapper(func):
+ if name in _FORMATS:
+ raise ValueError("format already exists") # or warn instead
+ _FORMATS[name] = func
+
+ return wrapper
def formatter(